Skip to content

Commit

Permalink
Add-response-model (#64)
Browse files Browse the repository at this point in the history
* 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 <40204976+anand2312@users.noreply.github.com>

* 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 <joel@joellee.org>
Co-authored-by: Dani Reinón <dani@dribo.es>

* chore: modify ValueError with ValidationError

* chore: add "_" to internal methods

Co-authored-by: Anand <40204976+anand2312@users.noreply.github.com>
Co-authored-by: Lee Yi Jie Joel <lee.yi.jie.joel@gmail.com>
Co-authored-by: Joel Lee <joel@joellee.org>
  • Loading branch information
4 people committed Jan 28, 2022
1 parent e190621 commit 07ef4d4
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 49 deletions.
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
@@ -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
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
26 changes: 15 additions & 11 deletions postgrest_py/_async/request_builder.py
@@ -1,8 +1,11 @@
from __future__ import annotations

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

from pydantic import ValidationError

from ..base_request_builder import (
APIResponse,
BaseFilterRequestBuilder,
BaseSelectRequestBuilder,
CountMethod,
Expand All @@ -11,8 +14,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 +33,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 ValidationError 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 +58,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 +81,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 +94,6 @@ def insert(
) -> AsyncQueryRequestBuilder:
method, json = pre_insert(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -104,7 +111,6 @@ def upsert(
) -> AsyncQueryRequestBuilder:
method, json = pre_upsert(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -121,7 +127,6 @@ def update(
) -> AsyncFilterRequestBuilder:
method, json = pre_update(
self.session,
self.path,
json,
count=count,
returning=returning,
Expand All @@ -136,7 +141,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
@@ -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

0 comments on commit 07ef4d4

Please sign in to comment.