Skip to content

Commit 8728ee8

Browse files
authored
feat: add return mode like a parameter (#59)
* fix: interpolations erros and other things reported by sourcery-ai * feat: add return mode like a parameter * chore: change constants.py by types.py
1 parent d1254a6 commit 8728ee8

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

postgrest_py/_async/request_builder.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
pre_upsert,
1212
process_response,
1313
)
14+
from postgrest_py.types import ReturnMethod
1415
from postgrest_py.utils import AsyncClient
1516

1617

@@ -78,13 +79,15 @@ def insert(
7879
json: dict,
7980
*,
8081
count: Optional[CountMethod] = None,
82+
returning: ReturnMethod = ReturnMethod.representation,
8183
upsert=False,
8284
) -> AsyncQueryRequestBuilder:
8385
method, json = pre_insert(
8486
self.session,
8587
self.path,
8688
json,
8789
count=count,
90+
returning=returning,
8891
upsert=upsert,
8992
)
9093
return AsyncQueryRequestBuilder(self.session, self.path, method, json)
@@ -94,13 +97,15 @@ def upsert(
9497
json: dict,
9598
*,
9699
count: Optional[CountMethod] = None,
100+
returning: ReturnMethod = ReturnMethod.representation,
97101
ignore_duplicates=False,
98102
) -> AsyncQueryRequestBuilder:
99103
method, json = pre_upsert(
100104
self.session,
101105
self.path,
102106
json,
103107
count=count,
108+
returning=returning,
104109
ignore_duplicates=ignore_duplicates,
105110
)
106111
return AsyncQueryRequestBuilder(self.session, self.path, method, json)
@@ -110,10 +115,27 @@ def update(
110115
json: dict,
111116
*,
112117
count: Optional[CountMethod] = None,
118+
returning: ReturnMethod = ReturnMethod.representation,
113119
) -> AsyncFilterRequestBuilder:
114-
method, json = pre_update(self.session, self.path, json, count=count)
120+
method, json = pre_update(
121+
self.session,
122+
self.path,
123+
json,
124+
count=count,
125+
returning=returning,
126+
)
115127
return AsyncFilterRequestBuilder(self.session, self.path, method, json)
116128

117-
def delete(self, *, count: Optional[CountMethod] = None) -> AsyncFilterRequestBuilder:
118-
method, json = pre_delete(self.session, self.path, count=count)
129+
def delete(
130+
self,
131+
*,
132+
count: Optional[CountMethod] = None,
133+
returning: ReturnMethod = ReturnMethod.representation,
134+
) -> AsyncFilterRequestBuilder:
135+
method, json = pre_delete(
136+
self.session,
137+
self.path,
138+
count=count,
139+
returning=returning,
140+
)
119141
return AsyncFilterRequestBuilder(self.session, self.path, method, json)

postgrest_py/_sync/request_builder.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
pre_upsert,
1212
process_response,
1313
)
14+
from postgrest_py.types import ReturnMethod
1415
from postgrest_py.utils import SyncClient
1516

1617

@@ -78,13 +79,15 @@ def insert(
7879
json: dict,
7980
*,
8081
count: Optional[CountMethod] = None,
82+
returning: ReturnMethod = ReturnMethod.representation,
8183
upsert=False,
8284
) -> SyncQueryRequestBuilder:
8385
method, json = pre_insert(
8486
self.session,
8587
self.path,
8688
json,
8789
count=count,
90+
returning=returning,
8891
upsert=upsert,
8992
)
9093
return SyncQueryRequestBuilder(self.session, self.path, method, json)
@@ -94,13 +97,15 @@ def upsert(
9497
json: dict,
9598
*,
9699
count: Optional[CountMethod] = None,
100+
returning: ReturnMethod = ReturnMethod.representation,
97101
ignore_duplicates=False,
98102
) -> SyncQueryRequestBuilder:
99103
method, json = pre_upsert(
100104
self.session,
101105
self.path,
102106
json,
103107
count=count,
108+
returning=returning,
104109
ignore_duplicates=ignore_duplicates,
105110
)
106111
return SyncQueryRequestBuilder(self.session, self.path, method, json)
@@ -110,10 +115,27 @@ def update(
110115
json: dict,
111116
*,
112117
count: Optional[CountMethod] = None,
118+
returning: ReturnMethod = ReturnMethod.representation,
113119
) -> SyncFilterRequestBuilder:
114-
method, json = pre_update(self.session, self.path, json, count=count)
120+
method, json = pre_update(
121+
self.session,
122+
self.path,
123+
json,
124+
count=count,
125+
returning=returning,
126+
)
115127
return SyncFilterRequestBuilder(self.session, self.path, method, json)
116128

117-
def delete(self, *, count: Optional[CountMethod] = None) -> SyncFilterRequestBuilder:
118-
method, json = pre_delete(self.session, self.path, count=count)
129+
def delete(
130+
self,
131+
*,
132+
count: Optional[CountMethod] = None,
133+
returning: ReturnMethod = ReturnMethod.representation,
134+
) -> SyncFilterRequestBuilder:
135+
method, json = pre_delete(
136+
self.session,
137+
self.path,
138+
count=count,
139+
returning=returning,
140+
)
119141
return SyncFilterRequestBuilder(self.session, self.path, method, json)

postgrest_py/base_request_builder.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from httpx import Response
55

6-
from postgrest_py.constants import CountMethod, Filters, RequestMethod
6+
from postgrest_py.types import CountMethod, Filters, RequestMethod, ReturnMethod
77
from postgrest_py.utils import (
88
AsyncClient,
99
SyncClient,
@@ -33,10 +33,11 @@ def pre_insert(
3333
path: str,
3434
json: dict,
3535
*,
36-
count: Optional[CountMethod] = None,
37-
upsert=False,
36+
count: Optional[CountMethod],
37+
returning: ReturnMethod,
38+
upsert: bool,
3839
) -> Tuple[RequestMethod, dict]:
39-
prefer_headers = ["return=representation"]
40+
prefer_headers = [f"return={returning}"]
4041
if count:
4142
prefer_headers.append(f"count={count}")
4243
if upsert:
@@ -50,8 +51,9 @@ def pre_upsert(
5051
path: str,
5152
json: dict,
5253
*,
53-
count: Optional[CountMethod] = None,
54-
ignore_duplicates=False,
54+
count: Optional[CountMethod],
55+
returning: ReturnMethod,
56+
ignore_duplicates: bool,
5557
) -> Tuple[RequestMethod, dict]:
5658
prefer_headers = ["return=representation"]
5759
if count:
@@ -67,9 +69,10 @@ def pre_update(
6769
path: str,
6870
json: dict,
6971
*,
70-
count: Optional[CountMethod] = None,
72+
count: Optional[CountMethod],
73+
returning: ReturnMethod,
7174
) -> Tuple[RequestMethod, dict]:
72-
prefer_headers = ["return=representation"]
75+
prefer_headers = [f"return={returning}"]
7376
if count:
7477
prefer_headers.append(f"count={count}")
7578
session.headers["prefer"] = ",".join(prefer_headers)
@@ -80,9 +83,10 @@ def pre_delete(
8083
session: Union[AsyncClient, SyncClient],
8184
path: str,
8285
*,
83-
count: Optional[CountMethod] = None,
86+
count: Optional[CountMethod],
87+
returning: ReturnMethod,
8488
) -> Tuple[RequestMethod, dict]:
85-
prefer_headers = ["return=representation"]
89+
prefer_headers = [f"return={returning}"]
8690
if count:
8791
prefer_headers.append(f"count={count}")
8892
session.headers["prefer"] = ",".join(prefer_headers)

postgrest_py/constants.py renamed to postgrest_py/types.py

+5
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ class RequestMethod(str, Enum):
4040
PUT = "PUT"
4141
DELETE = "DELETE"
4242
HEAD = "HEAD"
43+
44+
45+
class ReturnMethod(str, Enum):
46+
minimal = "minimal"
47+
representation = "representation"

tests/_async/test_request_builder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from postgrest_py import AsyncRequestBuilder
4-
from postgrest_py.constants import CountMethod
4+
from postgrest_py.types import CountMethod
55
from postgrest_py.utils import AsyncClient
66

77

tests/_sync/test_request_builder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from postgrest_py import SyncRequestBuilder
4-
from postgrest_py.constants import CountMethod
4+
from postgrest_py.types import CountMethod
55
from postgrest_py.utils import SyncClient
66

77

0 commit comments

Comments
 (0)