Skip to content

Commit b1d48bc

Browse files
authored
fix: add get, head and count parameters to the rpc method. (#444)
1 parent 465232a commit b1d48bc

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

postgrest/_async/client.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict, Union, cast
3+
from typing import Any, Dict, Optional, Union, cast
44

55
from deprecation import deprecated
66
from httpx import Headers, QueryParams, Timeout
@@ -11,6 +11,7 @@
1111
DEFAULT_POSTGREST_CLIENT_HEADERS,
1212
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
1313
)
14+
from ..types import CountMethod
1415
from ..utils import AsyncClient
1516
from .request_builder import AsyncRequestBuilder, AsyncRPCFilterRequestBuilder
1617

@@ -78,12 +79,22 @@ def from_table(self, table: str) -> AsyncRequestBuilder:
7879
"""Alias to :meth:`from_`."""
7980
return self.from_(table)
8081

81-
def rpc(self, func: str, params: dict) -> AsyncRPCFilterRequestBuilder[Any]:
82+
def rpc(
83+
self,
84+
func: str,
85+
params: dict,
86+
count: Optional[CountMethod] = None,
87+
head: bool = False,
88+
get: bool = False,
89+
) -> AsyncRPCFilterRequestBuilder[Any]:
8290
"""Perform a stored procedure call.
8391
8492
Args:
8593
func: The name of the remote procedure to run.
8694
params: The parameters to be passed to the remote procedure.
95+
count: The method to use to get the count of rows returned.
96+
head: When set to `true`, `data` will not be returned. Useful if you only need the count.
97+
get: When set to `true`, the function will be called with read-only access mode.
8798
Returns:
8899
:class:`AsyncRPCFilterRequestBuilder`
89100
Example:
@@ -97,7 +108,11 @@ def rpc(self, func: str, params: dict) -> AsyncRPCFilterRequestBuilder[Any]:
97108
This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to
98109
filter on the RPC's resultset.
99110
"""
111+
method = "HEAD" if head else "GET" if get else "POST"
112+
113+
headers = Headers({"Prefer": f"count={count}"}) if count else Headers()
114+
100115
# the params here are params to be sent to the RPC and not the queryparams!
101116
return AsyncRPCFilterRequestBuilder[Any](
102-
self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params
117+
self.session, f"/rpc/{func}", method, headers, QueryParams(), json=params
103118
)

postgrest/_sync/client.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict, Union, cast
3+
from typing import Any, Dict, Optional, Union, cast
44

55
from deprecation import deprecated
66
from httpx import Headers, QueryParams, Timeout
@@ -11,6 +11,7 @@
1111
DEFAULT_POSTGREST_CLIENT_HEADERS,
1212
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
1313
)
14+
from ..types import CountMethod
1415
from ..utils import SyncClient
1516
from .request_builder import SyncRequestBuilder, SyncRPCFilterRequestBuilder
1617

@@ -78,12 +79,22 @@ def from_table(self, table: str) -> SyncRequestBuilder:
7879
"""Alias to :meth:`from_`."""
7980
return self.from_(table)
8081

81-
def rpc(self, func: str, params: dict) -> SyncRPCFilterRequestBuilder[Any]:
82+
def rpc(
83+
self,
84+
func: str,
85+
params: dict,
86+
count: Optional[CountMethod] = None,
87+
head: bool = False,
88+
get: bool = False,
89+
) -> SyncRPCFilterRequestBuilder[Any]:
8290
"""Perform a stored procedure call.
8391
8492
Args:
8593
func: The name of the remote procedure to run.
8694
params: The parameters to be passed to the remote procedure.
95+
count: The method to use to get the count of rows returned.
96+
head: When set to `true`, `data` will not be returned. Useful if you only need the count.
97+
get: When set to `true`, the function will be called with read-only access mode.
8798
Returns:
8899
:class:`AsyncRPCFilterRequestBuilder`
89100
Example:
@@ -97,7 +108,11 @@ def rpc(self, func: str, params: dict) -> SyncRPCFilterRequestBuilder[Any]:
97108
This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to
98109
filter on the RPC's resultset.
99110
"""
111+
method = "HEAD" if head else "GET" if get else "POST"
112+
113+
headers = Headers({"Prefer": f"count={count}"}) if count else Headers()
114+
100115
# the params here are params to be sent to the RPC and not the queryparams!
101116
return SyncRPCFilterRequestBuilder[Any](
102-
self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params
117+
self.session, f"/rpc/{func}", method, headers, QueryParams(), json=params
103118
)

0 commit comments

Comments
 (0)