corebase 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. corebase/__init__.py +108 -0
  2. corebase/_async/__init__.py +1 -0
  3. corebase/_async/auth_client.py +55 -0
  4. corebase/_async/client.py +398 -0
  5. corebase/_sync/__init__.py +1 -0
  6. corebase/_sync/auth_client.py +55 -0
  7. corebase/_sync/client.py +393 -0
  8. corebase/auth/__init__.py +20 -0
  9. corebase/auth/_async/__init__.py +1 -0
  10. corebase/auth/_async/gotrue_admin_api.py +356 -0
  11. corebase/auth/_async/gotrue_admin_mfa_api.py +32 -0
  12. corebase/auth/_async/gotrue_admin_oauth_api.py +93 -0
  13. corebase/auth/_async/gotrue_base_api.py +78 -0
  14. corebase/auth/_async/gotrue_client.py +1404 -0
  15. corebase/auth/_async/gotrue_mfa_api.py +94 -0
  16. corebase/auth/_async/storage.py +31 -0
  17. corebase/auth/_sync/__init__.py +1 -0
  18. corebase/auth/_sync/gotrue_admin_api.py +356 -0
  19. corebase/auth/_sync/gotrue_admin_mfa_api.py +32 -0
  20. corebase/auth/_sync/gotrue_admin_oauth_api.py +93 -0
  21. corebase/auth/_sync/gotrue_base_api.py +78 -0
  22. corebase/auth/_sync/gotrue_client.py +1394 -0
  23. corebase/auth/_sync/gotrue_mfa_api.py +94 -0
  24. corebase/auth/_sync/storage.py +31 -0
  25. corebase/auth/constants.py +15 -0
  26. corebase/auth/errors.py +259 -0
  27. corebase/auth/helpers.py +288 -0
  28. corebase/auth/py.typed +0 -0
  29. corebase/auth/timer.py +53 -0
  30. corebase/auth/types.py +1021 -0
  31. corebase/auth/version.py +1 -0
  32. corebase/client.py +12 -0
  33. corebase/functions/__init__.py +47 -0
  34. corebase/functions/_async/__init__.py +1 -0
  35. corebase/functions/_async/functions_client.py +218 -0
  36. corebase/functions/_sync/__init__.py +1 -0
  37. corebase/functions/_sync/functions_client.py +218 -0
  38. corebase/functions/errors.py +62 -0
  39. corebase/functions/py.typed +0 -0
  40. corebase/functions/utils.py +33 -0
  41. corebase/functions/version.py +1 -0
  42. corebase/lib/__init__.py +3 -0
  43. corebase/lib/client_options.py +189 -0
  44. corebase/migrate/__init__.py +374 -0
  45. corebase/migrate/__main__.py +7 -0
  46. corebase/postgrest/__init__.py +62 -0
  47. corebase/postgrest/_async/__init__.py +1 -0
  48. corebase/postgrest/_async/client.py +206 -0
  49. corebase/postgrest/_async/request_builder.py +503 -0
  50. corebase/postgrest/_sync/__init__.py +1 -0
  51. corebase/postgrest/_sync/client.py +206 -0
  52. corebase/postgrest/_sync/request_builder.py +503 -0
  53. corebase/postgrest/base_client.py +61 -0
  54. corebase/postgrest/base_request_builder.py +787 -0
  55. corebase/postgrest/constants.py +6 -0
  56. corebase/postgrest/exceptions.py +115 -0
  57. corebase/postgrest/py.typed +0 -0
  58. corebase/postgrest/types.py +72 -0
  59. corebase/postgrest/utils.py +91 -0
  60. corebase/postgrest/version.py +1 -0
  61. corebase/py.typed +0 -0
  62. corebase/realtime/__init__.py +17 -0
  63. corebase/realtime/_async/__init__.py +0 -0
  64. corebase/realtime/_async/channel.py +766 -0
  65. corebase/realtime/_async/client.py +498 -0
  66. corebase/realtime/_async/presence.py +185 -0
  67. corebase/realtime/_async/push.py +162 -0
  68. corebase/realtime/_async/timer.py +41 -0
  69. corebase/realtime/_sync/__init__.py +0 -0
  70. corebase/realtime/_sync/channel.py +30 -0
  71. corebase/realtime/_sync/client.py +71 -0
  72. corebase/realtime/_sync/presence.py +10 -0
  73. corebase/realtime/exceptions.py +38 -0
  74. corebase/realtime/message.py +176 -0
  75. corebase/realtime/py.typed +0 -0
  76. corebase/realtime/transformers.py +9 -0
  77. corebase/realtime/types.py +246 -0
  78. corebase/realtime/utils.py +5 -0
  79. corebase/realtime/version.py +1 -0
  80. corebase/storage/__init__.py +56 -0
  81. corebase/storage/_async/__init__.py +1 -0
  82. corebase/storage/_async/analytics.py +82 -0
  83. corebase/storage/_async/bucket.py +142 -0
  84. corebase/storage/_async/client.py +106 -0
  85. corebase/storage/_async/file_api.py +630 -0
  86. corebase/storage/_async/request.py +32 -0
  87. corebase/storage/_async/vectors.py +213 -0
  88. corebase/storage/_sync/__init__.py +1 -0
  89. corebase/storage/_sync/analytics.py +80 -0
  90. corebase/storage/_sync/bucket.py +142 -0
  91. corebase/storage/_sync/client.py +106 -0
  92. corebase/storage/_sync/file_api.py +630 -0
  93. corebase/storage/_sync/request.py +32 -0
  94. corebase/storage/_sync/vectors.py +199 -0
  95. corebase/storage/constants.py +16 -0
  96. corebase/storage/exceptions.py +84 -0
  97. corebase/storage/py.typed +0 -0
  98. corebase/storage/types.py +313 -0
  99. corebase/storage/utils.py +26 -0
  100. corebase/storage/version.py +1 -0
  101. corebase/types.py +8 -0
  102. corebase/version.py +1 -0
  103. corebase-1.0.0.dist-info/METADATA +78 -0
  104. corebase-1.0.0.dist-info/RECORD +106 -0
  105. corebase-1.0.0.dist-info/WHEEL +4 -0
  106. corebase-1.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,393 @@
1
+ import copy
2
+ import re
3
+ from typing import Any
4
+
5
+ from httpx import Timeout
6
+ from corebase.postgrest import (
7
+ SyncPostgrestClient,
8
+ SyncRequestBuilder,
9
+ SyncRPCFilterRequestBuilder,
10
+ )
11
+ from corebase.postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT
12
+ from corebase.postgrest.types import CountMethod
13
+ from corebase.realtime import RealtimeChannelOptions, SyncRealtimeChannel, SyncRealtimeClient
14
+ from corebase.storage import SyncStorageClient
15
+ from corebase.storage.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT
16
+ from corebase.auth import SyncMemoryStorage
17
+ from corebase.auth.types import AuthChangeEvent, Session
18
+ from corebase.functions import SyncFunctionsClient
19
+ from yarl import URL
20
+
21
+ from ..lib.client_options import SyncClientOptions as ClientOptions
22
+ from ..lib.client_options import SyncHttpxClient
23
+ from ..types import RealtimeClientOptions
24
+ from .auth_client import SyncSupabaseAuthClient
25
+
26
+
27
+ # Create an exception class when user does not provide a valid url or key.
28
+ class SupabaseException(Exception):
29
+ def __init__(self, message: str) -> None:
30
+ self.message = message
31
+ super().__init__(self.message)
32
+
33
+
34
+ def _validate_str_arg(name: str, value: object) -> None:
35
+ if value is not None and not isinstance(value, str):
36
+ raise SupabaseException(f"{name} must be a string, got {type(value).__name__}")
37
+ if not value:
38
+ raise SupabaseException(f"{name} is required")
39
+
40
+
41
+ class Client:
42
+ """Supabase client class."""
43
+
44
+ def __init__(
45
+ self,
46
+ supabase_url: str,
47
+ supabase_key: str,
48
+ options: ClientOptions | None = None,
49
+ ) -> None:
50
+ """Instantiate the client.
51
+
52
+ Parameters
53
+ ----------
54
+ supabase_url: str
55
+ The URL to the Supabase instance that should be connected to.
56
+ supabase_key: str
57
+ The API key to the Supabase instance that should be connected to.
58
+ **options
59
+ Any extra settings to be optionally specified - also see the
60
+ `DEFAULT_OPTIONS` dict.
61
+ """
62
+
63
+ _validate_str_arg("supabase_url", supabase_url)
64
+ _validate_str_arg("supabase_key", supabase_key)
65
+
66
+ # Check if the url and key are valid
67
+ if not re.match(r"^(https?)://.+", supabase_url):
68
+ raise SupabaseException("Invalid URL")
69
+
70
+ if options is None:
71
+ options = ClientOptions(storage=SyncMemoryStorage())
72
+
73
+ self.supabase_url = (
74
+ URL(supabase_url) if supabase_url.endswith("/") else URL(supabase_url + "/")
75
+ )
76
+ self.supabase_key = supabase_key
77
+ self.options = copy.copy(options)
78
+ self.options.headers = {
79
+ **options.headers,
80
+ **self._get_auth_headers(),
81
+ }
82
+
83
+ self.rest_url = self.supabase_url.joinpath("rest", "v1")
84
+ self.realtime_url = self.supabase_url.joinpath("realtime", "v1").with_scheme(
85
+ "wss" if self.supabase_url.scheme == "https" else "ws"
86
+ )
87
+ self.auth_url = self.supabase_url.joinpath("auth", "v1")
88
+ self.storage_url = self.supabase_url.joinpath("storage", "v1", "")
89
+ self.functions_url = self.supabase_url.joinpath("functions", "v1")
90
+
91
+ # Instantiate clients.
92
+ self.auth = self._init_supabase_auth_client(
93
+ auth_url=str(self.auth_url),
94
+ client_options=self.options,
95
+ )
96
+ self.realtime = self._init_realtime_client(
97
+ realtime_url=self.realtime_url,
98
+ supabase_key=self.supabase_key,
99
+ options=self.options.realtime if self.options else None,
100
+ )
101
+ self._postgrest: SyncPostgrestClient | None = None
102
+ self._storage: SyncStorageClient | None = None
103
+ self._functions: SyncFunctionsClient | None = None
104
+ self.auth.on_auth_state_change(self._listen_to_auth_events)
105
+
106
+ @classmethod
107
+ def create(
108
+ cls,
109
+ supabase_url: str,
110
+ supabase_key: str,
111
+ options: ClientOptions | None = None,
112
+ ) -> "Client":
113
+ auth_header = options.headers.get("Authorization") if options else None
114
+ client = cls(supabase_url, supabase_key, options)
115
+
116
+ if auth_header is None:
117
+ try:
118
+ session = client.auth.get_session()
119
+ session_access_token = (
120
+ client._create_auth_header(session.access_token)
121
+ if session
122
+ else None
123
+ )
124
+ except Exception:
125
+ session_access_token = None
126
+
127
+ client.options.headers.update(
128
+ client._get_auth_headers(session_access_token)
129
+ )
130
+
131
+ return client
132
+
133
+ def table(self, table_name: str) -> SyncRequestBuilder:
134
+ """Perform a table operation.
135
+
136
+ Note that the supabase client uses the `from` method, but in Python,
137
+ this is a reserved keyword, so we have elected to use the name `table`.
138
+ Alternatively you can use the `.from_()` method.
139
+ """
140
+ return self.from_(table_name)
141
+
142
+ def schema(self, schema: str) -> SyncPostgrestClient:
143
+ """Select a schema to query or perform an function (rpc) call.
144
+
145
+ The schema needs to be on the list of exposed schemas inside Supabase.
146
+ """
147
+ return self.postgrest.schema(schema)
148
+
149
+ def from_(self, table_name: str) -> SyncRequestBuilder:
150
+ """Perform a table operation.
151
+
152
+ See the `table` method.
153
+ """
154
+ return self.postgrest.from_(table_name)
155
+
156
+ def rpc(
157
+ self,
158
+ fn: str,
159
+ params: dict[Any, Any] | None = None,
160
+ count: CountMethod | None = None,
161
+ head: bool = False,
162
+ get: bool = False,
163
+ ) -> SyncRPCFilterRequestBuilder:
164
+ """Performs a stored procedure call.
165
+
166
+ Parameters
167
+ ----------
168
+ fn : callable
169
+ The stored procedure call to be executed.
170
+ params : dict of any
171
+ Parameters passed into the stored procedure call.
172
+ count: The method to use to get the count of rows returned.
173
+ head: When set to `true`, `data` will not be returned. Useful if you only need the count.
174
+ get: When set to `true`, the function will be called with read-only access mode.
175
+
176
+ Returns
177
+ -------
178
+ SyncFilterRequestBuilder
179
+ Returns a filter builder. This lets you apply filters on the response
180
+ of an RPC.
181
+ """
182
+ if params is None:
183
+ params = {}
184
+ return self.postgrest.rpc(fn, params, count, head, get)
185
+
186
+ @property
187
+ def postgrest(self) -> SyncPostgrestClient:
188
+ if self._postgrest is None:
189
+ self._postgrest = self._init_postgrest_client(
190
+ rest_url=str(self.rest_url),
191
+ headers=self.options.headers,
192
+ schema=self.options.schema,
193
+ timeout=self.options.postgrest_client_timeout,
194
+ retry_enabled=self.options.postgrest_client_retry,
195
+ http_client=self.options.httpx_client,
196
+ )
197
+
198
+ return self._postgrest
199
+
200
+ @property
201
+ def storage(self) -> SyncStorageClient:
202
+ if self._storage is None:
203
+ self._storage = self._init_storage_client(
204
+ storage_url=str(self.storage_url),
205
+ headers=self.options.headers,
206
+ storage_client_timeout=self.options.storage_client_timeout,
207
+ http_client=self.options.httpx_client,
208
+ )
209
+ return self._storage
210
+
211
+ @property
212
+ def functions(self) -> SyncFunctionsClient:
213
+ if self._functions is None:
214
+ self._functions = SyncFunctionsClient(
215
+ url=str(self.functions_url),
216
+ headers=self.options.headers,
217
+ timeout=(
218
+ self.options.function_client_timeout
219
+ if self.options.httpx_client is None
220
+ else None
221
+ ),
222
+ http_client=self.options.httpx_client,
223
+ )
224
+ return self._functions
225
+
226
+ def channel(
227
+ self, topic: str, params: RealtimeChannelOptions | None = None
228
+ ) -> SyncRealtimeChannel:
229
+ """Creates a Realtime channel with Broadcast, Presence, and Postgres Changes."""
230
+ return self.realtime.channel(topic, params or {})
231
+
232
+ def get_channels(self) -> list[SyncRealtimeChannel]:
233
+ """Returns all realtime channels."""
234
+ return self.realtime.get_channels()
235
+
236
+ def remove_channel(self, channel: SyncRealtimeChannel) -> None:
237
+ """Unsubscribes and removes Realtime channel from Realtime client."""
238
+ self.realtime.remove_channel(channel)
239
+
240
+ def remove_all_channels(self) -> None:
241
+ """Unsubscribes and removes all Realtime channels from Realtime client."""
242
+ self.realtime.remove_all_channels()
243
+
244
+ @staticmethod
245
+ def _init_realtime_client(
246
+ realtime_url: URL,
247
+ supabase_key: str,
248
+ options: RealtimeClientOptions | None = None,
249
+ ) -> SyncRealtimeClient:
250
+ realtime_options = options or {}
251
+ """Private method for creating an instance of the realtime-py client."""
252
+ return SyncRealtimeClient(
253
+ str(realtime_url), token=supabase_key, **realtime_options
254
+ )
255
+
256
+ @staticmethod
257
+ def _init_storage_client(
258
+ storage_url: str,
259
+ headers: dict[str, str],
260
+ storage_client_timeout: int = DEFAULT_STORAGE_CLIENT_TIMEOUT,
261
+ verify: bool = True,
262
+ proxy: str | None = None,
263
+ http_client: SyncHttpxClient | None = None,
264
+ ) -> SyncStorageClient:
265
+ if http_client is not None:
266
+ # If an http client is provided, use it
267
+ return SyncStorageClient(
268
+ url=storage_url, headers=headers, http_client=http_client
269
+ )
270
+ return SyncStorageClient(
271
+ url=storage_url,
272
+ headers=headers,
273
+ timeout=storage_client_timeout,
274
+ verify=verify,
275
+ proxy=proxy,
276
+ http_client=None,
277
+ )
278
+
279
+ @staticmethod
280
+ def _init_supabase_auth_client(
281
+ auth_url: str,
282
+ client_options: ClientOptions,
283
+ verify: bool = True,
284
+ proxy: str | None = None,
285
+ ) -> SyncSupabaseAuthClient:
286
+ """Creates a wrapped instance of the GoTrue Client."""
287
+ return SyncSupabaseAuthClient(
288
+ url=auth_url,
289
+ auto_refresh_token=client_options.auto_refresh_token,
290
+ persist_session=client_options.persist_session,
291
+ storage=client_options.storage,
292
+ headers=client_options.headers,
293
+ flow_type=client_options.flow_type,
294
+ verify=verify,
295
+ proxy=proxy,
296
+ http_client=client_options.httpx_client,
297
+ )
298
+
299
+ @staticmethod
300
+ def _init_postgrest_client(
301
+ rest_url: str,
302
+ headers: dict[str, str],
303
+ schema: str,
304
+ timeout: int | float | Timeout = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
305
+ verify: bool = True,
306
+ proxy: str | None = None,
307
+ http_client: SyncHttpxClient | None = None,
308
+ retry_enabled: bool = True,
309
+ ) -> SyncPostgrestClient:
310
+ """Private helper for creating an instance of the Postgrest client."""
311
+ if http_client is not None:
312
+ # If an http client is provided, use it
313
+ return SyncPostgrestClient(
314
+ rest_url,
315
+ headers=headers,
316
+ schema=schema,
317
+ http_client=http_client,
318
+ retry_enabled=retry_enabled,
319
+ )
320
+ return SyncPostgrestClient(
321
+ rest_url,
322
+ headers=headers,
323
+ schema=schema,
324
+ timeout=timeout,
325
+ verify=verify,
326
+ proxy=proxy,
327
+ http_client=None,
328
+ retry_enabled=retry_enabled,
329
+ )
330
+
331
+ def _create_auth_header(self, token: str) -> str:
332
+ return f"Bearer {token}"
333
+
334
+ def _get_auth_headers(self, authorization: str | None = None) -> dict[str, str]:
335
+ if authorization is None:
336
+ authorization = self.options.headers.get(
337
+ "Authorization", self._create_auth_header(self.supabase_key)
338
+ )
339
+
340
+ """Helper method to get auth headers."""
341
+ return {
342
+ "apiKey": self.supabase_key,
343
+ "Authorization": authorization,
344
+ }
345
+
346
+ def _listen_to_auth_events(
347
+ self, event: AuthChangeEvent, session: Session | None
348
+ ) -> None:
349
+ # Every event except SIGNED_OUT carries the current session, including
350
+ # USER_UPDATED and MFA_CHALLENGE_VERIFIED, whose token can carry new claims (aal2).
351
+ self._postgrest = None
352
+ self._storage = None
353
+ self._functions = None
354
+ access_token = session.access_token if session else self.supabase_key
355
+ auth_header = self._create_auth_header(access_token)
356
+ self.options.headers["Authorization"] = auth_header
357
+ self.auth._headers["Authorization"] = auth_header
358
+
359
+
360
+ def create_client(
361
+ supabase_url: str,
362
+ supabase_key: str,
363
+ options: ClientOptions | None = None,
364
+ ) -> Client:
365
+ """Create client function to instantiate supabase client like JS runtime.
366
+
367
+ Parameters
368
+ ----------
369
+ supabase_url: str
370
+ The URL to the Supabase instance that should be connected to.
371
+ supabase_key: str
372
+ The API key to the Supabase instance that should be connected to.
373
+ **options
374
+ Any extra settings to be optionally specified - also see the
375
+ `DEFAULT_OPTIONS` dict.
376
+
377
+ Examples
378
+ --------
379
+ Instantiating the client.
380
+ >>> import os
381
+ >>> from supabase import create_client, Client
382
+ >>>
383
+ >>> url: str = os.environ.get("SUPABASE_TEST_URL")
384
+ >>> key: str = os.environ.get("SUPABASE_TEST_KEY")
385
+ >>> supabase: Client = create_client(url, key)
386
+
387
+ Returns
388
+ -------
389
+ Client
390
+ """
391
+ return Client.create(
392
+ supabase_url=supabase_url, supabase_key=supabase_key, options=options
393
+ )
@@ -0,0 +1,20 @@
1
+ from __future__ import annotations
2
+
3
+ from ._async.gotrue_admin_api import AsyncGoTrueAdminAPI as AsyncGoTrueAdminAPI
4
+ from ._async.gotrue_client import AsyncGoTrueClient as AsyncGoTrueClient
5
+ from ._async.storage import (
6
+ AsyncMemoryStorage as AsyncMemoryStorage,
7
+ )
8
+ from ._async.storage import (
9
+ AsyncSupportedStorage as AsyncSupportedStorage,
10
+ )
11
+ from ._sync.gotrue_admin_api import SyncGoTrueAdminAPI as SyncGoTrueAdminAPI
12
+ from ._sync.gotrue_client import SyncGoTrueClient as SyncGoTrueClient
13
+ from ._sync.storage import (
14
+ SyncMemoryStorage as SyncMemoryStorage,
15
+ )
16
+ from ._sync.storage import (
17
+ SyncSupportedStorage as SyncSupportedStorage,
18
+ )
19
+ from .types import *
20
+ from .version import __version__ as __version__
@@ -0,0 +1 @@
1
+ from __future__ import annotations