supabase_functions 2.33.0__tar.gz → 3.0.0a1__tar.gz
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.
- {supabase_functions-2.33.0 → supabase_functions-3.0.0a1}/PKG-INFO +6 -5
- {supabase_functions-2.33.0 → supabase_functions-3.0.0a1}/README.md +2 -2
- {supabase_functions-2.33.0 → supabase_functions-3.0.0a1}/pyproject.toml +35 -5
- supabase_functions-3.0.0a1/src/supabase_functions/__init__.py +11 -0
- supabase_functions-3.0.0a1/src/supabase_functions/client.py +198 -0
- {supabase_functions-2.33.0 → supabase_functions-3.0.0a1}/src/supabase_functions/errors.py +11 -18
- {supabase_functions-2.33.0 → supabase_functions-3.0.0a1}/src/supabase_functions/utils.py +0 -6
- supabase_functions-3.0.0a1/src/supabase_functions/version.py +1 -0
- supabase_functions-2.33.0/src/supabase_functions/__init__.py +0 -47
- supabase_functions-2.33.0/src/supabase_functions/_async/__init__.py +0 -1
- supabase_functions-2.33.0/src/supabase_functions/_async/functions_client.py +0 -183
- supabase_functions-2.33.0/src/supabase_functions/_sync/__init__.py +0 -1
- supabase_functions-2.33.0/src/supabase_functions/_sync/functions_client.py +0 -183
- supabase_functions-2.33.0/src/supabase_functions/version.py +0 -1
- {supabase_functions-2.33.0 → supabase_functions-3.0.0a1}/src/supabase_functions/py.typed +0 -0
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: supabase-functions
|
|
3
|
-
Version:
|
|
3
|
+
Version: 3.0.0a1
|
|
4
4
|
Summary: Library for Supabase Functions
|
|
5
5
|
Author: Joel Lee, Andrew Smith
|
|
6
6
|
Author-email: Joel Lee <joel@joellee.org>, Andrew Smith <a.smith@silentworks.co.uk>
|
|
7
7
|
License-Expression: MIT
|
|
8
|
-
Requires-Dist:
|
|
8
|
+
Requires-Dist: pydantic>=2.12.2
|
|
9
9
|
Requires-Dist: strenum>=0.4.15
|
|
10
|
+
Requires-Dist: supabase-utils==3.0.0a1
|
|
10
11
|
Requires-Dist: yarl>=1.20.1
|
|
11
12
|
Maintainer: Leonardo Santiago
|
|
12
13
|
Maintainer-email: Leonardo Santiago <leonardo.santiago@supabase.io>
|
|
13
|
-
Requires-Python: >=3.
|
|
14
|
+
Requires-Python: >=3.10
|
|
14
15
|
Project-URL: changelog, https://github.com/supabase/supabase-py/tree/main/CHANGELOG.md
|
|
15
16
|
Project-URL: homepage, https://github.com/supabase/supabase-py/tree/main/src/functions
|
|
16
17
|
Project-URL: repository, https://github.com/supabase/supabase-py
|
|
@@ -47,7 +48,7 @@ from supabase_functions import AsyncFunctionsClient
|
|
|
47
48
|
async def run_func():
|
|
48
49
|
# Initialize the client with your project URL and optional headers
|
|
49
50
|
headers = {
|
|
50
|
-
"Authorization": "Bearer your-
|
|
51
|
+
"Authorization": "Bearer your-anon-key",
|
|
51
52
|
# Add any other headers you might need
|
|
52
53
|
}
|
|
53
54
|
|
|
@@ -71,7 +72,7 @@ if __name__ == "__main__":
|
|
|
71
72
|
from supabase_functions import SyncFunctionsClient
|
|
72
73
|
|
|
73
74
|
# Initialize the client
|
|
74
|
-
headers = {"Authorization": "Bearer your-
|
|
75
|
+
headers = {"Authorization": "Bearer your-anon-key"}
|
|
75
76
|
fc = SyncFunctionsClient("https://<project_ref>.functions.supabase.co", headers)
|
|
76
77
|
|
|
77
78
|
# Invoke your Edge Function
|
|
@@ -29,7 +29,7 @@ from supabase_functions import AsyncFunctionsClient
|
|
|
29
29
|
async def run_func():
|
|
30
30
|
# Initialize the client with your project URL and optional headers
|
|
31
31
|
headers = {
|
|
32
|
-
"Authorization": "Bearer your-
|
|
32
|
+
"Authorization": "Bearer your-anon-key",
|
|
33
33
|
# Add any other headers you might need
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -53,7 +53,7 @@ if __name__ == "__main__":
|
|
|
53
53
|
from supabase_functions import SyncFunctionsClient
|
|
54
54
|
|
|
55
55
|
# Initialize the client
|
|
56
|
-
headers = {"Authorization": "Bearer your-
|
|
56
|
+
headers = {"Authorization": "Bearer your-anon-key"}
|
|
57
57
|
fc = SyncFunctionsClient("https://<project_ref>.functions.supabase.co", headers)
|
|
58
58
|
|
|
59
59
|
# Invoke your Edge Function
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "supabase_functions"
|
|
3
|
-
version = "
|
|
3
|
+
version = "3.0.0a1" # {x-release-please-version}
|
|
4
4
|
description = "Library for Supabase Functions"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Joel Lee", email = "joel@joellee.org" },
|
|
@@ -11,10 +11,11 @@ maintainers = [
|
|
|
11
11
|
]
|
|
12
12
|
license = "MIT"
|
|
13
13
|
readme = "README.md"
|
|
14
|
-
requires-python = ">=3.
|
|
14
|
+
requires-python = ">=3.10"
|
|
15
15
|
dependencies = [
|
|
16
|
-
"
|
|
16
|
+
"pydantic>=2.12.2",
|
|
17
17
|
"strenum >=0.4.15",
|
|
18
|
+
"supabase-utils==3.0.0a1", # x-release-please-version
|
|
18
19
|
"yarl>=1.20.1",
|
|
19
20
|
]
|
|
20
21
|
|
|
@@ -26,13 +27,13 @@ changelog = "https://github.com/supabase/supabase-py/tree/main/CHANGELOG.md"
|
|
|
26
27
|
|
|
27
28
|
[dependency-groups]
|
|
28
29
|
tests = [
|
|
29
|
-
"pyjwt>=2.
|
|
30
|
+
"pyjwt >=2.8.0",
|
|
30
31
|
"pytest >=7.4.2,<9.0.0",
|
|
31
32
|
"pytest-cov >=4,<7",
|
|
32
33
|
"pytest-asyncio >=0.21.1,<1.2.0",
|
|
34
|
+
"supabase_utils[all]",
|
|
33
35
|
]
|
|
34
36
|
lints = [
|
|
35
|
-
"unasync>=0.6.0",
|
|
36
37
|
"ruff >=0.12.1",
|
|
37
38
|
"python-lsp-server (>=1.12.2,<2.0.0)",
|
|
38
39
|
"pylsp-mypy (>=0.7.0,<0.8.0)",
|
|
@@ -43,6 +44,9 @@ dev = [{ include-group = "lints" }, {include-group = "tests" }]
|
|
|
43
44
|
[tool.uv]
|
|
44
45
|
default-groups = [ "dev" ]
|
|
45
46
|
|
|
47
|
+
[tool.uv.sources]
|
|
48
|
+
supabase-utils = { workspace = true }
|
|
49
|
+
|
|
46
50
|
[tool.pytest.ini_options]
|
|
47
51
|
asyncio_mode = "auto"
|
|
48
52
|
addopts = "tests"
|
|
@@ -50,6 +54,32 @@ filterwarnings = [
|
|
|
50
54
|
"ignore::DeprecationWarning", # ignore deprecation warnings globally
|
|
51
55
|
]
|
|
52
56
|
|
|
57
|
+
[tool.ruff.lint]
|
|
58
|
+
select = [
|
|
59
|
+
# pycodestyle
|
|
60
|
+
"E",
|
|
61
|
+
# Pyflakes
|
|
62
|
+
"F",
|
|
63
|
+
# pyupgrade
|
|
64
|
+
"UP",
|
|
65
|
+
# flake8-bugbear
|
|
66
|
+
# "B",
|
|
67
|
+
# flake8-simplify
|
|
68
|
+
# "SIM",
|
|
69
|
+
# isort
|
|
70
|
+
"I",
|
|
71
|
+
]
|
|
72
|
+
ignore = ["E712", "E501", "E402", "UP006", "UP035"]
|
|
73
|
+
|
|
53
74
|
[build-system]
|
|
54
75
|
requires = ["uv_build>=0.8.3,<0.9.0"]
|
|
55
76
|
build-backend = "uv_build"
|
|
77
|
+
|
|
78
|
+
[tool.mypy]
|
|
79
|
+
strict = true
|
|
80
|
+
allow_redefinition = true
|
|
81
|
+
|
|
82
|
+
no_warn_no_return = true
|
|
83
|
+
warn_return_any = true
|
|
84
|
+
warn_unused_configs = true
|
|
85
|
+
warn_redundant_casts = true
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .client import AsyncFunctionsClient, SyncFunctionsClient, create_client
|
|
4
|
+
from .utils import FunctionRegion
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"create_client",
|
|
8
|
+
"FunctionRegion",
|
|
9
|
+
"AsyncFunctionsClient",
|
|
10
|
+
"SyncFunctionsClient",
|
|
11
|
+
]
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import platform
|
|
2
|
+
from typing import Dict, Generic, Literal, overload
|
|
3
|
+
|
|
4
|
+
from httpx import AsyncClient, Client
|
|
5
|
+
from supabase_utils.http.adapters.httpx import AsyncHttpxSession, HttpxSession
|
|
6
|
+
from supabase_utils.http.headers import Headers
|
|
7
|
+
from supabase_utils.http.io import (
|
|
8
|
+
AsyncHttpIO,
|
|
9
|
+
HttpIO,
|
|
10
|
+
HttpMethod,
|
|
11
|
+
SyncHttpIO,
|
|
12
|
+
handle_http_io,
|
|
13
|
+
)
|
|
14
|
+
from supabase_utils.http.query import URLQuery
|
|
15
|
+
from supabase_utils.http.request import (
|
|
16
|
+
BytesRequest,
|
|
17
|
+
EmptyRequest,
|
|
18
|
+
HTTPRequestMethod,
|
|
19
|
+
JSONRequest,
|
|
20
|
+
Response,
|
|
21
|
+
TextRequest,
|
|
22
|
+
ToRequest,
|
|
23
|
+
)
|
|
24
|
+
from supabase_utils.types import JSON
|
|
25
|
+
from yarl import URL
|
|
26
|
+
|
|
27
|
+
from .errors import on_error_response
|
|
28
|
+
from .utils import (
|
|
29
|
+
FunctionRegion,
|
|
30
|
+
is_valid_str_arg,
|
|
31
|
+
)
|
|
32
|
+
from .version import __version__
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class FunctionsClient(Generic[HttpIO]):
|
|
36
|
+
def __init__(self, url: URL, headers: Dict[str, str], executor: HttpIO) -> None:
|
|
37
|
+
if not (url.scheme == "http" or url.scheme == "https"):
|
|
38
|
+
raise ValueError("url must be a valid HTTP URL string")
|
|
39
|
+
self.default_headers = Headers.from_mapping(
|
|
40
|
+
{
|
|
41
|
+
"X-Client-Info": f"supabase-py/supabase_functions v{__version__}",
|
|
42
|
+
"X-Supabase-Client-Platform": platform.system(),
|
|
43
|
+
"X-Supabase-Client-Platform-Version": platform.release(),
|
|
44
|
+
"X-Supabase-Client-Runtime": "python",
|
|
45
|
+
"X-Supabase-Client-Runtime-Version": platform.python_version(),
|
|
46
|
+
**headers,
|
|
47
|
+
}
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
self.executor: HttpIO = executor
|
|
51
|
+
self.base_url = url
|
|
52
|
+
|
|
53
|
+
def set_auth(self, token: str) -> None:
|
|
54
|
+
"""Updates the authorization header
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
token : str
|
|
59
|
+
the new jwt token sent in the authorization header
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
self.default_headers = self.default_headers.override(
|
|
63
|
+
"Authorization", f"Bearer {token}"
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def _invoke_options_to_request(
|
|
67
|
+
self,
|
|
68
|
+
function_name: str,
|
|
69
|
+
body: bytes | str | Dict[str, JSON] | None,
|
|
70
|
+
region: FunctionRegion | None,
|
|
71
|
+
headers: Dict[str, str] | None,
|
|
72
|
+
method: HTTPRequestMethod,
|
|
73
|
+
) -> ToRequest:
|
|
74
|
+
if not is_valid_str_arg(function_name):
|
|
75
|
+
raise ValueError("function_name must a valid string value.")
|
|
76
|
+
|
|
77
|
+
path = [function_name]
|
|
78
|
+
new_headers = Headers.from_mapping(headers) if headers else Headers.empty()
|
|
79
|
+
query_params = URLQuery.empty()
|
|
80
|
+
|
|
81
|
+
if region and region != FunctionRegion.Any:
|
|
82
|
+
new_headers = new_headers.set("x-region", region.value)
|
|
83
|
+
# Add region as query parameter
|
|
84
|
+
query_params = query_params.set("forceFunctionRegion", region.value)
|
|
85
|
+
if isinstance(body, str):
|
|
86
|
+
return TextRequest(
|
|
87
|
+
text=body,
|
|
88
|
+
method=method,
|
|
89
|
+
path=path,
|
|
90
|
+
headers=new_headers,
|
|
91
|
+
query=query_params,
|
|
92
|
+
)
|
|
93
|
+
elif isinstance(body, dict):
|
|
94
|
+
return JSONRequest(
|
|
95
|
+
body=body,
|
|
96
|
+
method=method,
|
|
97
|
+
path=path,
|
|
98
|
+
headers=new_headers,
|
|
99
|
+
query=query_params,
|
|
100
|
+
exclude_none=False,
|
|
101
|
+
)
|
|
102
|
+
elif isinstance(body, bytes):
|
|
103
|
+
return BytesRequest(
|
|
104
|
+
body=body,
|
|
105
|
+
method=method,
|
|
106
|
+
path=path,
|
|
107
|
+
headers=new_headers,
|
|
108
|
+
query=query_params,
|
|
109
|
+
)
|
|
110
|
+
else:
|
|
111
|
+
return EmptyRequest(
|
|
112
|
+
method=method, path=path, headers=new_headers, query=query_params
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
@handle_http_io
|
|
116
|
+
def invoke(
|
|
117
|
+
self,
|
|
118
|
+
function_name: str,
|
|
119
|
+
body: bytes | str | Dict[str, JSON] | None = None,
|
|
120
|
+
region: FunctionRegion | None = None,
|
|
121
|
+
headers: Dict[str, str] | None = None,
|
|
122
|
+
method: HTTPRequestMethod = "POST",
|
|
123
|
+
) -> HttpMethod[Response]:
|
|
124
|
+
"""Invokes a function
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
function_name : the name of the function to invoke
|
|
129
|
+
invoke_options : object with the following properties
|
|
130
|
+
`headers`: object representing the headers to send with the request
|
|
131
|
+
`body`: the body of the request
|
|
132
|
+
`responseType`: how the response should be parsed. The default is `json`
|
|
133
|
+
"""
|
|
134
|
+
response = yield self._invoke_options_to_request(
|
|
135
|
+
function_name, body, region, headers, method
|
|
136
|
+
)
|
|
137
|
+
if not response.is_success:
|
|
138
|
+
raise on_error_response(response)
|
|
139
|
+
return response
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class AsyncFunctionsClient(FunctionsClient[AsyncHttpIO]):
|
|
143
|
+
def __init__(
|
|
144
|
+
self,
|
|
145
|
+
url: str,
|
|
146
|
+
headers: Dict[str, str],
|
|
147
|
+
http_client: AsyncClient | None = None,
|
|
148
|
+
) -> None:
|
|
149
|
+
http_client = http_client or AsyncClient(
|
|
150
|
+
follow_redirects=True,
|
|
151
|
+
http2=True,
|
|
152
|
+
)
|
|
153
|
+
FunctionsClient.__init__(
|
|
154
|
+
self,
|
|
155
|
+
url=URL(url),
|
|
156
|
+
executor=AsyncHttpIO(session=AsyncHttpxSession(client=http_client)),
|
|
157
|
+
headers=headers,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class SyncFunctionsClient(FunctionsClient[SyncHttpIO]):
|
|
162
|
+
def __init__(
|
|
163
|
+
self,
|
|
164
|
+
url: str,
|
|
165
|
+
headers: Dict[str, str],
|
|
166
|
+
http_client: Client | None = None,
|
|
167
|
+
) -> None:
|
|
168
|
+
http_client = http_client or Client(
|
|
169
|
+
follow_redirects=True,
|
|
170
|
+
http2=True,
|
|
171
|
+
)
|
|
172
|
+
FunctionsClient.__init__(
|
|
173
|
+
self,
|
|
174
|
+
url=URL(url),
|
|
175
|
+
executor=SyncHttpIO(session=HttpxSession(client=http_client)),
|
|
176
|
+
headers=headers,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@overload
|
|
181
|
+
def create_client(
|
|
182
|
+
url: str, headers: dict[str, str], *, is_async: Literal[True]
|
|
183
|
+
) -> AsyncFunctionsClient: ...
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@overload
|
|
187
|
+
def create_client(
|
|
188
|
+
url: str, headers: dict[str, str], *, is_async: Literal[False]
|
|
189
|
+
) -> SyncFunctionsClient: ...
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def create_client(
|
|
193
|
+
url: str, headers: dict[str, str], *, is_async: bool
|
|
194
|
+
) -> AsyncFunctionsClient | SyncFunctionsClient:
|
|
195
|
+
if is_async:
|
|
196
|
+
return AsyncFunctionsClient(url, headers)
|
|
197
|
+
else:
|
|
198
|
+
return SyncFunctionsClient(url, headers)
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import
|
|
3
|
+
from typing import TypedDict
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
from httpx import Response
|
|
5
|
+
from supabase_utils.http.request import Response
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
class FunctionsApiErrorDict(TypedDict):
|
|
@@ -36,6 +35,15 @@ class FunctionsHttpError(FunctionsError):
|
|
|
36
35
|
)
|
|
37
36
|
|
|
38
37
|
|
|
38
|
+
def on_error_response(response: Response) -> FunctionsHttpError | FunctionsRelayError:
|
|
39
|
+
is_relay_error = response.headers.get("x-relay-header")
|
|
40
|
+
if is_relay_error == "true":
|
|
41
|
+
return FunctionsRelayError(
|
|
42
|
+
response.content.decode("utf-8"), code=response.status
|
|
43
|
+
)
|
|
44
|
+
return FunctionsHttpError(response.content.decode("utf-8"), response.status)
|
|
45
|
+
|
|
46
|
+
|
|
39
47
|
class FunctionsRelayError(FunctionsError):
|
|
40
48
|
"""Base exception for relay errors."""
|
|
41
49
|
|
|
@@ -45,18 +53,3 @@ class FunctionsRelayError(FunctionsError):
|
|
|
45
53
|
"FunctionsRelayError",
|
|
46
54
|
400 if code is None else code,
|
|
47
55
|
)
|
|
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
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import sys
|
|
2
|
-
from urllib.parse import urlparse
|
|
3
2
|
|
|
4
3
|
from httpx import AsyncClient as AsyncClient # noqa: F401
|
|
5
4
|
|
|
@@ -10,7 +9,6 @@ else:
|
|
|
10
9
|
|
|
11
10
|
|
|
12
11
|
DEFAULT_FUNCTION_CLIENT_TIMEOUT = 5
|
|
13
|
-
BASE64URL_REGEX = r"^([a-z0-9_-]{4})*($|[a-z0-9_-]{3}$|[a-z0-9_-]{2}$)$"
|
|
14
12
|
|
|
15
13
|
|
|
16
14
|
class FunctionRegion(StrEnum):
|
|
@@ -33,7 +31,3 @@ class FunctionRegion(StrEnum):
|
|
|
33
31
|
|
|
34
32
|
def is_valid_str_arg(target: str) -> bool:
|
|
35
33
|
return isinstance(target, str) and len(target.strip()) > 0
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def is_http_url(url: str) -> bool:
|
|
39
|
-
return urlparse(url).scheme in {"https", "http"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.28.3" # {x-release-please-version}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from typing import Literal, Union, 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
|
-
) -> Union[AsyncFunctionsClient, SyncFunctionsClient]:
|
|
44
|
-
if is_async:
|
|
45
|
-
return AsyncFunctionsClient(url, headers, verify=verify)
|
|
46
|
-
else:
|
|
47
|
-
return SyncFunctionsClient(url, headers, verify=verify)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import platform
|
|
2
|
-
import sys
|
|
3
|
-
from typing import Any, Dict, Literal, Optional, Union
|
|
4
|
-
from warnings import warn
|
|
5
|
-
|
|
6
|
-
from httpx import AsyncClient, Headers, HTTPError, QueryParams, 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
|
-
class AsyncFunctionsClient:
|
|
21
|
-
def __init__(
|
|
22
|
-
self,
|
|
23
|
-
url: str,
|
|
24
|
-
headers: Dict,
|
|
25
|
-
timeout: Optional[int] = None,
|
|
26
|
-
verify: Optional[bool] = None,
|
|
27
|
-
proxy: Optional[str] = None,
|
|
28
|
-
http_client: Optional[AsyncClient] = None,
|
|
29
|
-
) -> None:
|
|
30
|
-
if not is_http_url(url):
|
|
31
|
-
raise ValueError("url must be a valid HTTP URL string")
|
|
32
|
-
self.url = URL(url)
|
|
33
|
-
self.headers = Headers(
|
|
34
|
-
{
|
|
35
|
-
"X-Client-Info": (
|
|
36
|
-
f"supabase-py/supabase_functions v{__version__}"
|
|
37
|
-
f"; platform={platform.system()}"
|
|
38
|
-
f"; platform-version={platform.release()}"
|
|
39
|
-
f"; runtime=python"
|
|
40
|
-
f"; runtime-version={platform.python_version()}"
|
|
41
|
-
)
|
|
42
|
-
}
|
|
43
|
-
)
|
|
44
|
-
self.headers.update(headers)
|
|
45
|
-
|
|
46
|
-
if sys.version_info < (3, 10):
|
|
47
|
-
warn(
|
|
48
|
-
"Python versions below 3.10 are deprecated and will not be supported in future versions. Please upgrade to Python 3.10 or newer.",
|
|
49
|
-
DeprecationWarning,
|
|
50
|
-
stacklevel=2,
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
if timeout is not None:
|
|
54
|
-
warn(
|
|
55
|
-
"The 'timeout' parameter is deprecated. Please configure it in the http client instead.",
|
|
56
|
-
DeprecationWarning,
|
|
57
|
-
stacklevel=2,
|
|
58
|
-
)
|
|
59
|
-
if verify is not None:
|
|
60
|
-
warn(
|
|
61
|
-
"The 'verify' parameter is deprecated. Please configure it in the http client instead.",
|
|
62
|
-
DeprecationWarning,
|
|
63
|
-
stacklevel=2,
|
|
64
|
-
)
|
|
65
|
-
if proxy is not None:
|
|
66
|
-
warn(
|
|
67
|
-
"The 'proxy' parameter is deprecated. Please configure it in the http client instead.",
|
|
68
|
-
DeprecationWarning,
|
|
69
|
-
stacklevel=2,
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
self.verify = bool(verify) if verify is not None else True
|
|
73
|
-
self.timeout = int(abs(timeout)) if timeout is not None else 60
|
|
74
|
-
self._client = http_client or AsyncClient(
|
|
75
|
-
verify=self.verify,
|
|
76
|
-
timeout=self.timeout,
|
|
77
|
-
proxy=proxy,
|
|
78
|
-
follow_redirects=True,
|
|
79
|
-
http2=True,
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
async def _request(
|
|
83
|
-
self,
|
|
84
|
-
method: HTTPMethod,
|
|
85
|
-
path: list[str],
|
|
86
|
-
headers: Headers,
|
|
87
|
-
json: Optional[Union[Dict[Any, Any], str, bytes]] = None,
|
|
88
|
-
params: Optional[QueryParams] = None,
|
|
89
|
-
) -> Response:
|
|
90
|
-
url = self.url.joinpath(*path)
|
|
91
|
-
response = (
|
|
92
|
-
await self._client.request(
|
|
93
|
-
method, str(url), content=json, headers=headers, params=params
|
|
94
|
-
)
|
|
95
|
-
if isinstance(json, (str, bytes))
|
|
96
|
-
else await self._client.request(
|
|
97
|
-
method, str(url), json=json, headers=headers, params=params
|
|
98
|
-
)
|
|
99
|
-
)
|
|
100
|
-
# Relay failures set this header even on non-2xx responses.
|
|
101
|
-
if response.headers.get("x-relay-error") == "true":
|
|
102
|
-
raise FunctionsRelayError(response.text, response.status_code)
|
|
103
|
-
try:
|
|
104
|
-
response.raise_for_status()
|
|
105
|
-
except HTTPError as exc:
|
|
106
|
-
status_code = None
|
|
107
|
-
if hasattr(response, "status_code"):
|
|
108
|
-
status_code = response.status_code
|
|
109
|
-
|
|
110
|
-
raise FunctionsHttpError(
|
|
111
|
-
_error_message_from(response)
|
|
112
|
-
or f"An error occurred while requesting your edge function at {exc.request.url!r}.",
|
|
113
|
-
status_code,
|
|
114
|
-
) from exc
|
|
115
|
-
|
|
116
|
-
return response
|
|
117
|
-
|
|
118
|
-
def set_auth(self, token: str) -> None:
|
|
119
|
-
"""Updates the authorization header
|
|
120
|
-
|
|
121
|
-
Parameters
|
|
122
|
-
----------
|
|
123
|
-
token : str
|
|
124
|
-
the new jwt token sent in the authorization header
|
|
125
|
-
"""
|
|
126
|
-
|
|
127
|
-
self.headers["Authorization"] = f"Bearer {token}"
|
|
128
|
-
|
|
129
|
-
async def invoke(
|
|
130
|
-
self, function_name: str, invoke_options: Optional[Dict] = None
|
|
131
|
-
) -> Union[Dict, bytes]:
|
|
132
|
-
"""Invokes a function
|
|
133
|
-
|
|
134
|
-
Parameters
|
|
135
|
-
----------
|
|
136
|
-
function_name : the name of the function to invoke
|
|
137
|
-
invoke_options : object with the following properties
|
|
138
|
-
`headers`: object representing the headers to send with the request
|
|
139
|
-
`body`: the body of the request
|
|
140
|
-
`method`: the HTTP method to invoke the function with. The default is `POST`
|
|
141
|
-
`responseType`: how the response should be parsed. The default is `json`
|
|
142
|
-
"""
|
|
143
|
-
if not is_valid_str_arg(function_name):
|
|
144
|
-
raise ValueError("function_name must a valid string value.")
|
|
145
|
-
headers = Headers(self.headers)
|
|
146
|
-
params = QueryParams()
|
|
147
|
-
body = None
|
|
148
|
-
method: HTTPMethod = "POST"
|
|
149
|
-
response_type = "text/plain"
|
|
150
|
-
|
|
151
|
-
if invoke_options is not None:
|
|
152
|
-
headers.update(invoke_options.get("headers", {}))
|
|
153
|
-
method = invoke_options.get("method", "POST")
|
|
154
|
-
response_type = invoke_options.get("responseType", "text/plain")
|
|
155
|
-
|
|
156
|
-
region = invoke_options.get("region")
|
|
157
|
-
if region:
|
|
158
|
-
if not isinstance(region, FunctionRegion):
|
|
159
|
-
warn(f"Use FunctionRegion({region})", stacklevel=2)
|
|
160
|
-
region = FunctionRegion(region)
|
|
161
|
-
|
|
162
|
-
if region.value != "any":
|
|
163
|
-
headers["x-region"] = region.value
|
|
164
|
-
# Add region as query parameter
|
|
165
|
-
params = params.set("forceFunctionRegion", region.value)
|
|
166
|
-
|
|
167
|
-
body = invoke_options.get("body")
|
|
168
|
-
if headers.get("content-type", None) is None:
|
|
169
|
-
if isinstance(body, str):
|
|
170
|
-
headers["Content-Type"] = "text/plain"
|
|
171
|
-
elif isinstance(body, dict):
|
|
172
|
-
headers["Content-Type"] = "application/json"
|
|
173
|
-
elif isinstance(body, bytes):
|
|
174
|
-
headers["Content-Type"] = "application/octet-stream"
|
|
175
|
-
|
|
176
|
-
response = await self._request(
|
|
177
|
-
method, [function_name], headers=headers, json=body, params=params
|
|
178
|
-
)
|
|
179
|
-
if response_type == "json":
|
|
180
|
-
data = response.json()
|
|
181
|
-
else:
|
|
182
|
-
data = response.content
|
|
183
|
-
return data
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import platform
|
|
2
|
-
import sys
|
|
3
|
-
from typing import Any, Dict, Literal, Optional, Union
|
|
4
|
-
from warnings import warn
|
|
5
|
-
|
|
6
|
-
from httpx import Client, Headers, HTTPError, QueryParams, 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
|
-
class SyncFunctionsClient:
|
|
21
|
-
def __init__(
|
|
22
|
-
self,
|
|
23
|
-
url: str,
|
|
24
|
-
headers: Dict,
|
|
25
|
-
timeout: Optional[int] = None,
|
|
26
|
-
verify: Optional[bool] = None,
|
|
27
|
-
proxy: Optional[str] = None,
|
|
28
|
-
http_client: Optional[Client] = None,
|
|
29
|
-
) -> None:
|
|
30
|
-
if not is_http_url(url):
|
|
31
|
-
raise ValueError("url must be a valid HTTP URL string")
|
|
32
|
-
self.url = URL(url)
|
|
33
|
-
self.headers = Headers(
|
|
34
|
-
{
|
|
35
|
-
"X-Client-Info": (
|
|
36
|
-
f"supabase-py/supabase_functions v{__version__}"
|
|
37
|
-
f"; platform={platform.system()}"
|
|
38
|
-
f"; platform-version={platform.release()}"
|
|
39
|
-
f"; runtime=python"
|
|
40
|
-
f"; runtime-version={platform.python_version()}"
|
|
41
|
-
)
|
|
42
|
-
}
|
|
43
|
-
)
|
|
44
|
-
self.headers.update(headers)
|
|
45
|
-
|
|
46
|
-
if sys.version_info < (3, 10):
|
|
47
|
-
warn(
|
|
48
|
-
"Python versions below 3.10 are deprecated and will not be supported in future versions. Please upgrade to Python 3.10 or newer.",
|
|
49
|
-
DeprecationWarning,
|
|
50
|
-
stacklevel=2,
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
if timeout is not None:
|
|
54
|
-
warn(
|
|
55
|
-
"The 'timeout' parameter is deprecated. Please configure it in the http client instead.",
|
|
56
|
-
DeprecationWarning,
|
|
57
|
-
stacklevel=2,
|
|
58
|
-
)
|
|
59
|
-
if verify is not None:
|
|
60
|
-
warn(
|
|
61
|
-
"The 'verify' parameter is deprecated. Please configure it in the http client instead.",
|
|
62
|
-
DeprecationWarning,
|
|
63
|
-
stacklevel=2,
|
|
64
|
-
)
|
|
65
|
-
if proxy is not None:
|
|
66
|
-
warn(
|
|
67
|
-
"The 'proxy' parameter is deprecated. Please configure it in the http client instead.",
|
|
68
|
-
DeprecationWarning,
|
|
69
|
-
stacklevel=2,
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
self.verify = bool(verify) if verify is not None else True
|
|
73
|
-
self.timeout = int(abs(timeout)) if timeout is not None else 60
|
|
74
|
-
self._client = http_client or Client(
|
|
75
|
-
verify=self.verify,
|
|
76
|
-
timeout=self.timeout,
|
|
77
|
-
proxy=proxy,
|
|
78
|
-
follow_redirects=True,
|
|
79
|
-
http2=True,
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
def _request(
|
|
83
|
-
self,
|
|
84
|
-
method: HTTPMethod,
|
|
85
|
-
path: list[str],
|
|
86
|
-
headers: Headers,
|
|
87
|
-
json: Optional[Union[Dict[Any, Any], str, bytes]] = None,
|
|
88
|
-
params: Optional[QueryParams] = None,
|
|
89
|
-
) -> Response:
|
|
90
|
-
url = self.url.joinpath(*path)
|
|
91
|
-
response = (
|
|
92
|
-
self._client.request(
|
|
93
|
-
method, str(url), content=json, headers=headers, params=params
|
|
94
|
-
)
|
|
95
|
-
if isinstance(json, (str, bytes))
|
|
96
|
-
else self._client.request(
|
|
97
|
-
method, str(url), json=json, headers=headers, params=params
|
|
98
|
-
)
|
|
99
|
-
)
|
|
100
|
-
# Relay failures set this header even on non-2xx responses.
|
|
101
|
-
if response.headers.get("x-relay-error") == "true":
|
|
102
|
-
raise FunctionsRelayError(response.text, response.status_code)
|
|
103
|
-
try:
|
|
104
|
-
response.raise_for_status()
|
|
105
|
-
except HTTPError as exc:
|
|
106
|
-
status_code = None
|
|
107
|
-
if hasattr(response, "status_code"):
|
|
108
|
-
status_code = response.status_code
|
|
109
|
-
|
|
110
|
-
raise FunctionsHttpError(
|
|
111
|
-
_error_message_from(response)
|
|
112
|
-
or f"An error occurred while requesting your edge function at {exc.request.url!r}.",
|
|
113
|
-
status_code,
|
|
114
|
-
) from exc
|
|
115
|
-
|
|
116
|
-
return response
|
|
117
|
-
|
|
118
|
-
def set_auth(self, token: str) -> None:
|
|
119
|
-
"""Updates the authorization header
|
|
120
|
-
|
|
121
|
-
Parameters
|
|
122
|
-
----------
|
|
123
|
-
token : str
|
|
124
|
-
the new jwt token sent in the authorization header
|
|
125
|
-
"""
|
|
126
|
-
|
|
127
|
-
self.headers["Authorization"] = f"Bearer {token}"
|
|
128
|
-
|
|
129
|
-
def invoke(
|
|
130
|
-
self, function_name: str, invoke_options: Optional[Dict] = None
|
|
131
|
-
) -> Union[Dict, bytes]:
|
|
132
|
-
"""Invokes a function
|
|
133
|
-
|
|
134
|
-
Parameters
|
|
135
|
-
----------
|
|
136
|
-
function_name : the name of the function to invoke
|
|
137
|
-
invoke_options : object with the following properties
|
|
138
|
-
`headers`: object representing the headers to send with the request
|
|
139
|
-
`body`: the body of the request
|
|
140
|
-
`method`: the HTTP method to invoke the function with. The default is `POST`
|
|
141
|
-
`responseType`: how the response should be parsed. The default is `json`
|
|
142
|
-
"""
|
|
143
|
-
if not is_valid_str_arg(function_name):
|
|
144
|
-
raise ValueError("function_name must a valid string value.")
|
|
145
|
-
headers = Headers(self.headers)
|
|
146
|
-
params = QueryParams()
|
|
147
|
-
body = None
|
|
148
|
-
method: HTTPMethod = "POST"
|
|
149
|
-
response_type = "text/plain"
|
|
150
|
-
|
|
151
|
-
if invoke_options is not None:
|
|
152
|
-
headers.update(invoke_options.get("headers", {}))
|
|
153
|
-
method = invoke_options.get("method", "POST")
|
|
154
|
-
response_type = invoke_options.get("responseType", "text/plain")
|
|
155
|
-
|
|
156
|
-
region = invoke_options.get("region")
|
|
157
|
-
if region:
|
|
158
|
-
if not isinstance(region, FunctionRegion):
|
|
159
|
-
warn(f"Use FunctionRegion({region})", stacklevel=2)
|
|
160
|
-
region = FunctionRegion(region)
|
|
161
|
-
|
|
162
|
-
if region.value != "any":
|
|
163
|
-
headers["x-region"] = region.value
|
|
164
|
-
# Add region as query parameter
|
|
165
|
-
params = params.set("forceFunctionRegion", region.value)
|
|
166
|
-
|
|
167
|
-
body = invoke_options.get("body")
|
|
168
|
-
if headers.get("content-type", None) is None:
|
|
169
|
-
if isinstance(body, str):
|
|
170
|
-
headers["Content-Type"] = "text/plain"
|
|
171
|
-
elif isinstance(body, dict):
|
|
172
|
-
headers["Content-Type"] = "application/json"
|
|
173
|
-
elif isinstance(body, bytes):
|
|
174
|
-
headers["Content-Type"] = "application/octet-stream"
|
|
175
|
-
|
|
176
|
-
response = self._request(
|
|
177
|
-
method, [function_name], headers=headers, json=body, params=params
|
|
178
|
-
)
|
|
179
|
-
if response_type == "json":
|
|
180
|
-
data = response.json()
|
|
181
|
-
else:
|
|
182
|
-
data = response.content
|
|
183
|
-
return data
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "2.33.0" # {x-release-please-version}
|
|
File without changes
|