diff --git a/postgrest_py/_async/request_builder.py b/postgrest_py/_async/request_builder.py index c058940f..5566b6f4 100644 --- a/postgrest_py/_async/request_builder.py +++ b/postgrest_py/_async/request_builder.py @@ -11,6 +11,7 @@ pre_upsert, process_response, ) +from postgrest_py.types import ReturnMethod from postgrest_py.utils import AsyncClient @@ -78,6 +79,7 @@ def insert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, upsert=False, ) -> AsyncQueryRequestBuilder: method, json = pre_insert( @@ -85,6 +87,7 @@ def insert( self.path, json, count=count, + returning=returning, upsert=upsert, ) return AsyncQueryRequestBuilder(self.session, self.path, method, json) @@ -94,6 +97,7 @@ def upsert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ignore_duplicates=False, ) -> AsyncQueryRequestBuilder: method, json = pre_upsert( @@ -101,6 +105,7 @@ def upsert( self.path, json, count=count, + returning=returning, ignore_duplicates=ignore_duplicates, ) return AsyncQueryRequestBuilder(self.session, self.path, method, json) @@ -110,10 +115,27 @@ def update( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ) -> AsyncFilterRequestBuilder: - method, json = pre_update(self.session, self.path, json, count=count) + method, json = pre_update( + self.session, + self.path, + json, + count=count, + returning=returning, + ) return AsyncFilterRequestBuilder(self.session, self.path, method, json) - def delete(self, *, count: Optional[CountMethod] = None) -> AsyncFilterRequestBuilder: - method, json = pre_delete(self.session, self.path, count=count) + def delete( + self, + *, + count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, + ) -> AsyncFilterRequestBuilder: + method, json = pre_delete( + self.session, + self.path, + count=count, + returning=returning, + ) return AsyncFilterRequestBuilder(self.session, self.path, method, json) diff --git a/postgrest_py/_sync/request_builder.py b/postgrest_py/_sync/request_builder.py index 01cd3a97..3144cbd9 100644 --- a/postgrest_py/_sync/request_builder.py +++ b/postgrest_py/_sync/request_builder.py @@ -11,6 +11,7 @@ pre_upsert, process_response, ) +from postgrest_py.types import ReturnMethod from postgrest_py.utils import SyncClient @@ -78,6 +79,7 @@ def insert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, upsert=False, ) -> SyncQueryRequestBuilder: method, json = pre_insert( @@ -85,6 +87,7 @@ def insert( self.path, json, count=count, + returning=returning, upsert=upsert, ) return SyncQueryRequestBuilder(self.session, self.path, method, json) @@ -94,6 +97,7 @@ def upsert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ignore_duplicates=False, ) -> SyncQueryRequestBuilder: method, json = pre_upsert( @@ -101,6 +105,7 @@ def upsert( self.path, json, count=count, + returning=returning, ignore_duplicates=ignore_duplicates, ) return SyncQueryRequestBuilder(self.session, self.path, method, json) @@ -110,10 +115,27 @@ def update( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ) -> SyncFilterRequestBuilder: - method, json = pre_update(self.session, self.path, json, count=count) + method, json = pre_update( + self.session, + self.path, + json, + count=count, + returning=returning, + ) return SyncFilterRequestBuilder(self.session, self.path, method, json) - def delete(self, *, count: Optional[CountMethod] = None) -> SyncFilterRequestBuilder: - method, json = pre_delete(self.session, self.path, count=count) + def delete( + self, + *, + count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, + ) -> SyncFilterRequestBuilder: + method, json = pre_delete( + self.session, + self.path, + count=count, + returning=returning, + ) return SyncFilterRequestBuilder(self.session, self.path, method, json) diff --git a/postgrest_py/base_request_builder.py b/postgrest_py/base_request_builder.py index 350a71d0..b471580a 100644 --- a/postgrest_py/base_request_builder.py +++ b/postgrest_py/base_request_builder.py @@ -3,7 +3,7 @@ from httpx import Response -from postgrest_py.constants import CountMethod, Filters, RequestMethod +from postgrest_py.types import CountMethod, Filters, RequestMethod, ReturnMethod from postgrest_py.utils import ( AsyncClient, SyncClient, @@ -33,10 +33,11 @@ def pre_insert( path: str, json: dict, *, - count: Optional[CountMethod] = None, - upsert=False, + count: Optional[CountMethod], + returning: ReturnMethod, + upsert: bool, ) -> Tuple[RequestMethod, dict]: - prefer_headers = ["return=representation"] + prefer_headers = [f"return={returning}"] if count: prefer_headers.append(f"count={count}") if upsert: @@ -50,8 +51,9 @@ def pre_upsert( path: str, json: dict, *, - count: Optional[CountMethod] = None, - ignore_duplicates=False, + count: Optional[CountMethod], + returning: ReturnMethod, + ignore_duplicates: bool, ) -> Tuple[RequestMethod, dict]: prefer_headers = ["return=representation"] if count: @@ -67,9 +69,10 @@ def pre_update( path: str, json: dict, *, - count: Optional[CountMethod] = None, + count: Optional[CountMethod], + returning: ReturnMethod, ) -> Tuple[RequestMethod, dict]: - prefer_headers = ["return=representation"] + prefer_headers = [f"return={returning}"] if count: prefer_headers.append(f"count={count}") session.headers["prefer"] = ",".join(prefer_headers) @@ -80,9 +83,10 @@ def pre_delete( session: Union[AsyncClient, SyncClient], path: str, *, - count: Optional[CountMethod] = None, + count: Optional[CountMethod], + returning: ReturnMethod, ) -> Tuple[RequestMethod, dict]: - prefer_headers = ["return=representation"] + prefer_headers = [f"return={returning}"] if count: prefer_headers.append(f"count={count}") session.headers["prefer"] = ",".join(prefer_headers) diff --git a/postgrest_py/constants.py b/postgrest_py/types.py similarity index 87% rename from postgrest_py/constants.py rename to postgrest_py/types.py index 93c294e5..652074bc 100644 --- a/postgrest_py/constants.py +++ b/postgrest_py/types.py @@ -40,3 +40,8 @@ class RequestMethod(str, Enum): PUT = "PUT" DELETE = "DELETE" HEAD = "HEAD" + + +class ReturnMethod(str, Enum): + minimal = "minimal" + representation = "representation" diff --git a/tests/_async/test_request_builder.py b/tests/_async/test_request_builder.py index 385a6337..85339b01 100644 --- a/tests/_async/test_request_builder.py +++ b/tests/_async/test_request_builder.py @@ -1,7 +1,7 @@ import pytest from postgrest_py import AsyncRequestBuilder -from postgrest_py.constants import CountMethod +from postgrest_py.types import CountMethod from postgrest_py.utils import AsyncClient diff --git a/tests/_sync/test_request_builder.py b/tests/_sync/test_request_builder.py index dac03efe..4fb38500 100644 --- a/tests/_sync/test_request_builder.py +++ b/tests/_sync/test_request_builder.py @@ -1,7 +1,7 @@ import pytest from postgrest_py import SyncRequestBuilder -from postgrest_py.constants import CountMethod +from postgrest_py.types import CountMethod from postgrest_py.utils import SyncClient