Skip to content

fix!: schema method persisting only on current query #575

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 2 commits into from
Mar 11, 2025
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
11 changes: 11 additions & 0 deletions postgrest/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def create_session(
http2=True,
)

def schema(self, schema: str):
"""Switch to another schema."""
return AsyncPostgrestClient(
base_url=self.base_url,
schema=schema,
headers=self.headers,
timeout=self.timeout,
verify=self.verify,
proxy=self.proxy,
)

async def __aenter__(self) -> AsyncPostgrestClient:
return self

Expand Down
11 changes: 11 additions & 0 deletions postgrest/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def create_session(
http2=True,
)

def schema(self, schema: str):
"""Switch to another schema."""
return SyncPostgrestClient(
base_url=self.base_url,
schema=schema,
headers=self.headers,
timeout=self.timeout,
verify=self.verify,
proxy=self.proxy,
)

def __enter__(self) -> SyncPostgrestClient:
return self

Expand Down
2 changes: 1 addition & 1 deletion postgrest/_sync/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
http_method: str,
headers: Headers,
params: QueryParams,
json: Union[dict, list],
json: dict,
) -> None:
self.session = session
self.path = path
Expand Down
21 changes: 9 additions & 12 deletions postgrest/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ def __init__(
) -> None:
if not is_http_url(base_url):
ValueError("base_url must be a valid HTTP URL string")
headers = {

self.base_url = base_url
self.headers = {
**headers,
"Accept-Profile": schema,
"Content-Profile": schema,
}
self.session = self.create_session(base_url, headers, timeout, verify, proxy)
self.timeout = timeout
self.verify = verify
self.proxy = proxy
self.session = self.create_session(
self.base_url, self.headers, self.timeout, self.verify, self.proxy
)

@abstractmethod
def create_session(
Expand Down Expand Up @@ -68,13 +75,3 @@ def auth(
"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
4 changes: 2 additions & 2 deletions tests/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def test_auth_basic(self, postgrest_client: AsyncPostgrestClient):


def test_schema(postgrest_client: AsyncPostgrestClient):
postgrest_client.schema("private")
session = postgrest_client.session
client = postgrest_client.schema("private")
session = client.session
subheaders = {
"accept-profile": "private",
"content-profile": "private",
Expand Down
4 changes: 2 additions & 2 deletions tests/_sync/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def test_auth_basic(self, postgrest_client: SyncPostgrestClient):


def test_schema(postgrest_client: SyncPostgrestClient):
postgrest_client.schema("private")
session = postgrest_client.session
client = postgrest_client.schema("private")
session = client.session
subheaders = {
"accept-profile": "private",
"content-profile": "private",
Expand Down