-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathrequest_builder.py
359 lines (320 loc) · 11.1 KB
/
request_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from __future__ import annotations
from json import JSONDecodeError
from typing import Optional, Union
from httpx import Headers, QueryParams
from pydantic import ValidationError
from ..base_request_builder import (
APIResponse,
BaseFilterRequestBuilder,
BaseSelectRequestBuilder,
CountMethod,
SingleAPIResponse,
pre_delete,
pre_insert,
pre_select,
pre_update,
pre_upsert,
)
from ..exceptions import APIError, generate_default_error_message
from ..types import ReturnMethod
from ..utils import SyncClient
class SyncQueryRequestBuilder:
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
headers: Headers,
params: QueryParams,
json: dict,
) -> None:
self.session = session
self.path = path
self.http_method = http_method
self.headers = headers
self.params = params
self.json = json
def execute(self) -> APIResponse:
"""Execute the query.
.. tip::
This is the last method called, after the query is built.
Returns:
:class:`APIResponse`
Raises:
:class:`APIError` If the API raised an error.
"""
r = self.session.request(
self.http_method,
self.path,
json=self.json,
params=self.params,
headers=self.headers,
)
try:
if (
200 <= r.status_code <= 299
): # Response.ok from JS (https://developer.mozilla.org/en-US/docs/Web/API/Response/ok)
return APIResponse.from_http_request_response(r)
else:
raise APIError(r.json())
except ValidationError as e:
raise APIError(r.json()) from e
except JSONDecodeError as e:
raise APIError(generate_default_error_message(r))
class SyncSingleRequestBuilder:
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
headers: Headers,
params: QueryParams,
json: dict,
) -> None:
self.session = session
self.path = path
self.http_method = http_method
self.headers = headers
self.params = params
self.json = json
def execute(self) -> SingleAPIResponse:
"""Execute the query.
.. tip::
This is the last method called, after the query is built.
Returns:
:class:`SingleAPIResponse`
Raises:
:class:`APIError` If the API raised an error.
"""
r = self.session.request(
self.http_method,
self.path,
json=self.json,
params=self.params,
headers=self.headers,
)
try:
if (
200 <= r.status_code <= 299
): # Response.ok from JS (https://developer.mozilla.org/en-US/docs/Web/API/Response/ok)
return SingleAPIResponse.from_http_request_response(r)
else:
raise APIError(r.json())
except ValidationError as e:
raise APIError(r.json()) from e
except JSONDecodeError as e:
raise APIError(generate_default_error_message(r))
class SyncMaybeSingleRequestBuilder(SyncSingleRequestBuilder):
def execute(self) -> Optional[SingleAPIResponse]:
r = None
try:
r = super().execute()
except APIError as e:
if e.details and "The result contains 0 rows" in e.details:
return None
if not r:
raise APIError(
{
"message": "Missing response",
"code": "204",
"hint": "Please check traceback of the code",
"details": "Postgrest couldn't retrieve response, please check traceback of the code. Please create an issue in `supabase-community/postgrest-py` if needed.",
}
)
return r
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
class SyncFilterRequestBuilder(BaseFilterRequestBuilder, SyncQueryRequestBuilder): # type: ignore
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
headers: Headers,
params: QueryParams,
json: dict,
) -> None:
BaseFilterRequestBuilder.__init__(self, session, headers, params)
SyncQueryRequestBuilder.__init__(
self, session, path, http_method, headers, params, json
)
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
class SyncSelectRequestBuilder(BaseSelectRequestBuilder, SyncQueryRequestBuilder): # type: ignore
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
headers: Headers,
params: QueryParams,
json: dict,
) -> None:
BaseSelectRequestBuilder.__init__(self, session, headers, params)
SyncQueryRequestBuilder.__init__(
self, session, path, http_method, headers, params, json
)
def single(self) -> SyncSingleRequestBuilder:
"""Specify that the query will only return a single row in response.
.. caution::
The API will raise an error if the query returned more than one row.
"""
self.headers["Accept"] = "application/vnd.pgrst.object+json"
return SyncSingleRequestBuilder(
headers=self.headers,
http_method=self.http_method,
json=self.json,
params=self.params,
path=self.path,
session=self.session, # type: ignore
)
def maybe_single(self) -> SyncMaybeSingleRequestBuilder:
"""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."""
self.headers["Accept"] = "application/vnd.pgrst.object+json"
return SyncMaybeSingleRequestBuilder(
headers=self.headers,
http_method=self.http_method,
json=self.json,
params=self.params,
path=self.path,
session=self.session, # type: ignore
)
def text_search(
self, column: str, query: str, options: Dict[str, any] = {}
) -> SyncFilterRequestBuilder:
type_ = options.get("type")
type_part = ""
if type_ == "plain":
type_part = "pl"
elif type_ == "phrase":
type_part = "ph"
elif type_ == "web_search":
type_part = "w"
config_part = f"({options.get('config')})" if options.get("config") else ""
self.params = self.params.add(column, f"{type_part}fts{config_part}.{query}")
return SyncQueryRequestBuilder(
headers=self.headers,
http_method=self.http_method,
json=self.json,
params=self.params,
path=self.path,
session=self.session, # type: ignore
)
class SyncRequestBuilder:
def __init__(self, session: SyncClient, path: str) -> None:
self.session = session
self.path = path
def select(
self,
*columns: str,
count: Optional[CountMethod] = None,
) -> SyncSelectRequestBuilder:
"""Run a SELECT query.
Args:
*columns: The names of the columns to fetch.
count: The method to use to get the count of rows returned.
Returns:
:class:`AsyncSelectRequestBuilder`
"""
method, params, headers, json = pre_select(*columns, count=count)
return SyncSelectRequestBuilder(
self.session, self.path, method, headers, params, json
)
def insert(
self,
json: Union[dict, list],
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
upsert: bool = False,
) -> SyncQueryRequestBuilder:
"""Run an INSERT query.
Args:
json: The row to be inserted.
count: The method to use to get the count of rows returned.
returning: Either 'minimal' or 'representation'
upsert: Whether the query should be an upsert.
Returns:
:class:`AsyncQueryRequestBuilder`
"""
method, params, headers, json = pre_insert(
json,
count=count,
returning=returning,
upsert=upsert,
)
return SyncQueryRequestBuilder(
self.session, self.path, method, headers, params, json
)
def upsert(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
ignore_duplicates: bool = False,
on_conflict: str = "",
) -> SyncQueryRequestBuilder:
"""Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.
Args:
json: The row to be inserted.
count: The method to use to get the count of rows returned.
returning: Either 'minimal' or 'representation'
ignore_duplicates: Whether duplicate rows should be ignored.
on_conflict: Specified columns to be made to work with UNIQUE constraint.
Returns:
:class:`AsyncQueryRequestBuilder`
"""
method, params, headers, json = pre_upsert(
json,
count=count,
returning=returning,
ignore_duplicates=ignore_duplicates,
on_conflict=on_conflict,
)
return SyncQueryRequestBuilder(
self.session, self.path, method, headers, params, json
)
def update(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
) -> SyncFilterRequestBuilder:
"""Run an UPDATE query.
Args:
json: The updated fields.
count: The method to use to get the count of rows returned.
returning: Either 'minimal' or 'representation'
Returns:
:class:`AsyncFilterRequestBuilder`
"""
method, params, headers, json = pre_update(
json,
count=count,
returning=returning,
)
return SyncFilterRequestBuilder(
self.session, self.path, method, headers, params, json
)
def delete(
self,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
) -> SyncFilterRequestBuilder:
"""Run a DELETE query.
Args:
count: The method to use to get the count of rows returned.
returning: Either 'minimal' or 'representation'
Returns:
:class:`AsyncFilterRequestBuilder`
"""
method, params, headers, json = pre_delete(
count=count,
returning=returning,
)
return SyncFilterRequestBuilder(
self.session, self.path, method, headers, params, json
)
def stub(self):
return None