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/auth/version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0" # {x-release-please-version}
|
corebase/client.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import corebase as _package
|
|
2
|
+
|
|
3
|
+
from . import *
|
|
4
|
+
from ._async.auth_client import AsyncSupabaseAuthClient as AsyncSupabaseAuthClient
|
|
5
|
+
from ._async.client import AsyncStorageClient as _AsyncStorageClient
|
|
6
|
+
|
|
7
|
+
AsyncSupabaseStorageClient = _AsyncStorageClient
|
|
8
|
+
|
|
9
|
+
__all__ = _package.__all__ + (
|
|
10
|
+
"AsyncSupabaseAuthClient",
|
|
11
|
+
"AsyncSupabaseStorageClient",
|
|
12
|
+
)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal, overload
|
|
4
|
+
|
|
5
|
+
from ._async.functions_client import AsyncFunctionsClient
|
|
6
|
+
from ._sync.functions_client import SyncFunctionsClient
|
|
7
|
+
from .utils import FunctionRegion
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"create_client",
|
|
11
|
+
"FunctionRegion",
|
|
12
|
+
"AsyncFunctionsClient",
|
|
13
|
+
"SyncFunctionsClient",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@overload
|
|
18
|
+
def create_client(
|
|
19
|
+
url: str,
|
|
20
|
+
headers: dict[str, str],
|
|
21
|
+
*,
|
|
22
|
+
is_async: Literal[True],
|
|
23
|
+
verify: bool = ...,
|
|
24
|
+
) -> AsyncFunctionsClient: ...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@overload
|
|
28
|
+
def create_client(
|
|
29
|
+
url: str,
|
|
30
|
+
headers: dict[str, str],
|
|
31
|
+
*,
|
|
32
|
+
is_async: Literal[False],
|
|
33
|
+
verify: bool = ...,
|
|
34
|
+
) -> SyncFunctionsClient: ...
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def create_client(
|
|
38
|
+
url: str,
|
|
39
|
+
headers: dict[str, str],
|
|
40
|
+
*,
|
|
41
|
+
is_async: bool,
|
|
42
|
+
verify: bool = True,
|
|
43
|
+
) -> AsyncFunctionsClient | SyncFunctionsClient:
|
|
44
|
+
if is_async:
|
|
45
|
+
return AsyncFunctionsClient(url, headers, verify=verify)
|
|
46
|
+
else:
|
|
47
|
+
return SyncFunctionsClient(url, headers, verify=verify)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import platform
|
|
2
|
+
from typing import Any, Literal
|
|
3
|
+
from warnings import warn
|
|
4
|
+
|
|
5
|
+
from httpx import URL as HttpxURL
|
|
6
|
+
from httpx import AsyncClient, Headers, HTTPError, QueryParams, Request, Response
|
|
7
|
+
from yarl import URL
|
|
8
|
+
|
|
9
|
+
from ..errors import FunctionsHttpError, FunctionsRelayError, _error_message_from
|
|
10
|
+
from ..utils import (
|
|
11
|
+
FunctionRegion,
|
|
12
|
+
is_http_url,
|
|
13
|
+
is_valid_str_arg,
|
|
14
|
+
)
|
|
15
|
+
from ..version import __version__
|
|
16
|
+
|
|
17
|
+
HTTPMethod = Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _port_or_default(url: HttpxURL) -> int | None:
|
|
21
|
+
if url.port is not None:
|
|
22
|
+
return url.port
|
|
23
|
+
return {"http": 80, "https": 443}.get(url.scheme)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _keeps_credentials(source: HttpxURL, target: HttpxURL) -> bool:
|
|
27
|
+
if source.host != target.host:
|
|
28
|
+
return False
|
|
29
|
+
if source.scheme == target.scheme:
|
|
30
|
+
return _port_or_default(source) == _port_or_default(target)
|
|
31
|
+
return (
|
|
32
|
+
source.scheme == "http"
|
|
33
|
+
and _port_or_default(source) == 80
|
|
34
|
+
and target.scheme == "https"
|
|
35
|
+
and _port_or_default(target) == 443
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _RedirectGuardClient(AsyncClient):
|
|
40
|
+
"""Drops the ``apiKey`` header on redirects that leave the origin.
|
|
41
|
+
|
|
42
|
+
httpx only strips ``Authorization`` on such redirects, so the project key
|
|
43
|
+
(the ``service_role`` key in server side usage) would otherwise be sent to
|
|
44
|
+
whatever host an invoked edge function redirects to.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def _redirect_headers(
|
|
48
|
+
self, request: Request, url: HttpxURL, method: str
|
|
49
|
+
) -> Headers:
|
|
50
|
+
headers = super()._redirect_headers(request, url, method)
|
|
51
|
+
if not _keeps_credentials(request.url, url):
|
|
52
|
+
headers.pop("apiKey", None)
|
|
53
|
+
return headers
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class AsyncFunctionsClient:
|
|
57
|
+
def __init__(
|
|
58
|
+
self,
|
|
59
|
+
url: str,
|
|
60
|
+
headers: dict,
|
|
61
|
+
timeout: int | None = None,
|
|
62
|
+
verify: bool | None = None,
|
|
63
|
+
proxy: str | None = None,
|
|
64
|
+
http_client: AsyncClient | None = None,
|
|
65
|
+
) -> None:
|
|
66
|
+
if not is_http_url(url):
|
|
67
|
+
raise ValueError("url must be a valid HTTP URL string")
|
|
68
|
+
self.url = URL(url)
|
|
69
|
+
self.headers = Headers(
|
|
70
|
+
{
|
|
71
|
+
"X-Client-Info": (
|
|
72
|
+
f"corebase-py/supabase_functions v{__version__}"
|
|
73
|
+
f"; platform={platform.system()}"
|
|
74
|
+
f"; platform-version={platform.release()}"
|
|
75
|
+
f"; runtime=python"
|
|
76
|
+
f"; runtime-version={platform.python_version()}"
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
self.headers.update(headers)
|
|
81
|
+
|
|
82
|
+
if timeout is not None:
|
|
83
|
+
warn(
|
|
84
|
+
"The 'timeout' parameter is deprecated. Please configure it in the http client instead.",
|
|
85
|
+
DeprecationWarning,
|
|
86
|
+
stacklevel=2,
|
|
87
|
+
)
|
|
88
|
+
if verify is not None:
|
|
89
|
+
warn(
|
|
90
|
+
"The 'verify' parameter is deprecated. Please configure it in the http client instead.",
|
|
91
|
+
DeprecationWarning,
|
|
92
|
+
stacklevel=2,
|
|
93
|
+
)
|
|
94
|
+
if proxy is not None:
|
|
95
|
+
warn(
|
|
96
|
+
"The 'proxy' parameter is deprecated. Please configure it in the http client instead.",
|
|
97
|
+
DeprecationWarning,
|
|
98
|
+
stacklevel=2,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
self.verify = bool(verify) if verify is not None else True
|
|
102
|
+
self.timeout = int(abs(timeout)) if timeout is not None else 60
|
|
103
|
+
self._client = http_client or _RedirectGuardClient(
|
|
104
|
+
verify=self.verify,
|
|
105
|
+
timeout=self.timeout,
|
|
106
|
+
proxy=proxy,
|
|
107
|
+
follow_redirects=True,
|
|
108
|
+
http2=True,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
async def _request(
|
|
112
|
+
self,
|
|
113
|
+
method: HTTPMethod,
|
|
114
|
+
path: list[str],
|
|
115
|
+
headers: Headers,
|
|
116
|
+
json: dict[Any, Any] | str | bytes | None = None,
|
|
117
|
+
params: QueryParams | None = None,
|
|
118
|
+
) -> Response:
|
|
119
|
+
url = self.url.joinpath(*path)
|
|
120
|
+
response = (
|
|
121
|
+
await self._client.request(
|
|
122
|
+
method, str(url), content=json, headers=headers, params=params
|
|
123
|
+
)
|
|
124
|
+
if isinstance(json, (str, bytes))
|
|
125
|
+
else await self._client.request(
|
|
126
|
+
method, str(url), json=json, headers=headers, params=params
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
# Relay failures set this header even on non-2xx responses.
|
|
130
|
+
if response.headers.get("x-relay-error") == "true":
|
|
131
|
+
raise FunctionsRelayError(response.text, response.status_code)
|
|
132
|
+
try:
|
|
133
|
+
response.raise_for_status()
|
|
134
|
+
except HTTPError as exc:
|
|
135
|
+
status_code = None
|
|
136
|
+
if hasattr(response, "status_code"):
|
|
137
|
+
status_code = response.status_code
|
|
138
|
+
|
|
139
|
+
raise FunctionsHttpError(
|
|
140
|
+
_error_message_from(response)
|
|
141
|
+
or f"An error occurred while requesting your edge function at {exc.request.url!r}.",
|
|
142
|
+
status_code,
|
|
143
|
+
) from exc
|
|
144
|
+
|
|
145
|
+
return response
|
|
146
|
+
|
|
147
|
+
def set_auth(self, token: str) -> None:
|
|
148
|
+
"""Updates the authorization header
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
token : str
|
|
153
|
+
the new jwt token sent in the authorization header
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
self.headers["Authorization"] = f"Bearer {token}"
|
|
157
|
+
|
|
158
|
+
async def invoke(
|
|
159
|
+
self, function_name: str, invoke_options: dict | None = None
|
|
160
|
+
) -> dict | bytes:
|
|
161
|
+
"""Invokes a function
|
|
162
|
+
|
|
163
|
+
Parameters
|
|
164
|
+
----------
|
|
165
|
+
function_name : the name of the function to invoke. It may carry a query
|
|
166
|
+
string, as in `hello?name=bob`. A `#fragment` is dropped.
|
|
167
|
+
invoke_options : object with the following properties
|
|
168
|
+
`headers`: object representing the headers to send with the request
|
|
169
|
+
`body`: the body of the request
|
|
170
|
+
`method`: the HTTP method to invoke the function with. The default is `POST`
|
|
171
|
+
`responseType`: how the response should be parsed. The default is `json`
|
|
172
|
+
"""
|
|
173
|
+
if not is_valid_str_arg(function_name):
|
|
174
|
+
raise ValueError("function_name must a valid string value.")
|
|
175
|
+
# Like a URL, a name such as "hello?name=bob" carries query params and
|
|
176
|
+
# the fragment is never sent.
|
|
177
|
+
path, _, query = function_name.split("#", 1)[0].partition("?")
|
|
178
|
+
if not is_valid_str_arg(path):
|
|
179
|
+
raise ValueError("function_name must a valid string value.")
|
|
180
|
+
headers = Headers(self.headers)
|
|
181
|
+
params = QueryParams(query)
|
|
182
|
+
body = None
|
|
183
|
+
method: HTTPMethod = "POST"
|
|
184
|
+
response_type = "text/plain"
|
|
185
|
+
|
|
186
|
+
if invoke_options is not None:
|
|
187
|
+
headers.update(invoke_options.get("headers", {}))
|
|
188
|
+
method = invoke_options.get("method", "POST")
|
|
189
|
+
response_type = invoke_options.get("responseType", "text/plain")
|
|
190
|
+
|
|
191
|
+
region = invoke_options.get("region")
|
|
192
|
+
if region:
|
|
193
|
+
if not isinstance(region, FunctionRegion):
|
|
194
|
+
warn(f"Use FunctionRegion({region})", stacklevel=2)
|
|
195
|
+
region = FunctionRegion(region)
|
|
196
|
+
|
|
197
|
+
if region.value != "any":
|
|
198
|
+
headers["x-region"] = region.value
|
|
199
|
+
# Add region as query parameter
|
|
200
|
+
params = params.set("forceFunctionRegion", region.value)
|
|
201
|
+
|
|
202
|
+
body = invoke_options.get("body")
|
|
203
|
+
if headers.get("content-type", None) is None:
|
|
204
|
+
if isinstance(body, str):
|
|
205
|
+
headers["Content-Type"] = "text/plain"
|
|
206
|
+
elif isinstance(body, dict):
|
|
207
|
+
headers["Content-Type"] = "application/json"
|
|
208
|
+
elif isinstance(body, bytes):
|
|
209
|
+
headers["Content-Type"] = "application/octet-stream"
|
|
210
|
+
|
|
211
|
+
response = await self._request(
|
|
212
|
+
method, [path], headers=headers, json=body, params=params
|
|
213
|
+
)
|
|
214
|
+
if response_type == "json":
|
|
215
|
+
data = response.json()
|
|
216
|
+
else:
|
|
217
|
+
data = response.content
|
|
218
|
+
return data
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import platform
|
|
2
|
+
from typing import Any, Literal
|
|
3
|
+
from warnings import warn
|
|
4
|
+
|
|
5
|
+
from httpx import URL as HttpxURL
|
|
6
|
+
from httpx import Client, Headers, HTTPError, QueryParams, Request, Response
|
|
7
|
+
from yarl import URL
|
|
8
|
+
|
|
9
|
+
from ..errors import FunctionsHttpError, FunctionsRelayError, _error_message_from
|
|
10
|
+
from ..utils import (
|
|
11
|
+
FunctionRegion,
|
|
12
|
+
is_http_url,
|
|
13
|
+
is_valid_str_arg,
|
|
14
|
+
)
|
|
15
|
+
from ..version import __version__
|
|
16
|
+
|
|
17
|
+
HTTPMethod = Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _port_or_default(url: HttpxURL) -> int | None:
|
|
21
|
+
if url.port is not None:
|
|
22
|
+
return url.port
|
|
23
|
+
return {"http": 80, "https": 443}.get(url.scheme)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _keeps_credentials(source: HttpxURL, target: HttpxURL) -> bool:
|
|
27
|
+
if source.host != target.host:
|
|
28
|
+
return False
|
|
29
|
+
if source.scheme == target.scheme:
|
|
30
|
+
return _port_or_default(source) == _port_or_default(target)
|
|
31
|
+
return (
|
|
32
|
+
source.scheme == "http"
|
|
33
|
+
and _port_or_default(source) == 80
|
|
34
|
+
and target.scheme == "https"
|
|
35
|
+
and _port_or_default(target) == 443
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _RedirectGuardClient(Client):
|
|
40
|
+
"""Drops the ``apiKey`` header on redirects that leave the origin.
|
|
41
|
+
|
|
42
|
+
httpx only strips ``Authorization`` on such redirects, so the project key
|
|
43
|
+
(the ``service_role`` key in server side usage) would otherwise be sent to
|
|
44
|
+
whatever host an invoked edge function redirects to.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def _redirect_headers(
|
|
48
|
+
self, request: Request, url: HttpxURL, method: str
|
|
49
|
+
) -> Headers:
|
|
50
|
+
headers = super()._redirect_headers(request, url, method)
|
|
51
|
+
if not _keeps_credentials(request.url, url):
|
|
52
|
+
headers.pop("apiKey", None)
|
|
53
|
+
return headers
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class SyncFunctionsClient:
|
|
57
|
+
def __init__(
|
|
58
|
+
self,
|
|
59
|
+
url: str,
|
|
60
|
+
headers: dict,
|
|
61
|
+
timeout: int | None = None,
|
|
62
|
+
verify: bool | None = None,
|
|
63
|
+
proxy: str | None = None,
|
|
64
|
+
http_client: Client | None = None,
|
|
65
|
+
) -> None:
|
|
66
|
+
if not is_http_url(url):
|
|
67
|
+
raise ValueError("url must be a valid HTTP URL string")
|
|
68
|
+
self.url = URL(url)
|
|
69
|
+
self.headers = Headers(
|
|
70
|
+
{
|
|
71
|
+
"X-Client-Info": (
|
|
72
|
+
f"corebase-py/supabase_functions v{__version__}"
|
|
73
|
+
f"; platform={platform.system()}"
|
|
74
|
+
f"; platform-version={platform.release()}"
|
|
75
|
+
f"; runtime=python"
|
|
76
|
+
f"; runtime-version={platform.python_version()}"
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
self.headers.update(headers)
|
|
81
|
+
|
|
82
|
+
if timeout is not None:
|
|
83
|
+
warn(
|
|
84
|
+
"The 'timeout' parameter is deprecated. Please configure it in the http client instead.",
|
|
85
|
+
DeprecationWarning,
|
|
86
|
+
stacklevel=2,
|
|
87
|
+
)
|
|
88
|
+
if verify is not None:
|
|
89
|
+
warn(
|
|
90
|
+
"The 'verify' parameter is deprecated. Please configure it in the http client instead.",
|
|
91
|
+
DeprecationWarning,
|
|
92
|
+
stacklevel=2,
|
|
93
|
+
)
|
|
94
|
+
if proxy is not None:
|
|
95
|
+
warn(
|
|
96
|
+
"The 'proxy' parameter is deprecated. Please configure it in the http client instead.",
|
|
97
|
+
DeprecationWarning,
|
|
98
|
+
stacklevel=2,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
self.verify = bool(verify) if verify is not None else True
|
|
102
|
+
self.timeout = int(abs(timeout)) if timeout is not None else 60
|
|
103
|
+
self._client = http_client or _RedirectGuardClient(
|
|
104
|
+
verify=self.verify,
|
|
105
|
+
timeout=self.timeout,
|
|
106
|
+
proxy=proxy,
|
|
107
|
+
follow_redirects=True,
|
|
108
|
+
http2=True,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
def _request(
|
|
112
|
+
self,
|
|
113
|
+
method: HTTPMethod,
|
|
114
|
+
path: list[str],
|
|
115
|
+
headers: Headers,
|
|
116
|
+
json: dict[Any, Any] | str | bytes | None = None,
|
|
117
|
+
params: QueryParams | None = None,
|
|
118
|
+
) -> Response:
|
|
119
|
+
url = self.url.joinpath(*path)
|
|
120
|
+
response = (
|
|
121
|
+
self._client.request(
|
|
122
|
+
method, str(url), content=json, headers=headers, params=params
|
|
123
|
+
)
|
|
124
|
+
if isinstance(json, (str, bytes))
|
|
125
|
+
else self._client.request(
|
|
126
|
+
method, str(url), json=json, headers=headers, params=params
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
# Relay failures set this header even on non-2xx responses.
|
|
130
|
+
if response.headers.get("x-relay-error") == "true":
|
|
131
|
+
raise FunctionsRelayError(response.text, response.status_code)
|
|
132
|
+
try:
|
|
133
|
+
response.raise_for_status()
|
|
134
|
+
except HTTPError as exc:
|
|
135
|
+
status_code = None
|
|
136
|
+
if hasattr(response, "status_code"):
|
|
137
|
+
status_code = response.status_code
|
|
138
|
+
|
|
139
|
+
raise FunctionsHttpError(
|
|
140
|
+
_error_message_from(response)
|
|
141
|
+
or f"An error occurred while requesting your edge function at {exc.request.url!r}.",
|
|
142
|
+
status_code,
|
|
143
|
+
) from exc
|
|
144
|
+
|
|
145
|
+
return response
|
|
146
|
+
|
|
147
|
+
def set_auth(self, token: str) -> None:
|
|
148
|
+
"""Updates the authorization header
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
token : str
|
|
153
|
+
the new jwt token sent in the authorization header
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
self.headers["Authorization"] = f"Bearer {token}"
|
|
157
|
+
|
|
158
|
+
def invoke(
|
|
159
|
+
self, function_name: str, invoke_options: dict | None = None
|
|
160
|
+
) -> dict | bytes:
|
|
161
|
+
"""Invokes a function
|
|
162
|
+
|
|
163
|
+
Parameters
|
|
164
|
+
----------
|
|
165
|
+
function_name : the name of the function to invoke. It may carry a query
|
|
166
|
+
string, as in `hello?name=bob`. A `#fragment` is dropped.
|
|
167
|
+
invoke_options : object with the following properties
|
|
168
|
+
`headers`: object representing the headers to send with the request
|
|
169
|
+
`body`: the body of the request
|
|
170
|
+
`method`: the HTTP method to invoke the function with. The default is `POST`
|
|
171
|
+
`responseType`: how the response should be parsed. The default is `json`
|
|
172
|
+
"""
|
|
173
|
+
if not is_valid_str_arg(function_name):
|
|
174
|
+
raise ValueError("function_name must a valid string value.")
|
|
175
|
+
# Like a URL, a name such as "hello?name=bob" carries query params and
|
|
176
|
+
# the fragment is never sent.
|
|
177
|
+
path, _, query = function_name.split("#", 1)[0].partition("?")
|
|
178
|
+
if not is_valid_str_arg(path):
|
|
179
|
+
raise ValueError("function_name must a valid string value.")
|
|
180
|
+
headers = Headers(self.headers)
|
|
181
|
+
params = QueryParams(query)
|
|
182
|
+
body = None
|
|
183
|
+
method: HTTPMethod = "POST"
|
|
184
|
+
response_type = "text/plain"
|
|
185
|
+
|
|
186
|
+
if invoke_options is not None:
|
|
187
|
+
headers.update(invoke_options.get("headers", {}))
|
|
188
|
+
method = invoke_options.get("method", "POST")
|
|
189
|
+
response_type = invoke_options.get("responseType", "text/plain")
|
|
190
|
+
|
|
191
|
+
region = invoke_options.get("region")
|
|
192
|
+
if region:
|
|
193
|
+
if not isinstance(region, FunctionRegion):
|
|
194
|
+
warn(f"Use FunctionRegion({region})", stacklevel=2)
|
|
195
|
+
region = FunctionRegion(region)
|
|
196
|
+
|
|
197
|
+
if region.value != "any":
|
|
198
|
+
headers["x-region"] = region.value
|
|
199
|
+
# Add region as query parameter
|
|
200
|
+
params = params.set("forceFunctionRegion", region.value)
|
|
201
|
+
|
|
202
|
+
body = invoke_options.get("body")
|
|
203
|
+
if headers.get("content-type", None) is None:
|
|
204
|
+
if isinstance(body, str):
|
|
205
|
+
headers["Content-Type"] = "text/plain"
|
|
206
|
+
elif isinstance(body, dict):
|
|
207
|
+
headers["Content-Type"] = "application/json"
|
|
208
|
+
elif isinstance(body, bytes):
|
|
209
|
+
headers["Content-Type"] = "application/octet-stream"
|
|
210
|
+
|
|
211
|
+
response = self._request(
|
|
212
|
+
method, [path], headers=headers, json=body, params=params
|
|
213
|
+
)
|
|
214
|
+
if response_type == "json":
|
|
215
|
+
data = response.json()
|
|
216
|
+
else:
|
|
217
|
+
data = response.content
|
|
218
|
+
return data
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING, TypedDict
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from httpx import Response
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class FunctionsApiErrorDict(TypedDict):
|
|
10
|
+
name: str
|
|
11
|
+
message: str
|
|
12
|
+
status: int
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FunctionsError(Exception):
|
|
16
|
+
def __init__(self, message: str, name: str, status: int) -> None:
|
|
17
|
+
super().__init__(message)
|
|
18
|
+
self.message = message
|
|
19
|
+
self.name = name
|
|
20
|
+
self.status = status
|
|
21
|
+
|
|
22
|
+
def to_dict(self) -> FunctionsApiErrorDict:
|
|
23
|
+
return {
|
|
24
|
+
"name": self.name,
|
|
25
|
+
"message": self.message,
|
|
26
|
+
"status": self.status,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class FunctionsHttpError(FunctionsError):
|
|
31
|
+
def __init__(self, message: str, code: int | None = None) -> None:
|
|
32
|
+
super().__init__(
|
|
33
|
+
message,
|
|
34
|
+
"FunctionsHttpError",
|
|
35
|
+
400 if code is None else code,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class FunctionsRelayError(FunctionsError):
|
|
40
|
+
"""Base exception for relay errors."""
|
|
41
|
+
|
|
42
|
+
def __init__(self, message: str, code: int | None = None) -> None:
|
|
43
|
+
super().__init__(
|
|
44
|
+
message,
|
|
45
|
+
"FunctionsRelayError",
|
|
46
|
+
400 if code is None else code,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _error_message_from(response: Response) -> str | None:
|
|
51
|
+
"""Best-effort extraction of an error message from an edge function response.
|
|
52
|
+
|
|
53
|
+
An edge function may reply with a plain-text or empty body, so a failed JSON
|
|
54
|
+
decode must not mask the underlying HTTP error.
|
|
55
|
+
"""
|
|
56
|
+
try:
|
|
57
|
+
body = response.json()
|
|
58
|
+
except ValueError:
|
|
59
|
+
return response.text or None
|
|
60
|
+
if isinstance(body, dict):
|
|
61
|
+
return body.get("error")
|
|
62
|
+
return response.text or None
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
from urllib.parse import urlparse
|
|
3
|
+
|
|
4
|
+
from httpx import AsyncClient as AsyncClient
|
|
5
|
+
|
|
6
|
+
DEFAULT_FUNCTION_CLIENT_TIMEOUT = 5
|
|
7
|
+
BASE64URL_REGEX = r"^([a-z0-9_-]{4})*($|[a-z0-9_-]{3}$|[a-z0-9_-]{2}$)$"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FunctionRegion(StrEnum):
|
|
11
|
+
Any = "any"
|
|
12
|
+
ApNortheast1 = "ap-northeast-1"
|
|
13
|
+
ApNortheast2 = "ap-northeast-2"
|
|
14
|
+
ApSouth1 = "ap-south-1"
|
|
15
|
+
ApSoutheast1 = "ap-southeast-1"
|
|
16
|
+
ApSoutheast2 = "ap-southeast-2"
|
|
17
|
+
CaCentral1 = "ca-central-1"
|
|
18
|
+
EuCentral1 = "eu-central-1"
|
|
19
|
+
EuWest1 = "eu-west-1"
|
|
20
|
+
EuWest2 = "eu-west-2"
|
|
21
|
+
EuWest3 = "eu-west-3"
|
|
22
|
+
SaEast1 = "sa-east-1"
|
|
23
|
+
UsEast1 = "us-east-1"
|
|
24
|
+
UsWest1 = "us-west-1"
|
|
25
|
+
UsWest2 = "us-west-2"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def is_valid_str_arg(target: str) -> bool:
|
|
29
|
+
return isinstance(target, str) and len(target.strip()) > 0
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def is_http_url(url: str) -> bool:
|
|
33
|
+
return urlparse(url).scheme in {"https", "http"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0" # {x-release-please-version}
|
corebase/lib/__init__.py
ADDED