Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement async sync with
unasync-cli
(#30)
Co-authored-by: Dani Reinón <dani@dribo.es>
- Loading branch information
Showing
23 changed files
with
676 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
from postgrest_py.__version__ import __version__ | ||
from postgrest_py.client import DEFAULT_POSTGREST_CLIENT_HEADERS, Client, PostgrestClient | ||
from postgrest_py._async.client import AsyncPostgrestClient # noqa: F401 | ||
from postgrest_py._async.request_builder import AsyncFilterRequestBuilder # noqa: F401 | ||
from postgrest_py._async.request_builder import AsyncQueryRequestBuilder # noqa: F401 | ||
from postgrest_py._async.request_builder import AsyncRequestBuilder # noqa: F401 | ||
from postgrest_py._async.request_builder import AsyncSelectRequestBuilder # noqa: F401 | ||
from postgrest_py._sync.client import SyncPostgrestClient # noqa: F401 | ||
from postgrest_py._sync.request_builder import SyncFilterRequestBuilder # noqa: F401 | ||
from postgrest_py._sync.request_builder import SyncQueryRequestBuilder # noqa: F401 | ||
from postgrest_py._sync.request_builder import SyncRequestBuilder # noqa: F401 | ||
from postgrest_py._sync.request_builder import SyncSelectRequestBuilder # noqa: F401 | ||
from postgrest_py.config import DEFAULT_POSTGREST_CLIENT_HEADERS # noqa: F401 | ||
from postgrest_py.deprecated_client import Client, PostgrestClient # noqa: F401 | ||
from postgrest_py.deprecated_get_request_builder import GetRequestBuilder # noqa: F401 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from typing import Dict, Optional, Union | ||
|
||
from deprecation import deprecated | ||
from httpx import BasicAuth, Response | ||
|
||
from postgrest_py.__version__ import __version__ | ||
from postgrest_py.config import DEFAULT_POSTGREST_CLIENT_HEADERS | ||
from postgrest_py.utils import SyncClient | ||
|
||
from .request_builder import SyncRequestBuilder | ||
|
||
|
||
class SyncPostgrestClient: | ||
"""PostgREST client.""" | ||
|
||
def __init__( | ||
self, | ||
base_url: str, | ||
*, | ||
schema: str = "public", | ||
headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS, | ||
) -> None: | ||
headers = { | ||
**headers, | ||
"Accept-Profile": schema, | ||
"Content-Profile": schema, | ||
} | ||
self.session = SyncClient(base_url=base_url, headers=headers) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc, tb) -> None: | ||
self.aclose() | ||
|
||
def aclose(self) -> None: | ||
self.session.aclose() | ||
|
||
def auth( | ||
self, | ||
token: Optional[str], | ||
*, | ||
username: Union[str, bytes, None] = None, | ||
password: Union[str, bytes] = "", | ||
): | ||
""" | ||
Authenticate the client with either bearer token or basic authentication. | ||
Raise `ValueError` if neither authentication scheme is provided. | ||
Bearer token is preferred if both ones are provided. | ||
""" | ||
if token: | ||
self.session.headers["Authorization"] = f"Bearer {token}" | ||
elif username: | ||
self.session.auth = BasicAuth(username, password) | ||
else: | ||
raise ValueError( | ||
"Neither bearer token or basic authentication scheme is provided" | ||
) | ||
return self | ||
|
||
def schema(self, schema: str): | ||
"""Switch to another schema.""" | ||
self.session.headers.update({"Accept-Profile": schema, "Content-Profile": schema}) | ||
return self | ||
|
||
def from_(self, table: str) -> SyncRequestBuilder: | ||
"""Perform a table operation.""" | ||
return SyncRequestBuilder(self.session, f"/{table}") | ||
|
||
@deprecated("0.2.0", "1.0.0", __version__, "Use PostgrestClient.from_() instead") | ||
def from_table(self, table: str) -> SyncRequestBuilder: | ||
"""Alias to Self.from_().""" | ||
return self.from_(table) | ||
|
||
def rpc(self, func: str, params: dict) -> Response: | ||
"""Perform a stored procedure call.""" | ||
path = f"/rpc/{func}" | ||
r = self.session.post(path, json=params) | ||
return r |
Oops, something went wrong.