Skip to content

Commit 315f596

Browse files
leynierdreinonDani Reinón
authored
perf: use inheritance to improve our code base (#47)
* fix: interpolations erros and other things reported by sourcery-ai * feat: use inheritance to improve the code base * fix: sourcery refactored * chore: update pre commit rules * fix: remove noqa F401 comments * fix: remove duplicate and unused imports in base_client.py * feat: use enum instance literals in base_request_builder.py * pref: cast session only once in __init__ * pref: remove unnecesary cast * tests: update tests * chore: generate sync code * feat: add support for upsert * Rm cast from rpc in async client * Rm cast from rpc in sync client * Add table method as an alias for from_ Co-authored-by: dreinon <[email protected]> Co-authored-by: Dani Reinón <[email protected]>
1 parent 3934fb2 commit 315f596

13 files changed

+692
-530
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ dmypy.json
130130

131131
# PyCharm
132132
.idea/
133+
134+
# Visual Studio Code
135+
.vscode/

.pre-commit-config.yaml

+48-33
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,52 @@
11
repos:
2-
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.0.1
4-
hooks:
5-
- id: trailing-whitespace
6-
- id: check-added-large-files
7-
- id: end-of-file-fixer
8-
- id: mixed-line-ending
9-
args: ["--fix=lf"]
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.0.1
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: check-added-large-files
7+
- id: end-of-file-fixer
8+
- id: mixed-line-ending
9+
args: ["--fix=lf"]
1010

11-
- repo: https://github.com/pre-commit/mirrors-isort
12-
rev: v5.8.0
13-
hooks:
14-
- id: isort
15-
args:
16-
[
17-
"--multi-line=3",
18-
"--trailing-comma",
19-
"--force-grid-wrap=0",
20-
"--use-parentheses",
21-
"--line-width=88",
22-
]
11+
- repo: https://github.com/pycqa/isort
12+
rev: 5.10.1
13+
hooks:
14+
- id: isort
15+
args:
16+
[
17+
"--profile",
18+
"black",
19+
"--multi-line=3",
20+
"--trailing-comma",
21+
"--force-grid-wrap=0",
22+
"--use-parentheses",
23+
"--line-width=88",
24+
]
2325

24-
- repo: https://github.com/humitos/mirrors-autoflake.git
25-
rev: v1.1
26-
hooks:
27-
- id: autoflake
28-
args: ["--in-place", "--remove-all-unused-imports"]
26+
- repo: https://github.com/myint/autoflake.git
27+
rev: v1.4
28+
hooks:
29+
- id: autoflake
30+
args:
31+
[
32+
"--in-place",
33+
"--remove-all-unused-imports",
34+
"--ignore-init-module-imports",
35+
]
2936

30-
- repo: https://github.com/psf/black
31-
rev: 21.9b0
32-
hooks:
33-
- id: black
34-
- repo: https://github.com/pre-commit/mirrors-prettier
35-
rev: v2.4.1
36-
hooks:
37-
- id: prettier
37+
- repo: https://github.com/ambv/black
38+
rev: 21.11b1
39+
hooks:
40+
- id: black
41+
42+
- repo: https://github.com/asottile/pyupgrade
43+
rev: v2.29.1
44+
hooks:
45+
- id: pyupgrade
46+
args: ["--py37-plus", "--keep-runtime-typing"]
47+
48+
- repo: https://github.com/commitizen-tools/commitizen
49+
rev: v2.20.0
50+
hooks:
51+
- id: commitizen
52+
stages: [commit-msg]

postgrest_py/__init__.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
from postgrest_py._async.client import AsyncPostgrestClient # noqa: F401
2-
from postgrest_py._async.request_builder import AsyncFilterRequestBuilder # noqa: F401
3-
from postgrest_py._async.request_builder import AsyncQueryRequestBuilder # noqa: F401
4-
from postgrest_py._async.request_builder import AsyncRequestBuilder # noqa: F401
5-
from postgrest_py._async.request_builder import AsyncSelectRequestBuilder # noqa: F401
6-
from postgrest_py._sync.client import SyncPostgrestClient # noqa: F401
7-
from postgrest_py._sync.request_builder import SyncFilterRequestBuilder # noqa: F401
8-
from postgrest_py._sync.request_builder import SyncQueryRequestBuilder # noqa: F401
9-
from postgrest_py._sync.request_builder import SyncRequestBuilder # noqa: F401
10-
from postgrest_py._sync.request_builder import SyncSelectRequestBuilder # noqa: F401
11-
from postgrest_py.config import DEFAULT_POSTGREST_CLIENT_HEADERS # noqa: F401
12-
from postgrest_py.deprecated_client import Client, PostgrestClient # noqa: F401
13-
from postgrest_py.deprecated_get_request_builder import GetRequestBuilder # noqa: F401
1+
from postgrest_py._async.client import AsyncPostgrestClient
2+
from postgrest_py._async.request_builder import (
3+
AsyncFilterRequestBuilder,
4+
AsyncQueryRequestBuilder,
5+
AsyncRequestBuilder,
6+
AsyncSelectRequestBuilder,
7+
)
8+
from postgrest_py._sync.client import SyncPostgrestClient
9+
from postgrest_py._sync.request_builder import (
10+
SyncFilterRequestBuilder,
11+
SyncQueryRequestBuilder,
12+
SyncRequestBuilder,
13+
SyncSelectRequestBuilder,
14+
)
15+
from postgrest_py.base_client import DEFAULT_POSTGREST_CLIENT_HEADERS
16+
from postgrest_py.deprecated_client import Client, PostgrestClient
17+
from postgrest_py.deprecated_get_request_builder import GetRequestBuilder

postgrest_py/_async/client.py

+23-41
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
from typing import Dict, Optional, Union
1+
from typing import Dict, cast
22

33
from deprecation import deprecated
4-
from httpx import BasicAuth, Response
4+
from httpx import Response
55

66
from postgrest_py.__version__ import __version__
7-
from postgrest_py.config import DEFAULT_POSTGREST_CLIENT_HEADERS
7+
from postgrest_py.base_client import (
8+
DEFAULT_POSTGREST_CLIENT_HEADERS,
9+
BasePostgrestClient,
10+
)
811
from postgrest_py.utils import AsyncClient
912

1013
from .request_builder import AsyncRequestBuilder
1114

1215

13-
class AsyncPostgrestClient:
16+
class AsyncPostgrestClient(BasePostgrestClient):
1417
"""PostgREST client."""
1518

1619
def __init__(
@@ -20,14 +23,17 @@ def __init__(
2023
schema: str = "public",
2124
headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS,
2225
) -> None:
23-
headers = {
24-
**headers,
25-
"Accept-Profile": schema,
26-
"Content-Profile": schema,
27-
}
28-
self.session = AsyncClient(base_url=base_url, headers=headers)
26+
BasePostgrestClient.__init__(self, base_url, schema=schema, headers=headers)
27+
self.session = cast(AsyncClient, self.session)
2928

30-
async def __aenter__(self):
29+
def create_session(
30+
self,
31+
base_url: str,
32+
headers: Dict[str, str],
33+
) -> AsyncClient:
34+
return AsyncClient(base_url=base_url, headers=headers)
35+
36+
async def __aenter__(self) -> "AsyncPostgrestClient":
3137
return self
3238

3339
async def __aexit__(self, exc_type, exc, tb) -> None:
@@ -36,41 +42,17 @@ async def __aexit__(self, exc_type, exc, tb) -> None:
3642
async def aclose(self) -> None:
3743
await self.session.aclose()
3844

39-
def auth(
40-
self,
41-
token: Optional[str],
42-
*,
43-
username: Union[str, bytes, None] = None,
44-
password: Union[str, bytes] = "",
45-
):
46-
"""
47-
Authenticate the client with either bearer token or basic authentication.
48-
49-
Raise `ValueError` if neither authentication scheme is provided.
50-
Bearer token is preferred if both ones are provided.
51-
"""
52-
if token:
53-
self.session.headers["Authorization"] = f"Bearer {token}"
54-
elif username:
55-
self.session.auth = BasicAuth(username, password)
56-
else:
57-
raise ValueError(
58-
"Neither bearer token or basic authentication scheme is provided"
59-
)
60-
return self
61-
62-
def schema(self, schema: str):
63-
"""Switch to another schema."""
64-
self.session.headers.update({"Accept-Profile": schema, "Content-Profile": schema})
65-
return self
66-
6745
def from_(self, table: str) -> AsyncRequestBuilder:
6846
"""Perform a table operation."""
6947
return AsyncRequestBuilder(self.session, f"/{table}")
48+
49+
def table(self, table: str) -> AsyncRequestBuilder:
50+
"""Alias to self.from_()."""
51+
return self.from_(table)
7052

71-
@deprecated("0.2.0", "1.0.0", __version__, "Use PostgrestClient.from_() instead")
53+
@deprecated("0.2.0", "1.0.0", __version__, "Use self.from_() instead")
7254
def from_table(self, table: str) -> AsyncRequestBuilder:
73-
"""Alias to Self.from_()."""
55+
"""Alias to self.from_()."""
7456
return self.from_(table)
7557

7658
async def rpc(self, func: str, params: dict) -> Response:

0 commit comments

Comments
 (0)