Skip to content

Commit 07ef4d4

Browse files
dreinonanand2312J0Joel Lee
authored
Add-response-model (#64)
* add poetry dependency * create APIResponse model * return APIResponse model in execute method * sort imports * mypy bug workaround (python/mypy#9319) * split logic, validate error existance and better type APIResponse * Implement APIError * add missing black config in pre-commit config * type APIError properties * fix: rm unused code and use returning param in update * refactor: reorder lines * chore: rebuild sync * chore: rebuild poetry.lock * fix: remove wrong parameter * chore: format * Chore: add missing return types Co-authored-by: Anand <[email protected]> * chore: replace builtin dict by Dict to support python < 3.9 * chore: update precommit hooks * chore: apply format * update return type in execute method * use relative import * add link to mypy issue * switch super init by class init to avoid future errors * chore: apply future annotations notation to return * chore: rebuild sync * tests: Add tests for response model (#74) * initial commit * tests: add fixtures for APIResponse * tests: [WIP] Test methods that don't interact with RequestResponse * tests: replace builtin type by typing type and add type annotations * tests: add requests Response fixtures * chore: change return order to improve readability * tests: add tests for left methods Co-authored-by: Joel Lee <[email protected]> Co-authored-by: Dani Reinón <[email protected]> * chore: modify ValueError with ValidationError * chore: add "_" to internal methods Co-authored-by: Anand <[email protected]> Co-authored-by: Lee Yi Jie Joel <[email protected]> Co-authored-by: Joel Lee <[email protected]>
1 parent e190621 commit 07ef4d4

10 files changed

+554
-49
lines changed

.pre-commit-config.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.0.1
3+
rev: v4.1.0
44
hooks:
55
- id: trailing-whitespace
66
- id: check-added-large-files
@@ -35,18 +35,19 @@ repos:
3535
]
3636

3737
- repo: https://github.com/ambv/black
38-
rev: 21.11b1
38+
rev: 21.12b0
3939
hooks:
4040
- id: black
41+
args: [--line-length, "90"]
4142

4243
- repo: https://github.com/asottile/pyupgrade
43-
rev: v2.29.1
44+
rev: v2.31.0
4445
hooks:
4546
- id: pyupgrade
4647
args: ["--py37-plus", "--keep-runtime-typing"]
4748

4849
- repo: https://github.com/commitizen-tools/commitizen
49-
rev: v2.20.0
50+
rev: v2.20.3
5051
hooks:
5152
- id: commitizen
5253
stages: [commit-msg]

poetry.lock

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

postgrest_py/_async/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def create_session(
4747
timeout=timeout,
4848
)
4949

50-
async def __aenter__(self) -> "AsyncPostgrestClient":
50+
async def __aenter__(self) -> AsyncPostgrestClient:
5151
return self
5252

5353
async def __aexit__(self, exc_type, exc, tb) -> None:

postgrest_py/_async/request_builder.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from __future__ import annotations
22

3-
from typing import Any, Optional, Tuple
3+
from typing import Optional
4+
5+
from pydantic import ValidationError
46

57
from ..base_request_builder import (
8+
APIResponse,
69
BaseFilterRequestBuilder,
710
BaseSelectRequestBuilder,
811
CountMethod,
@@ -11,8 +14,8 @@
1114
pre_select,
1215
pre_update,
1316
pre_upsert,
14-
process_response,
1517
)
18+
from ..exceptions import APIError
1619
from ..types import ReturnMethod
1720
from ..utils import AsyncClient
1821

@@ -30,16 +33,20 @@ def __init__(
3033
self.http_method = http_method
3134
self.json = json
3235

33-
async def execute(self) -> Tuple[Any, Optional[int]]:
36+
async def execute(self) -> APIResponse:
3437
r = await self.session.request(
3538
self.http_method,
3639
self.path,
3740
json=self.json,
3841
)
39-
return process_response(self.session, r)
42+
try:
43+
return APIResponse.from_http_request_response(r)
44+
except ValidationError as e:
45+
raise APIError(r.json()) from e
4046

4147

42-
class AsyncFilterRequestBuilder(BaseFilterRequestBuilder, AsyncQueryRequestBuilder):
48+
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
49+
class AsyncFilterRequestBuilder(BaseFilterRequestBuilder, AsyncQueryRequestBuilder): # type: ignore
4350
def __init__(
4451
self,
4552
session: AsyncClient,
@@ -51,7 +58,8 @@ def __init__(
5158
AsyncQueryRequestBuilder.__init__(self, session, path, http_method, json)
5259

5360

54-
class AsyncSelectRequestBuilder(BaseSelectRequestBuilder, AsyncQueryRequestBuilder):
61+
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
62+
class AsyncSelectRequestBuilder(BaseSelectRequestBuilder, AsyncQueryRequestBuilder): # type: ignore
5563
def __init__(
5664
self,
5765
session: AsyncClient,
@@ -73,7 +81,7 @@ def select(
7381
*columns: str,
7482
count: Optional[CountMethod] = None,
7583
) -> AsyncSelectRequestBuilder:
76-
method, json = pre_select(self.session, self.path, *columns, count=count)
84+
method, json = pre_select(self.session, *columns, count=count)
7785
return AsyncSelectRequestBuilder(self.session, self.path, method, json)
7886

7987
def insert(
@@ -86,7 +94,6 @@ def insert(
8694
) -> AsyncQueryRequestBuilder:
8795
method, json = pre_insert(
8896
self.session,
89-
self.path,
9097
json,
9198
count=count,
9299
returning=returning,
@@ -104,7 +111,6 @@ def upsert(
104111
) -> AsyncQueryRequestBuilder:
105112
method, json = pre_upsert(
106113
self.session,
107-
self.path,
108114
json,
109115
count=count,
110116
returning=returning,
@@ -121,7 +127,6 @@ def update(
121127
) -> AsyncFilterRequestBuilder:
122128
method, json = pre_update(
123129
self.session,
124-
self.path,
125130
json,
126131
count=count,
127132
returning=returning,
@@ -136,7 +141,6 @@ def delete(
136141
) -> AsyncFilterRequestBuilder:
137142
method, json = pre_delete(
138143
self.session,
139-
self.path,
140144
count=count,
141145
returning=returning,
142146
)

postgrest_py/_sync/request_builder.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

3-
from typing import Any, Optional, Tuple
3+
from typing import Optional
44

55
from ..base_request_builder import (
6+
APIResponse,
67
BaseFilterRequestBuilder,
78
BaseSelectRequestBuilder,
89
CountMethod,
@@ -11,8 +12,8 @@
1112
pre_select,
1213
pre_update,
1314
pre_upsert,
14-
process_response,
1515
)
16+
from ..exceptions import APIError
1617
from ..types import ReturnMethod
1718
from ..utils import SyncClient
1819

@@ -30,16 +31,20 @@ def __init__(
3031
self.http_method = http_method
3132
self.json = json
3233

33-
def execute(self) -> Tuple[Any, Optional[int]]:
34+
def execute(self) -> APIResponse:
3435
r = self.session.request(
3536
self.http_method,
3637
self.path,
3738
json=self.json,
3839
)
39-
return process_response(self.session, r)
40+
try:
41+
return APIResponse.from_http_request_response(r)
42+
except ValueError as e:
43+
raise APIError(r.json()) from e
4044

4145

42-
class SyncFilterRequestBuilder(BaseFilterRequestBuilder, SyncQueryRequestBuilder):
46+
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
47+
class SyncFilterRequestBuilder(BaseFilterRequestBuilder, SyncQueryRequestBuilder): # type: ignore
4348
def __init__(
4449
self,
4550
session: SyncClient,
@@ -51,7 +56,8 @@ def __init__(
5156
SyncQueryRequestBuilder.__init__(self, session, path, http_method, json)
5257

5358

54-
class SyncSelectRequestBuilder(BaseSelectRequestBuilder, SyncQueryRequestBuilder):
59+
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
60+
class SyncSelectRequestBuilder(BaseSelectRequestBuilder, SyncQueryRequestBuilder): # type: ignore
5561
def __init__(
5662
self,
5763
session: SyncClient,
@@ -73,7 +79,7 @@ def select(
7379
*columns: str,
7480
count: Optional[CountMethod] = None,
7581
) -> SyncSelectRequestBuilder:
76-
method, json = pre_select(self.session, self.path, *columns, count=count)
82+
method, json = pre_select(self.session, *columns, count=count)
7783
return SyncSelectRequestBuilder(self.session, self.path, method, json)
7884

7985
def insert(
@@ -86,7 +92,6 @@ def insert(
8692
) -> SyncQueryRequestBuilder:
8793
method, json = pre_insert(
8894
self.session,
89-
self.path,
9095
json,
9196
count=count,
9297
returning=returning,
@@ -104,7 +109,6 @@ def upsert(
104109
) -> SyncQueryRequestBuilder:
105110
method, json = pre_upsert(
106111
self.session,
107-
self.path,
108112
json,
109113
count=count,
110114
returning=returning,
@@ -121,7 +125,6 @@ def update(
121125
) -> SyncFilterRequestBuilder:
122126
method, json = pre_update(
123127
self.session,
124-
self.path,
125128
json,
126129
count=count,
127130
returning=returning,
@@ -136,7 +139,6 @@ def delete(
136139
) -> SyncFilterRequestBuilder:
137140
method, json = pre_delete(
138141
self.session,
139-
self.path,
140142
count=count,
141143
returning=returning,
142144
)

0 commit comments

Comments
 (0)