Skip to content

Allow setting headers in PostgrestClient's constructor #10

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 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## CHANGELOG

### _Unreleased_

#### Added

- Allow setting headers in `PostgrestClient`'s constructor

### v0.4.0

#### Added
Expand Down
2 changes: 1 addition & 1 deletion postgrest_py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from postgrest_py.__version__ import __version__
from postgrest_py.client import Client, PostgrestClient
from postgrest_py.client import DEFAULT_POSTGREST_CLIENT_HEADERS, Client, PostgrestClient
24 changes: 16 additions & 8 deletions postgrest_py/client.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
from typing import Union
from typing import Dict, Union

from deprecation import deprecated
from httpx import AsyncClient, BasicAuth, Response

from postgrest_py.__version__ import __version__
from postgrest_py.request_builder import RequestBuilder

DEFAULT_POSTGREST_CLIENT_HEADERS: Dict[str, str] = {
"Accept": "application/json",
"Content-Type": "application/json",
}


class PostgrestClient:
"""PostgREST client."""

def __init__(self, base_url: str, *, schema="public") -> None:
def __init__(
self,
base_url: str,
*,
schema: str = "public",
headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS,
) -> None:
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
**headers,
"Accept-Profile": schema,
"Content-Profile": schema,
}
Expand All @@ -32,7 +42,7 @@ def auth(
self,
token: str,
*,
username: Union[str, bytes] = None,
username: Union[str, bytes, None] = None,
password: Union[str, bytes] = "",
):
"""Authenticate the client with either bearer token or basic authentication."""
Expand All @@ -44,9 +54,7 @@ def auth(

def schema(self, schema: str):
"""Switch to another schema."""
self.session.headers.update(
{"Accept-Profile": schema, "Content-Profile": schema}
)
self.session.headers.update({"Accept-Profile": schema, "Content-Profile": schema})
return self

def from_(self, table: str) -> RequestBuilder:
Expand Down