diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index b9d266d..94c9c7f 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -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 diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index 29b9aab..a9dd307 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -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 diff --git a/postgrest/_sync/request_builder.py b/postgrest/_sync/request_builder.py index 8b0eb16..9683b1a 100644 --- a/postgrest/_sync/request_builder.py +++ b/postgrest/_sync/request_builder.py @@ -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 diff --git a/postgrest/base_client.py b/postgrest/base_client.py index e2bf417..2c9756a 100644 --- a/postgrest/base_client.py +++ b/postgrest/base_client.py @@ -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( @@ -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 diff --git a/tests/_async/test_client.py b/tests/_async/test_client.py index e99f348..fb7881c 100644 --- a/tests/_async/test_client.py +++ b/tests/_async/test_client.py @@ -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", diff --git a/tests/_sync/test_client.py b/tests/_sync/test_client.py index 5c471c2..930fb7b 100644 --- a/tests/_sync/test_client.py +++ b/tests/_sync/test_client.py @@ -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",