Skip to content

Commit 1737e69

Browse files
authored
Allow setting headers in PostgrestClient's constructor (#10)
1 parent 3ffb7fb commit 1737e69

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## CHANGELOG
22

3+
### _Unreleased_
4+
5+
#### Added
6+
7+
- Allow setting headers in `PostgrestClient`'s constructor
8+
39
### v0.4.0
410

511
#### Added

postgrest_py/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from postgrest_py.__version__ import __version__
2-
from postgrest_py.client import Client, PostgrestClient
2+
from postgrest_py.client import DEFAULT_POSTGREST_CLIENT_HEADERS, Client, PostgrestClient

postgrest_py/client.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
from typing import Union
1+
from typing import Dict, Union
22

33
from deprecation import deprecated
44
from httpx import AsyncClient, BasicAuth, Response
55

66
from postgrest_py.__version__ import __version__
77
from postgrest_py.request_builder import RequestBuilder
88

9+
DEFAULT_POSTGREST_CLIENT_HEADERS: Dict[str, str] = {
10+
"Accept": "application/json",
11+
"Content-Type": "application/json",
12+
}
13+
914

1015
class PostgrestClient:
1116
"""PostgREST client."""
1217

13-
def __init__(self, base_url: str, *, schema="public") -> None:
18+
def __init__(
19+
self,
20+
base_url: str,
21+
*,
22+
schema: str = "public",
23+
headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS,
24+
) -> None:
1425
headers = {
15-
"Accept": "application/json",
16-
"Content-Type": "application/json",
26+
**headers,
1727
"Accept-Profile": schema,
1828
"Content-Profile": schema,
1929
}
@@ -32,7 +42,7 @@ def auth(
3242
self,
3343
token: str,
3444
*,
35-
username: Union[str, bytes] = None,
45+
username: Union[str, bytes, None] = None,
3646
password: Union[str, bytes] = "",
3747
):
3848
"""Authenticate the client with either bearer token or basic authentication."""
@@ -44,9 +54,7 @@ def auth(
4454

4555
def schema(self, schema: str):
4656
"""Switch to another schema."""
47-
self.session.headers.update(
48-
{"Accept-Profile": schema, "Content-Profile": schema}
49-
)
57+
self.session.headers.update({"Accept-Profile": schema, "Content-Profile": schema})
5058
return self
5159

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

0 commit comments

Comments
 (0)