MCPcopy Create free account
hub / github.com/instantdb/instant / _HTTP

Class _HTTP

client/packages/python/src/instantdb/_sync/http.py:27–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26
27class _HTTP:
28 def __init__(
29 self,
30 *,
31 app_id: str,
32 admin_token: str | None,
33 api_uri: str = DEFAULT_API_URI,
34 impersonation: dict[str, str] | None = None,
35 transport: httpx.BaseTransport | None = None,
36 shared_client: httpx.Client | None = None,
37 ) -> None:
38 self._app_id = app_id
39 self._admin_token = admin_token
40 self._api_uri = api_uri.rstrip("/")
41 self._impersonation = impersonation or {}
42 if shared_client is not None:
43 self._client = shared_client
44 self._owns_client = False
45 else:
46 self._client = httpx.Client(
47 base_url=self._api_uri,
48 timeout=DEFAULT_TIMEOUT,
49 transport=transport,
50 )
51 self._owns_client = True
52
53 def _headers(
54 self, *, unauthenticated: bool = False, admin_only: bool = False
55 ) -> dict[str, str]:
56 if unauthenticated:
57 return {"content-type": "application/json"}
58 self._validate_auth()
59 h: dict[str, str] = {
60 "content-type": "application/json",
61 "app-id": self._app_id,
62 "Instant-Admin-Version": __version__,
63 "Instant-Core-Version": __version__,
64 }
65 if self._admin_token:
66 h["authorization"] = f"Bearer {self._admin_token}"
67 # admin_only skips impersonation so the request uses admin auth.
68 if not admin_only:
69 h.update(self._impersonation)
70 return h
71
72 def _validate_auth(self) -> None:
73 if self._admin_token:
74 return
75 if "as-token" in self._impersonation or "as-guest" in self._impersonation:
76 return
77 if "as-email" in self._impersonation:
78 raise InstantError(
79 "admin_token is required to impersonate a user by email: "
80 "pass admin_token=... or set INSTANT_APP_ADMIN_TOKEN"
81 )
82 raise InstantError(
83 "admin_token is required for this operation: "
84 "pass admin_token=..., set INSTANT_APP_ADMIN_TOKEN, or use as_user(...)"

Callers 2

__init__Method · 0.90

Calls

no outgoing calls