From 3476cb9b73560db8ba75ed0296cba384fc1305e2 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Tue, 28 May 2024 20:03:59 +0000 Subject: [PATCH 1/4] add get, head and count parameters to the rpc method. --- postgrest/_async/client.py | 24 +++++++++++++++++++++--- postgrest/_sync/client.py | 24 +++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 32384da9..f94baf84 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -1,10 +1,12 @@ from __future__ import annotations -from typing import Any, Dict, Union, cast +from typing import Any, Dict, Optional, Union, cast from deprecation import deprecated from httpx import Headers, QueryParams, Timeout +from postgrest.types import CountMethod + from .. import __version__ from ..base_client import BasePostgrestClient from ..constants import ( @@ -78,12 +80,22 @@ def from_table(self, table: str) -> AsyncRequestBuilder: """Alias to :meth:`from_`.""" return self.from_(table) - def rpc(self, func: str, params: dict) -> AsyncRPCFilterRequestBuilder[Any]: + def rpc( + self, + func: str, + params: dict, + count: Optional[CountMethod] = None, + head: bool = False, + get: bool = False, + ) -> AsyncRPCFilterRequestBuilder[Any]: """Perform a stored procedure call. Args: func: The name of the remote procedure to run. params: The parameters to be passed to the remote procedure. + count: The method to use to get the count of rows returned. + head: When set to `true`, `data` will not be returned. Useful if you only need the count. + get: When set to `true`, the function will be called with read-only access mode. Returns: :class:`AsyncRPCFilterRequestBuilder` Example: @@ -97,7 +109,13 @@ def rpc(self, func: str, params: dict) -> AsyncRPCFilterRequestBuilder[Any]: This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to filter on the RPC's resultset. """ + method = "POST" + if head or get: + method = "HEAD" if head else "GET" + + headers = Headers({"Prefer": f"count={count}"}) if count else Headers() + # the params here are params to be sent to the RPC and not the queryparams! return AsyncRPCFilterRequestBuilder[Any]( - self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params + self.session, f"/rpc/{func}", method, headers, QueryParams(), json=params ) diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index 226e9994..355fb741 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -1,10 +1,12 @@ from __future__ import annotations -from typing import Any, Dict, Union, cast +from typing import Any, Dict, Optional, Union, cast from deprecation import deprecated from httpx import Headers, QueryParams, Timeout +from postgrest.types import CountMethod + from .. import __version__ from ..base_client import BasePostgrestClient from ..constants import ( @@ -78,12 +80,22 @@ def from_table(self, table: str) -> SyncRequestBuilder: """Alias to :meth:`from_`.""" return self.from_(table) - def rpc(self, func: str, params: dict) -> SyncRPCFilterRequestBuilder[Any]: + def rpc( + self, + func: str, + params: dict, + count: Optional[CountMethod] = None, + head: bool = False, + get: bool = False, + ) -> SyncRPCFilterRequestBuilder[Any]: """Perform a stored procedure call. Args: func: The name of the remote procedure to run. params: The parameters to be passed to the remote procedure. + count: The method to use to get the count of rows returned. + head: When set to `true`, `data` will not be returned. Useful if you only need the count. + get: When set to `true`, the function will be called with read-only access mode. Returns: :class:`AsyncRPCFilterRequestBuilder` Example: @@ -97,7 +109,13 @@ def rpc(self, func: str, params: dict) -> SyncRPCFilterRequestBuilder[Any]: This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to filter on the RPC's resultset. """ + method = "POST" + if head or get: + method = "HEAD" if head else "GET" + + headers = Headers({"Prefer": f"count={count}"}) if count else Headers() + # the params here are params to be sent to the RPC and not the queryparams! return SyncRPCFilterRequestBuilder[Any]( - self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params + self.session, f"/rpc/{func}", method, headers, QueryParams(), json=params ) From f598007162282487133fac53c9eba04bf6a29201 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Tue, 28 May 2024 20:13:35 +0000 Subject: [PATCH 2/4] import from relative path --- postgrest/_async/client.py | 3 +-- postgrest/_sync/client.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index f94baf84..2b480c42 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -5,14 +5,13 @@ from deprecation import deprecated from httpx import Headers, QueryParams, Timeout -from postgrest.types import CountMethod - from .. import __version__ from ..base_client import BasePostgrestClient from ..constants import ( DEFAULT_POSTGREST_CLIENT_HEADERS, DEFAULT_POSTGREST_CLIENT_TIMEOUT, ) +from ..types import CountMethod from ..utils import AsyncClient from .request_builder import AsyncRequestBuilder, AsyncRPCFilterRequestBuilder diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index 355fb741..c1433f26 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -5,14 +5,13 @@ from deprecation import deprecated from httpx import Headers, QueryParams, Timeout -from postgrest.types import CountMethod - from .. import __version__ from ..base_client import BasePostgrestClient from ..constants import ( DEFAULT_POSTGREST_CLIENT_HEADERS, DEFAULT_POSTGREST_CLIENT_TIMEOUT, ) +from ..types import CountMethod from ..utils import SyncClient from .request_builder import SyncRequestBuilder, SyncRPCFilterRequestBuilder From 87735cc249cbe3489111232c1cb0d7c6381012a4 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 1 Jun 2024 10:43:03 +0000 Subject: [PATCH 3/4] update method check to a ternary --- postgrest/_async/client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 2b480c42..4c9a102f 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -108,9 +108,7 @@ def rpc( This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to filter on the RPC's resultset. """ - method = "POST" - if head or get: - method = "HEAD" if head else "GET" + method = "HEAD" if head else "GET" if get else "POST" headers = Headers({"Prefer": f"count={count}"}) if count else Headers() From c0d36e4726483bafba1274f074fb3cad1590e94d Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 1 Jun 2024 11:56:21 +0000 Subject: [PATCH 4/4] update sync rpc method --- postgrest/_sync/client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index c1433f26..f3ca5e7e 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -108,9 +108,7 @@ def rpc( This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to filter on the RPC's resultset. """ - method = "POST" - if head or get: - method = "HEAD" if head else "GET" + method = "HEAD" if head else "GET" if get else "POST" headers = Headers({"Prefer": f"count={count}"}) if count else Headers()