forked from supabase/postgrest-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_builder.py
271 lines (238 loc) · 8.01 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
from __future__ import annotations
from typing import Any, Dict, Generic, Optional, Type, TypeVar, Union
from pydantic import BaseModel
from ..base_request_builder import (
APIResponse,
BaseFilterRequestBuilder,
BaseSelectRequestBuilder,
CountMethod,
pre_delete,
pre_insert,
pre_select,
pre_update,
pre_upsert,
)
from ..exceptions import APIError
from ..types import ReturnMethod
from ..utils import SyncClient
ModelType = TypeVar("ModelType", bound=Union[BaseModel, Dict[str, Any]])
class SyncQueryRequestBuilder:
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
json: dict,
) -> None:
self.session = session
self.path = path
self.http_method = http_method
self.json = json
def execute(
self, *, model: Type[ModelType] = Dict[str, Any]
) -> APIResponse[ModelType]:
r = self.session.request(
self.http_method,
self.path,
json=self.json,
)
try:
return APIResponse[model].from_http_request_response(r)
except ValueError as e:
raise APIError(r.json()) from e
# 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,
json: dict,
) -> None:
BaseFilterRequestBuilder.__init__(self, session)
SyncQueryRequestBuilder.__init__(self, session, path, http_method, 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,
json: dict,
) -> None:
BaseSelectRequestBuilder.__init__(self, session)
SyncQueryRequestBuilder.__init__(self, session, path, http_method, json)
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:
method, json = pre_select(self.session, *columns, count=count)
return SyncSelectRequestBuilder(self.session, self.path, method, json)
def insert(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
upsert=False,
) -> SyncQueryRequestBuilder:
method, json = pre_insert(
self.session,
json,
count=count,
returning=returning,
upsert=upsert,
)
return SyncQueryRequestBuilder(self.session, self.path, method, json)
def upsert(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
ignore_duplicates=False,
) -> SyncQueryRequestBuilder:
method, json = pre_upsert(
self.session,
json,
count=count,
returning=returning,
ignore_duplicates=ignore_duplicates,
)
return SyncQueryRequestBuilder(self.session, self.path, method, json)
def update(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
) -> SyncFilterRequestBuilder:
method, json = pre_update(
self.session,
json,
count=count,
returning=returning,
)
return SyncFilterRequestBuilder(self.session, self.path, method, json)
def delete(
self,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
) -> SyncFilterRequestBuilder:
method, json = pre_delete(
self.session,
count=count,
returning=returning,
)
return SyncFilterRequestBuilder(self.session, self.path, method, json)
_T = TypeVar("_T", bound=BaseModel)
# The following are replicas of the normal classes above
# Their only purpose is to ensure type-safety and provide good
# editor autocompletion, when using pydantic BaseModels with queries.
class _SyncModelQueryRequestBuilder(Generic[_T], SyncQueryRequestBuilder):
def execute(self) -> APIResponse[_T]:
# super().execute(model=_T) has NOT been used here
# as pyright was raising errors for it.
r = self.session.request(
self.http_method,
self.path,
json=self.json,
)
try:
return APIResponse[_T].from_http_request_response(r)
except ValueError as e:
raise APIError(r.json()) from e
class _SyncModelFilterRequestBuilder(BaseFilterRequestBuilder, _SyncModelQueryRequestBuilder[_T]): # type: ignore
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
json: dict,
) -> None:
BaseFilterRequestBuilder.__init__(self, session)
_SyncModelQueryRequestBuilder[_T].__init__(self, session, path, http_method, json)
class _SyncModelSelectRequestBuilder(
BaseSelectRequestBuilder, _SyncModelQueryRequestBuilder[_T]
):
def __init__(
self,
session: SyncClient,
path: str,
http_method: str,
json: dict,
) -> None:
BaseSelectRequestBuilder.__init__(self, session)
_SyncModelQueryRequestBuilder[_T].__init__(self, session, path, http_method, json)
class _SyncModelRequestBuilder(Generic[_T], SyncRequestBuilder):
def select(
self,
*columns: str,
count: Optional[CountMethod] = None,
) -> _SyncModelSelectRequestBuilder[_T]:
method, json = pre_select(self.session, *columns, count=count)
return _SyncModelSelectRequestBuilder[_T](self.session, self.path, method, json)
def insert(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
upsert: bool = False,
) -> _SyncModelQueryRequestBuilder[_T]:
method, json = pre_insert(
self.session,
json,
count=count,
returning=returning,
upsert=upsert,
)
return _SyncModelQueryRequestBuilder[_T](self.session, self.path, method, json)
def upsert(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
ignore_duplicates: bool = False,
) -> _SyncModelQueryRequestBuilder[_T]:
method, json = pre_upsert(
self.session,
json,
count=count,
returning=returning,
ignore_duplicates=ignore_duplicates,
)
return _SyncModelQueryRequestBuilder[_T](self.session, self.path, method, json)
def update(
self,
json: dict,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
) -> _SyncModelFilterRequestBuilder[_T]:
method, json = pre_update(
self.session,
json,
count=count,
returning=returning,
)
return _SyncModelFilterRequestBuilder[_T](self.session, self.path, method, json)
def delete(
self,
*,
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
) -> _SyncModelFilterRequestBuilder[_T]:
method, json = pre_delete(
self.session,
count=count,
returning=returning,
)
return _SyncModelFilterRequestBuilder[_T](self.session, self.path, method, json)