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