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
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import platform
|
|
4
|
+
from warnings import warn
|
|
5
|
+
|
|
6
|
+
from deprecation import deprecated
|
|
7
|
+
from httpx import Client, Headers, QueryParams, Timeout
|
|
8
|
+
from yarl import URL
|
|
9
|
+
|
|
10
|
+
from ..base_client import BasePostgrestClient
|
|
11
|
+
from ..constants import (
|
|
12
|
+
DEFAULT_POSTGREST_CLIENT_HEADERS,
|
|
13
|
+
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
|
|
14
|
+
)
|
|
15
|
+
from ..types import CountMethod
|
|
16
|
+
from ..utils import rpc_query_param
|
|
17
|
+
from ..version import __version__
|
|
18
|
+
from .request_builder import (
|
|
19
|
+
RequestConfig,
|
|
20
|
+
SyncRequestBuilder,
|
|
21
|
+
SyncRPCFilterRequestBuilder,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SyncPostgrestClient(BasePostgrestClient):
|
|
26
|
+
"""PostgREST client."""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
base_url: str,
|
|
31
|
+
*,
|
|
32
|
+
schema: str = "public",
|
|
33
|
+
headers: dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS,
|
|
34
|
+
timeout: int | float | Timeout | None = None,
|
|
35
|
+
verify: bool | None = None,
|
|
36
|
+
proxy: str | None = None,
|
|
37
|
+
http_client: Client | None = None,
|
|
38
|
+
retry_enabled: bool = True,
|
|
39
|
+
) -> None:
|
|
40
|
+
headers = {
|
|
41
|
+
"X-Client-Info": (
|
|
42
|
+
f"corebase-py/postgrest-py v{__version__}"
|
|
43
|
+
f"; platform={platform.system()}"
|
|
44
|
+
f"; platform-version={platform.release()}"
|
|
45
|
+
f"; runtime=python"
|
|
46
|
+
f"; runtime-version={platform.python_version()}"
|
|
47
|
+
),
|
|
48
|
+
**headers,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if timeout is not None:
|
|
52
|
+
warn(
|
|
53
|
+
"The 'timeout' parameter is deprecated. Please configure it in the http client instead.",
|
|
54
|
+
DeprecationWarning,
|
|
55
|
+
stacklevel=2,
|
|
56
|
+
)
|
|
57
|
+
if verify is not None:
|
|
58
|
+
warn(
|
|
59
|
+
"The 'verify' parameter is deprecated. Please configure it in the http client instead.",
|
|
60
|
+
DeprecationWarning,
|
|
61
|
+
stacklevel=2,
|
|
62
|
+
)
|
|
63
|
+
if proxy is not None:
|
|
64
|
+
warn(
|
|
65
|
+
"The 'proxy' parameter is deprecated. Please configure it in the http client instead.",
|
|
66
|
+
DeprecationWarning,
|
|
67
|
+
stacklevel=2,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
self.verify = bool(verify) if verify is not None else True
|
|
71
|
+
self.timeout = (
|
|
72
|
+
timeout
|
|
73
|
+
if isinstance(timeout, Timeout)
|
|
74
|
+
else (
|
|
75
|
+
int(abs(timeout))
|
|
76
|
+
if timeout is not None
|
|
77
|
+
else DEFAULT_POSTGREST_CLIENT_TIMEOUT
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
BasePostgrestClient.__init__(
|
|
81
|
+
self,
|
|
82
|
+
URL(base_url),
|
|
83
|
+
schema=schema,
|
|
84
|
+
headers=headers,
|
|
85
|
+
timeout=self.timeout,
|
|
86
|
+
verify=self.verify,
|
|
87
|
+
proxy=proxy,
|
|
88
|
+
)
|
|
89
|
+
self.retry_enabled = retry_enabled
|
|
90
|
+
|
|
91
|
+
self._owns_session = True
|
|
92
|
+
self.session = http_client or Client(
|
|
93
|
+
base_url=base_url,
|
|
94
|
+
headers=self.headers,
|
|
95
|
+
timeout=self.timeout,
|
|
96
|
+
verify=self.verify,
|
|
97
|
+
proxy=proxy,
|
|
98
|
+
follow_redirects=True,
|
|
99
|
+
http2=True,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
def schema(self, schema: str) -> SyncPostgrestClient:
|
|
103
|
+
"""Switch to another schema.
|
|
104
|
+
|
|
105
|
+
The returned client shares the HTTP session of the client it was
|
|
106
|
+
derived from. Only the client that owns the session closes it: closing
|
|
107
|
+
a client returned by `schema()` does nothing, and every client sharing
|
|
108
|
+
the session stops sending requests once the owner is closed.
|
|
109
|
+
"""
|
|
110
|
+
client = SyncPostgrestClient(
|
|
111
|
+
base_url=str(self.base_url),
|
|
112
|
+
schema=schema,
|
|
113
|
+
headers=dict(self.headers),
|
|
114
|
+
http_client=self.session,
|
|
115
|
+
retry_enabled=self.retry_enabled,
|
|
116
|
+
)
|
|
117
|
+
client.basic_auth = self.basic_auth
|
|
118
|
+
client._owns_session = False
|
|
119
|
+
return client
|
|
120
|
+
|
|
121
|
+
def __enter__(self) -> SyncPostgrestClient:
|
|
122
|
+
return self
|
|
123
|
+
|
|
124
|
+
def __exit__(self, exc_type, exc, tb) -> None:
|
|
125
|
+
self.aclose()
|
|
126
|
+
|
|
127
|
+
def aclose(self) -> None:
|
|
128
|
+
"""Close the underlying HTTP connections."""
|
|
129
|
+
if self._owns_session:
|
|
130
|
+
self.session.close()
|
|
131
|
+
|
|
132
|
+
def from_(self, table: str) -> SyncRequestBuilder:
|
|
133
|
+
"""Perform a table operation.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
table: The name of the table
|
|
137
|
+
Returns:
|
|
138
|
+
:class:`AsyncRequestBuilder`
|
|
139
|
+
"""
|
|
140
|
+
return SyncRequestBuilder(
|
|
141
|
+
self.session,
|
|
142
|
+
self.base_url.joinpath(table),
|
|
143
|
+
self.headers,
|
|
144
|
+
self.basic_auth,
|
|
145
|
+
retry_enabled=self.retry_enabled,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def table(self, table: str) -> SyncRequestBuilder:
|
|
149
|
+
"""Alias to :meth:`from_`."""
|
|
150
|
+
return self.from_(table)
|
|
151
|
+
|
|
152
|
+
@deprecated("0.2.0", "1.0.0", __version__, "Use self.from_() instead")
|
|
153
|
+
def from_table(self, table: str) -> SyncRequestBuilder:
|
|
154
|
+
"""Alias to :meth:`from_`."""
|
|
155
|
+
return self.from_(table)
|
|
156
|
+
|
|
157
|
+
def rpc(
|
|
158
|
+
self,
|
|
159
|
+
func: str,
|
|
160
|
+
params: dict[str, str],
|
|
161
|
+
count: CountMethod | None = None,
|
|
162
|
+
head: bool = False,
|
|
163
|
+
get: bool = False,
|
|
164
|
+
) -> SyncRPCFilterRequestBuilder:
|
|
165
|
+
"""Perform a stored procedure call.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
func: The name of the remote procedure to run.
|
|
169
|
+
params: The parameters to be passed to the remote procedure.
|
|
170
|
+
count: The method to use to get the count of rows returned.
|
|
171
|
+
head: When set to `true`, `data` will not be returned. Useful if you only need the count.
|
|
172
|
+
get: When set to `true`, the function will be called with read-only access mode.
|
|
173
|
+
Returns:
|
|
174
|
+
:class:`AsyncRPCFilterRequestBuilder`
|
|
175
|
+
Example:
|
|
176
|
+
.. code-block:: python
|
|
177
|
+
|
|
178
|
+
await client.rpc("foobar", {"arg": "value"}).execute()
|
|
179
|
+
|
|
180
|
+
.. versionchanged:: 0.10.9
|
|
181
|
+
This method now returns a :class:`AsyncRPCFilterRequestBuilder`.
|
|
182
|
+
.. versionchanged:: 0.10.2
|
|
183
|
+
This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to
|
|
184
|
+
filter on the RPC's resultset.
|
|
185
|
+
"""
|
|
186
|
+
method = "HEAD" if head else "GET" if get else "POST"
|
|
187
|
+
|
|
188
|
+
headers = Headers({"Prefer": f"count={count}"}) if count else Headers()
|
|
189
|
+
headers.update(self.headers)
|
|
190
|
+
# the params here are params to be sent to the RPC and not the queryparams!
|
|
191
|
+
json, http_params = (
|
|
192
|
+
({}, QueryParams({k: rpc_query_param(v) for k, v in params.items()}))
|
|
193
|
+
if method in ("HEAD", "GET")
|
|
194
|
+
else (params, QueryParams())
|
|
195
|
+
)
|
|
196
|
+
request = RequestConfig(
|
|
197
|
+
self.session,
|
|
198
|
+
self.base_url.joinpath("rpc", func),
|
|
199
|
+
method,
|
|
200
|
+
headers,
|
|
201
|
+
http_params,
|
|
202
|
+
self.basic_auth,
|
|
203
|
+
json,
|
|
204
|
+
retry_enabled=self.retry_enabled,
|
|
205
|
+
)
|
|
206
|
+
return SyncRPCFilterRequestBuilder(request)
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Literal, Self, TypeVar, overload
|
|
5
|
+
|
|
6
|
+
from httpx import (
|
|
7
|
+
BasicAuth,
|
|
8
|
+
Client,
|
|
9
|
+
Headers,
|
|
10
|
+
NetworkError,
|
|
11
|
+
RemoteProtocolError,
|
|
12
|
+
Response,
|
|
13
|
+
)
|
|
14
|
+
from pydantic import ValidationError
|
|
15
|
+
from yarl import URL
|
|
16
|
+
|
|
17
|
+
from ..base_request_builder import (
|
|
18
|
+
APIResponse,
|
|
19
|
+
BaseFilterRequestBuilder,
|
|
20
|
+
BaseRPCRequestBuilder,
|
|
21
|
+
BaseSelectRequestBuilder,
|
|
22
|
+
CountMethod,
|
|
23
|
+
RequestConfig,
|
|
24
|
+
SingleAPIResponse,
|
|
25
|
+
pre_delete,
|
|
26
|
+
pre_insert,
|
|
27
|
+
pre_select,
|
|
28
|
+
pre_update,
|
|
29
|
+
pre_upsert,
|
|
30
|
+
)
|
|
31
|
+
from ..exceptions import APIError, APIErrorFromJSON, generate_default_error_message
|
|
32
|
+
from ..types import RequestBody, ReturnMethod
|
|
33
|
+
|
|
34
|
+
ReqConfig = RequestConfig[Client]
|
|
35
|
+
QueryBuilderT = TypeVar("QueryBuilderT", bound="SyncQueryRequestBuilder")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_retry_delay(attempt_count: int) -> int:
|
|
39
|
+
delay: int = min(2**attempt_count, 30)
|
|
40
|
+
return delay
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def send_with_retry(req: ReqConfig) -> Response:
|
|
44
|
+
"""
|
|
45
|
+
Retries idempotent requests that failed due to a transient error.
|
|
46
|
+
Request method must be either "GET" or "HEAD", and the request must either
|
|
47
|
+
get a 503 or 520 response, or fail at the network level (connection failed,
|
|
48
|
+
reset or closed by the server, e.g. an HTTP/2 GOAWAY). Timeouts are not
|
|
49
|
+
retried.
|
|
50
|
+
"""
|
|
51
|
+
attempt_count = 0
|
|
52
|
+
while True:
|
|
53
|
+
headers = (
|
|
54
|
+
Headers({"X-Retry-Count": str(attempt_count)})
|
|
55
|
+
if attempt_count > 0
|
|
56
|
+
else Headers()
|
|
57
|
+
)
|
|
58
|
+
try:
|
|
59
|
+
resp = req.send(headers)
|
|
60
|
+
except (NetworkError, RemoteProtocolError):
|
|
61
|
+
if not req._can_retry(attempt_count):
|
|
62
|
+
raise
|
|
63
|
+
else:
|
|
64
|
+
if resp.is_success or not req.should_retry(
|
|
65
|
+
resp, attempt_count=attempt_count
|
|
66
|
+
):
|
|
67
|
+
return resp
|
|
68
|
+
time.sleep(get_retry_delay(attempt_count))
|
|
69
|
+
attempt_count += 1
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class SyncQueryRequestBuilder:
|
|
73
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
74
|
+
self.request = request
|
|
75
|
+
|
|
76
|
+
def select(self: QueryBuilderT, *columns: str) -> QueryBuilderT:
|
|
77
|
+
_, params, _, _ = pre_select(*columns, count=None)
|
|
78
|
+
self.request.params = self.request.params.add("select", params["select"])
|
|
79
|
+
if prefer_headers := self.request.headers.get_list("Prefer", split_commas=True):
|
|
80
|
+
prefer_headers = [h for h in prefer_headers if not h.startswith("return=")]
|
|
81
|
+
prefer_headers.append("return=representation")
|
|
82
|
+
self.request.headers["Prefer"] = ",".join(prefer_headers)
|
|
83
|
+
else:
|
|
84
|
+
self.request.headers["Prefer"] = "return=representation"
|
|
85
|
+
return self
|
|
86
|
+
|
|
87
|
+
def retry(self, enabled: bool) -> Self:
|
|
88
|
+
self.request.retry_enabled = enabled
|
|
89
|
+
return self
|
|
90
|
+
|
|
91
|
+
def execute(self) -> APIResponse:
|
|
92
|
+
"""Execute the query.
|
|
93
|
+
|
|
94
|
+
.. tip::
|
|
95
|
+
This is the last method called, after the query is built.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
:class:`APIResponse`
|
|
99
|
+
|
|
100
|
+
Raises:
|
|
101
|
+
:class:`APIError` If the API raised an error.
|
|
102
|
+
"""
|
|
103
|
+
r = send_with_retry(self.request)
|
|
104
|
+
try:
|
|
105
|
+
if r.is_success:
|
|
106
|
+
return APIResponse.from_http_request_response(r)
|
|
107
|
+
else:
|
|
108
|
+
json_obj = APIErrorFromJSON.model_validate_json(r.content)
|
|
109
|
+
raise APIError(dict(json_obj))
|
|
110
|
+
except ValidationError as e:
|
|
111
|
+
raise APIError(generate_default_error_message(r)) from e
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class SyncSingleRequestBuilder:
|
|
115
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
116
|
+
self.request = request
|
|
117
|
+
|
|
118
|
+
def retry(self, enabled: bool) -> Self:
|
|
119
|
+
self.request.retry_enabled = enabled
|
|
120
|
+
return self
|
|
121
|
+
|
|
122
|
+
def execute(self) -> SingleAPIResponse:
|
|
123
|
+
"""Execute the query.
|
|
124
|
+
|
|
125
|
+
.. tip::
|
|
126
|
+
This is the last method called, after the query is built.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
:class:`SingleAPIResponse`
|
|
130
|
+
na
|
|
131
|
+
Raises:
|
|
132
|
+
:class:`APIError` If the API raised an error.
|
|
133
|
+
"""
|
|
134
|
+
r = send_with_retry(self.request)
|
|
135
|
+
try:
|
|
136
|
+
if (
|
|
137
|
+
200 <= r.status_code <= 299
|
|
138
|
+
): # Response.ok from JS (https://developer.mozilla.org/en-US/docs/Web/API/Response/ok)
|
|
139
|
+
return SingleAPIResponse.from_http_request_response(r)
|
|
140
|
+
else:
|
|
141
|
+
json_obj = APIErrorFromJSON.model_validate_json(r.content)
|
|
142
|
+
raise APIError(dict(json_obj))
|
|
143
|
+
except ValidationError as e:
|
|
144
|
+
raise APIError(generate_default_error_message(r)) from e
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class SyncExplainRequestBuilder:
|
|
148
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
149
|
+
self.request = request
|
|
150
|
+
|
|
151
|
+
def retry(self, enabled: bool) -> Self:
|
|
152
|
+
self.request.retry_enabled = enabled
|
|
153
|
+
return self
|
|
154
|
+
|
|
155
|
+
def execute(self) -> str:
|
|
156
|
+
r = send_with_retry(self.request)
|
|
157
|
+
try:
|
|
158
|
+
if r.is_success:
|
|
159
|
+
return r.text
|
|
160
|
+
else:
|
|
161
|
+
json_obj = APIErrorFromJSON.model_validate_json(r.content)
|
|
162
|
+
raise APIError(dict(json_obj))
|
|
163
|
+
except ValidationError as e:
|
|
164
|
+
raise APIError(generate_default_error_message(r)) from e
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class SyncMaybeSingleRequestBuilder:
|
|
168
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
169
|
+
self.request = request
|
|
170
|
+
|
|
171
|
+
def retry(self, enabled: bool) -> Self:
|
|
172
|
+
self.request.retry_enabled = enabled
|
|
173
|
+
return self
|
|
174
|
+
|
|
175
|
+
def execute(self) -> SingleAPIResponse | None:
|
|
176
|
+
r = send_with_retry(self.request)
|
|
177
|
+
try:
|
|
178
|
+
if r.is_success:
|
|
179
|
+
parsed = APIResponse.from_http_request_response(r)
|
|
180
|
+
if len(parsed.data) == 0:
|
|
181
|
+
return None
|
|
182
|
+
if len(parsed.data) == 1:
|
|
183
|
+
return SingleAPIResponse(data=parsed.data[0], count=parsed.count)
|
|
184
|
+
else:
|
|
185
|
+
raise APIError(
|
|
186
|
+
{
|
|
187
|
+
"message": "Cannot coerce the result to a single JSON object",
|
|
188
|
+
"code": "406",
|
|
189
|
+
"hint": "Please check traceback of the code",
|
|
190
|
+
"details": "The result contains more than one row.",
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
else:
|
|
194
|
+
json_obj = APIErrorFromJSON.model_validate_json(r.content)
|
|
195
|
+
raise APIError(dict(json_obj))
|
|
196
|
+
except ValidationError as e:
|
|
197
|
+
raise APIError(generate_default_error_message(r)) from e
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class SyncFilterRequestBuilder(
|
|
201
|
+
BaseFilterRequestBuilder[Client], SyncQueryRequestBuilder
|
|
202
|
+
):
|
|
203
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
204
|
+
BaseFilterRequestBuilder.__init__(self, request)
|
|
205
|
+
SyncQueryRequestBuilder.__init__(self, request)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class SyncRPCFilterRequestBuilder(BaseRPCRequestBuilder, SyncSingleRequestBuilder):
|
|
209
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
210
|
+
BaseFilterRequestBuilder.__init__(self, request)
|
|
211
|
+
SyncSingleRequestBuilder.__init__(self, request)
|
|
212
|
+
|
|
213
|
+
def maybe_single(self) -> SyncMaybeSingleRequestBuilder:
|
|
214
|
+
"""Retrieves at most one row from the result. Result must be at most one row (e.g. using `eq` on a UNIQUE column), otherwise this will result in an error."""
|
|
215
|
+
return SyncMaybeSingleRequestBuilder(self.request)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
class SyncSelectRequestBuilder(
|
|
219
|
+
SyncQueryRequestBuilder, BaseSelectRequestBuilder[Client]
|
|
220
|
+
):
|
|
221
|
+
def __init__(self, request: ReqConfig) -> None:
|
|
222
|
+
BaseSelectRequestBuilder.__init__(self, request)
|
|
223
|
+
SyncQueryRequestBuilder.__init__(self, request)
|
|
224
|
+
|
|
225
|
+
def single(self) -> SyncSingleRequestBuilder:
|
|
226
|
+
"""Specify that the query will only return a single row in response.
|
|
227
|
+
|
|
228
|
+
.. caution::
|
|
229
|
+
The API will raise an error if the query returned more than one row.
|
|
230
|
+
"""
|
|
231
|
+
self.request.headers["Accept"] = "application/vnd.pgrst.object+json"
|
|
232
|
+
return SyncSingleRequestBuilder(self.request)
|
|
233
|
+
|
|
234
|
+
def maybe_single(self) -> SyncMaybeSingleRequestBuilder:
|
|
235
|
+
"""Retrieves at most one row from the result. Result must be at most one row (e.g. using `eq` on a UNIQUE column), otherwise this will result in an error."""
|
|
236
|
+
return SyncMaybeSingleRequestBuilder(self.request)
|
|
237
|
+
|
|
238
|
+
def text_search(
|
|
239
|
+
self, column: str, query: str, options: dict[str, Any] | None = None
|
|
240
|
+
) -> Self:
|
|
241
|
+
options = options or {}
|
|
242
|
+
type_ = options.get("type")
|
|
243
|
+
type_part = ""
|
|
244
|
+
if type_ == "plain":
|
|
245
|
+
type_part = "pl"
|
|
246
|
+
elif type_ == "phrase":
|
|
247
|
+
type_part = "ph"
|
|
248
|
+
elif type_ == "web_search":
|
|
249
|
+
type_part = "w"
|
|
250
|
+
config_part = f"({options.get('config')})" if options.get("config") else ""
|
|
251
|
+
self.filter(column, f"{type_part}fts{config_part}", query)
|
|
252
|
+
|
|
253
|
+
return self
|
|
254
|
+
|
|
255
|
+
def csv(self) -> SyncSingleRequestBuilder:
|
|
256
|
+
"""Specify that the query must retrieve data as a single CSV string."""
|
|
257
|
+
self.request._reject_strip_nulls("csv")
|
|
258
|
+
self.request.headers["Accept"] = "text/csv"
|
|
259
|
+
return SyncSingleRequestBuilder(self.request)
|
|
260
|
+
|
|
261
|
+
@overload
|
|
262
|
+
def explain(
|
|
263
|
+
self,
|
|
264
|
+
analyze: bool = False,
|
|
265
|
+
verbose: bool = False,
|
|
266
|
+
settings: bool = False,
|
|
267
|
+
buffers: bool = False,
|
|
268
|
+
wal: bool = False,
|
|
269
|
+
format: Literal["text"] = "text",
|
|
270
|
+
) -> SyncExplainRequestBuilder: ...
|
|
271
|
+
|
|
272
|
+
@overload
|
|
273
|
+
def explain(
|
|
274
|
+
self,
|
|
275
|
+
analyze: bool = False,
|
|
276
|
+
verbose: bool = False,
|
|
277
|
+
settings: bool = False,
|
|
278
|
+
buffers: bool = False,
|
|
279
|
+
wal: bool = False,
|
|
280
|
+
*,
|
|
281
|
+
format: Literal["json"],
|
|
282
|
+
) -> SyncSingleRequestBuilder: ...
|
|
283
|
+
|
|
284
|
+
def explain(
|
|
285
|
+
self,
|
|
286
|
+
analyze: bool = False,
|
|
287
|
+
verbose: bool = False,
|
|
288
|
+
settings: bool = False,
|
|
289
|
+
buffers: bool = False,
|
|
290
|
+
wal: bool = False,
|
|
291
|
+
format: Literal["text", "json"] = "text",
|
|
292
|
+
) -> SyncExplainRequestBuilder | SyncSingleRequestBuilder:
|
|
293
|
+
self.request._reject_strip_nulls("explain")
|
|
294
|
+
options = [
|
|
295
|
+
key
|
|
296
|
+
for key, value in locals().items()
|
|
297
|
+
if key not in ["self", "format"] and value
|
|
298
|
+
]
|
|
299
|
+
options_str = "|".join(options)
|
|
300
|
+
self.request.headers["Accept"] = (
|
|
301
|
+
f"application/vnd.pgrst.plan+{format}; options={options_str}"
|
|
302
|
+
)
|
|
303
|
+
if format == "text":
|
|
304
|
+
return SyncExplainRequestBuilder(self.request)
|
|
305
|
+
else:
|
|
306
|
+
return SyncSingleRequestBuilder(self.request)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
class SyncRequestBuilder: #
|
|
310
|
+
def __init__(
|
|
311
|
+
self,
|
|
312
|
+
session: Client,
|
|
313
|
+
path: URL,
|
|
314
|
+
headers: Headers,
|
|
315
|
+
auth: BasicAuth | None,
|
|
316
|
+
retry_enabled: bool = True,
|
|
317
|
+
) -> None:
|
|
318
|
+
self.session = session
|
|
319
|
+
self.path = path
|
|
320
|
+
self.headers = headers
|
|
321
|
+
self.auth = auth
|
|
322
|
+
self.retry_enabled = retry_enabled
|
|
323
|
+
|
|
324
|
+
def select(
|
|
325
|
+
self,
|
|
326
|
+
*columns: str,
|
|
327
|
+
count: CountMethod | None = None,
|
|
328
|
+
head: bool | None = None,
|
|
329
|
+
) -> SyncSelectRequestBuilder:
|
|
330
|
+
"""Run a SELECT query.
|
|
331
|
+
|
|
332
|
+
Args:
|
|
333
|
+
*columns: The names of the columns to fetch.
|
|
334
|
+
count: The method to use to get the count of rows returned.
|
|
335
|
+
Returns:
|
|
336
|
+
:class:`SyncSelectRequestBuilder`
|
|
337
|
+
"""
|
|
338
|
+
method, params, headers, json = pre_select(*columns, count=count, head=head)
|
|
339
|
+
headers.update(self.headers)
|
|
340
|
+
request = RequestConfig(
|
|
341
|
+
session=self.session,
|
|
342
|
+
path=self.path,
|
|
343
|
+
auth=self.auth,
|
|
344
|
+
params=params,
|
|
345
|
+
http_method=method,
|
|
346
|
+
headers=headers,
|
|
347
|
+
json=json,
|
|
348
|
+
retry_enabled=self.retry_enabled,
|
|
349
|
+
)
|
|
350
|
+
return SyncSelectRequestBuilder(request)
|
|
351
|
+
|
|
352
|
+
def insert(
|
|
353
|
+
self,
|
|
354
|
+
json: RequestBody,
|
|
355
|
+
*,
|
|
356
|
+
count: CountMethod | None = None,
|
|
357
|
+
returning: ReturnMethod = ReturnMethod.representation,
|
|
358
|
+
upsert: bool = False,
|
|
359
|
+
default_to_null: bool = True,
|
|
360
|
+
) -> SyncSelectRequestBuilder:
|
|
361
|
+
"""Run an INSERT query.
|
|
362
|
+
|
|
363
|
+
Args:
|
|
364
|
+
json: The row to be inserted.
|
|
365
|
+
count: The method to use to get the count of rows returned.
|
|
366
|
+
returning: Either 'minimal' or 'representation'
|
|
367
|
+
upsert: Whether the query should be an upsert.
|
|
368
|
+
default_to_null: Make missing fields default to `null`.
|
|
369
|
+
Otherwise, use the default value for the column.
|
|
370
|
+
Only applies for bulk inserts.
|
|
371
|
+
Returns:
|
|
372
|
+
:class:`SyncSelectRequestBuilder`
|
|
373
|
+
"""
|
|
374
|
+
method, params, headers, json = pre_insert(
|
|
375
|
+
json,
|
|
376
|
+
count=count,
|
|
377
|
+
returning=returning,
|
|
378
|
+
upsert=upsert,
|
|
379
|
+
default_to_null=default_to_null,
|
|
380
|
+
)
|
|
381
|
+
headers.update(self.headers)
|
|
382
|
+
request = RequestConfig(
|
|
383
|
+
session=self.session,
|
|
384
|
+
path=self.path,
|
|
385
|
+
auth=self.auth,
|
|
386
|
+
params=params,
|
|
387
|
+
http_method=method,
|
|
388
|
+
headers=headers,
|
|
389
|
+
json=json,
|
|
390
|
+
retry_enabled=self.retry_enabled,
|
|
391
|
+
)
|
|
392
|
+
return SyncSelectRequestBuilder(request)
|
|
393
|
+
|
|
394
|
+
def upsert(
|
|
395
|
+
self,
|
|
396
|
+
json: RequestBody,
|
|
397
|
+
*,
|
|
398
|
+
count: CountMethod | None = None,
|
|
399
|
+
returning: ReturnMethod = ReturnMethod.representation,
|
|
400
|
+
ignore_duplicates: bool = False,
|
|
401
|
+
on_conflict: str = "",
|
|
402
|
+
default_to_null: bool = True,
|
|
403
|
+
) -> SyncSelectRequestBuilder:
|
|
404
|
+
"""Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.
|
|
405
|
+
|
|
406
|
+
Args:
|
|
407
|
+
json: The row to be inserted.
|
|
408
|
+
count: The method to use to get the count of rows returned.
|
|
409
|
+
returning: Either 'minimal' or 'representation'
|
|
410
|
+
ignore_duplicates: Whether duplicate rows should be ignored.
|
|
411
|
+
on_conflict: Specified columns to be made to work with UNIQUE constraint.
|
|
412
|
+
default_to_null: Make missing fields default to `null`. Otherwise, use the
|
|
413
|
+
default value for the column. This only applies when inserting new rows,
|
|
414
|
+
not when merging with existing rows under `ignoreDuplicates: false`.
|
|
415
|
+
This also only applies when doing bulk upserts.
|
|
416
|
+
Returns:
|
|
417
|
+
:class:`SyncSelectRequestBuilder`
|
|
418
|
+
"""
|
|
419
|
+
method, params, headers, json = pre_upsert(
|
|
420
|
+
json,
|
|
421
|
+
count=count,
|
|
422
|
+
returning=returning,
|
|
423
|
+
ignore_duplicates=ignore_duplicates,
|
|
424
|
+
on_conflict=on_conflict,
|
|
425
|
+
default_to_null=default_to_null,
|
|
426
|
+
)
|
|
427
|
+
headers.update(self.headers)
|
|
428
|
+
request = RequestConfig(
|
|
429
|
+
session=self.session,
|
|
430
|
+
path=self.path,
|
|
431
|
+
auth=self.auth,
|
|
432
|
+
params=params,
|
|
433
|
+
http_method=method,
|
|
434
|
+
headers=headers,
|
|
435
|
+
json=json,
|
|
436
|
+
retry_enabled=self.retry_enabled,
|
|
437
|
+
)
|
|
438
|
+
return SyncSelectRequestBuilder(request)
|
|
439
|
+
|
|
440
|
+
def update(
|
|
441
|
+
self,
|
|
442
|
+
json: RequestBody,
|
|
443
|
+
*,
|
|
444
|
+
count: CountMethod | None = None,
|
|
445
|
+
returning: ReturnMethod = ReturnMethod.representation,
|
|
446
|
+
) -> SyncSelectRequestBuilder:
|
|
447
|
+
"""Run an UPDATE query.
|
|
448
|
+
|
|
449
|
+
Args:
|
|
450
|
+
json: The updated fields.
|
|
451
|
+
count: The method to use to get the count of rows returned.
|
|
452
|
+
returning: Either 'minimal' or 'representation'
|
|
453
|
+
Returns:
|
|
454
|
+
:class:`SyncSelectRequestBuilder`
|
|
455
|
+
"""
|
|
456
|
+
method, params, headers, json = pre_update(
|
|
457
|
+
json,
|
|
458
|
+
count=count,
|
|
459
|
+
returning=returning,
|
|
460
|
+
)
|
|
461
|
+
headers.update(self.headers)
|
|
462
|
+
request = RequestConfig(
|
|
463
|
+
session=self.session,
|
|
464
|
+
path=self.path,
|
|
465
|
+
auth=self.auth,
|
|
466
|
+
params=params,
|
|
467
|
+
http_method=method,
|
|
468
|
+
headers=headers,
|
|
469
|
+
json=json,
|
|
470
|
+
retry_enabled=self.retry_enabled,
|
|
471
|
+
)
|
|
472
|
+
return SyncSelectRequestBuilder(request)
|
|
473
|
+
|
|
474
|
+
def delete(
|
|
475
|
+
self,
|
|
476
|
+
*,
|
|
477
|
+
count: CountMethod | None = None,
|
|
478
|
+
returning: ReturnMethod = ReturnMethod.representation,
|
|
479
|
+
) -> SyncSelectRequestBuilder:
|
|
480
|
+
"""Run a DELETE query.
|
|
481
|
+
|
|
482
|
+
Args:
|
|
483
|
+
count: The method to use to get the count of rows returned.
|
|
484
|
+
returning: Either 'minimal' or 'representation'
|
|
485
|
+
Returns:
|
|
486
|
+
:class:`SyncSelectRequestBuilder`
|
|
487
|
+
"""
|
|
488
|
+
method, params, headers, json = pre_delete(
|
|
489
|
+
count=count,
|
|
490
|
+
returning=returning,
|
|
491
|
+
)
|
|
492
|
+
headers.update(self.headers)
|
|
493
|
+
request = RequestConfig(
|
|
494
|
+
session=self.session,
|
|
495
|
+
path=self.path,
|
|
496
|
+
auth=self.auth,
|
|
497
|
+
params=params,
|
|
498
|
+
http_method=method,
|
|
499
|
+
headers=headers,
|
|
500
|
+
json=json,
|
|
501
|
+
retry_enabled=self.retry_enabled,
|
|
502
|
+
)
|
|
503
|
+
return SyncSelectRequestBuilder(request)
|