Skip to content

feat: add-response-model #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c99d7fb
add poetry dependency
Jan 3, 2022
e911222
create APIResponse model
Jan 3, 2022
315a303
return APIResponse model in execute method
Jan 3, 2022
e6310c5
sort imports
Jan 3, 2022
790811f
mypy bug workaround (https://github.com/python/mypy/issues/9319)
Jan 4, 2022
54df5b2
split logic, validate error existance and better type APIResponse
Jan 4, 2022
c6a3f25
Implement APIError
Jan 4, 2022
df86617
add missing black config in pre-commit config
Jan 4, 2022
3708992
type APIError properties
Jan 4, 2022
032a221
fix: rm unused code and use returning param in update
Jan 4, 2022
425a1c6
refactor: reorder lines
Jan 4, 2022
1972db1
chore: Merge branch 'master' into add-response-model
Jan 4, 2022
d0638b4
chore: rebuild sync
Jan 4, 2022
b2eb3ac
chore: rebuild poetry.lock
Jan 4, 2022
bddf023
fix: remove wrong parameter
Jan 4, 2022
920b3e2
chore: format
Jan 4, 2022
8a7cce5
Chore: add missing return types
dreinon Jan 4, 2022
6dbd5cb
chore: replace builtin dict by Dict to support python < 3.9
Jan 4, 2022
c569b2a
chore: update precommit hooks
Jan 4, 2022
aea8dc4
chore: apply format
Jan 4, 2022
afdd725
update return type in execute method
Jan 9, 2022
6c9448c
use relative import
Jan 9, 2022
2ecbeca
add link to mypy issue
Jan 9, 2022
3df240b
switch super init by class init to avoid future errors
Jan 9, 2022
2a806b2
chore: apply future annotations notation to return
Jan 13, 2022
54ade70
chore: rebuild sync
Jan 13, 2022
2df9d14
tests: Add tests for response model (#74)
J0 Jan 27, 2022
4da9fe4
Merge branch 'master' into add-response-model
Jan 27, 2022
cfe8e38
chore: modify ValueError with ValidationError
Jan 27, 2022
11399b9
chore: add "_" to internal methods
Jan 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
Expand Down Expand Up @@ -35,18 +35,19 @@ repos:
]

- repo: https://github.com/ambv/black
rev: 21.11b1
rev: 21.12b0
hooks:
- id: black
args: [--line-length, "90"]

- repo: https://github.com/asottile/pyupgrade
rev: v2.29.1
rev: v2.31.0
hooks:
- id: pyupgrade
args: ["--py37-plus", "--keep-runtime-typing"]

- repo: https://github.com/commitizen-tools/commitizen
rev: v2.20.0
rev: v2.20.3
hooks:
- id: commitizen
stages: [commit-msg]
52 changes: 52 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion postgrest_py/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create_session(
timeout=timeout,
)

async def __aenter__(self) -> "AsyncPostgrestClient":
async def __aenter__(self) -> AsyncPostgrestClient:
return self

async def __aexit__(self, exc_type, exc, tb) -> None:
Expand Down
24 changes: 13 additions & 11 deletions postgrest_py/_async/request_builder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from typing import Any, Optional, Tuple
from typing import Optional

from ..base_request_builder import (
APIResponse,
BaseFilterRequestBuilder,
BaseSelectRequestBuilder,
CountMethod,
Expand All @@ -11,8 +12,8 @@
pre_select,
pre_update,
pre_upsert,
process_response,
)
from ..exceptions import APIError
from ..types import ReturnMethod
from ..utils import AsyncClient

Expand All @@ -30,16 +31,20 @@ def __init__(
self.http_method = http_method
self.json = json

async def execute(self) -> Tuple[Any, Optional[int]]:
async def execute(self) -> APIResponse:
r = await self.session.request(
self.http_method,
self.path,
json=self.json,
)
return process_response(self.session, r)
try:
return APIResponse.from_http_request_response(r)
except ValueError as e:
raise APIError(r.json()) from e


class AsyncFilterRequestBuilder(BaseFilterRequestBuilder, AsyncQueryRequestBuilder):
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
class AsyncFilterRequestBuilder(BaseFilterRequestBuilder, AsyncQueryRequestBuilder): # type: ignore
def __init__(
self,
session: AsyncClient,
Expand All @@ -51,7 +56,8 @@ def __init__(
AsyncQueryRequestBuilder.__init__(self, session, path, http_method, json)


class AsyncSelectRequestBuilder(BaseSelectRequestBuilder, AsyncQueryRequestBuilder):
# ignoring type checking as a workaround for https://github.com/python/mypy/issues/9319
class AsyncSelectRequestBuilder(BaseSelectRequestBuilder, AsyncQueryRequestBuilder): # type: ignore
def __init__(
self,
session: AsyncClient,
Expand All @@ -73,7 +79,7 @@ def select(
*columns: str,
count: Optional[CountMethod] = None,
) -> AsyncSelectRequestBuilder:
method, json = pre_select(self.session, self.path, *columns, count=count)
method, json = pre_select(self.session, *columns, count=count)
return AsyncSelectRequestBuilder(self.session, self.path, method, json)

def insert(
Expand All @@ -86,7 +92,6 @@ def insert(
) -> AsyncQueryRequestBuilder:
method, json = pre_insert(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -104,7 +109,6 @@ def upsert(
) -> AsyncQueryRequestBuilder:
method, json = pre_upsert(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -121,7 +125,6 @@ def update(
) -> AsyncFilterRequestBuilder:
method, json = pre_update(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -136,7 +139,6 @@ def delete(
) -> AsyncFilterRequestBuilder:
method, json = pre_delete(
self.session,
self.path,
count=count,
returning=returning,
)
Expand Down
24 changes: 13 additions & 11 deletions postgrest_py/_sync/request_builder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from typing import Any, Optional, Tuple
from typing import Optional

from ..base_request_builder import (
APIResponse,
BaseFilterRequestBuilder,
BaseSelectRequestBuilder,
CountMethod,
Expand All @@ -11,8 +12,8 @@
pre_select,
pre_update,
pre_upsert,
process_response,
)
from ..exceptions import APIError
from ..types import ReturnMethod
from ..utils import SyncClient

Expand All @@ -30,16 +31,20 @@ def __init__(
self.http_method = http_method
self.json = json

def execute(self) -> Tuple[Any, Optional[int]]:
def execute(self) -> APIResponse:
r = self.session.request(
self.http_method,
self.path,
json=self.json,
)
return process_response(self.session, r)
try:
return APIResponse.from_http_request_response(r)
except ValueError as e:
raise APIError(r.json()) from e


class SyncFilterRequestBuilder(BaseFilterRequestBuilder, SyncQueryRequestBuilder):
# 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,
Expand All @@ -51,7 +56,8 @@ def __init__(
SyncQueryRequestBuilder.__init__(self, session, path, http_method, json)


class SyncSelectRequestBuilder(BaseSelectRequestBuilder, SyncQueryRequestBuilder):
# 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,
Expand All @@ -73,7 +79,7 @@ def select(
*columns: str,
count: Optional[CountMethod] = None,
) -> SyncSelectRequestBuilder:
method, json = pre_select(self.session, self.path, *columns, count=count)
method, json = pre_select(self.session, *columns, count=count)
return SyncSelectRequestBuilder(self.session, self.path, method, json)

def insert(
Expand All @@ -86,7 +92,6 @@ def insert(
) -> SyncQueryRequestBuilder:
method, json = pre_insert(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -104,7 +109,6 @@ def upsert(
) -> SyncQueryRequestBuilder:
method, json = pre_upsert(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -121,7 +125,6 @@ def update(
) -> SyncFilterRequestBuilder:
method, json = pre_update(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -136,7 +139,6 @@ def delete(
) -> SyncFilterRequestBuilder:
method, json = pre_delete(
self.session,
self.path,
count=count,
returning=returning,
)
Expand Down
Loading