Skip to content

fix: use SelectRequestBuilder for rpc call #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ await client.from_("countries").delete().eq("name", "Việt Nam").execute()
### General filters

### Stored procedures (RPC)

```py
await client.rpc("foobar", {"arg1": "value1", "arg2": "value2"}).execute()
query = await client.rpc("foobar", {"arg1": "value1", "arg2": "value2"})
# for stored procedures that return a result set
await query.execute()
# for stored procedures that return a single object
await query.single().execute()
```

## DEVELOPMENT
Expand Down
10 changes: 5 additions & 5 deletions postgrest/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
)
from ..utils import AsyncClient
from .request_builder import AsyncFilterRequestBuilder, AsyncRequestBuilder
from .request_builder import AsyncRequestBuilder, AsyncSelectRequestBuilder


class AsyncPostgrestClient(BasePostgrestClient):
Expand Down Expand Up @@ -76,24 +76,24 @@ def from_table(self, table: str) -> AsyncRequestBuilder:
"""Alias to :meth:`from_`."""
return self.from_(table)

async def rpc(self, func: str, params: dict) -> AsyncFilterRequestBuilder:
async def rpc(self, func: str, params: dict) -> AsyncSelectRequestBuilder:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any reason to use async for this function?
await (await client.rpc(...)).execute()

Suggested change
async def rpc(self, func: str, params: dict) -> AsyncSelectRequestBuilder:
def rpc(self, func: str, params: dict) -> AsyncSelectRequestBuilder:

"""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.
Returns:
:class:`AsyncFilterRequestBuilder`
:class:`AsyncSelectRequestBuilder`
Example:
.. code-block:: python

await client.rpc("foobar", {"arg": "value"}).execute()

.. versionchanged:: 0.11.0
This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to
This method now returns a :class:`AsyncSelectRequestBuilder` which allows you to
filter on the RPC's resultset.
"""
# the params here are params to be sent to the RPC and not the queryparams!
return AsyncFilterRequestBuilder(
return AsyncSelectRequestBuilder(
self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params
)
10 changes: 5 additions & 5 deletions postgrest/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
)
from ..utils import SyncClient
from .request_builder import SyncFilterRequestBuilder, SyncRequestBuilder
from .request_builder import SyncRequestBuilder, SyncSelectRequestBuilder


class SyncPostgrestClient(BasePostgrestClient):
Expand Down Expand Up @@ -76,24 +76,24 @@ def from_table(self, table: str) -> SyncRequestBuilder:
"""Alias to :meth:`from_`."""
return self.from_(table)

def rpc(self, func: str, params: dict) -> SyncFilterRequestBuilder:
def rpc(self, func: str, params: dict) -> SyncSelectRequestBuilder:
"""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.
Returns:
:class:`AsyncFilterRequestBuilder`
:class:`ASyncSelectRequestBuilder`
Example:
.. code-block:: python

await client.rpc("foobar", {"arg": "value"}).execute()

.. versionchanged:: 0.11.0
This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to
This method now returns a :class:`ASyncSelectRequestBuilder` which allows you to
filter on the RPC's resultset.
"""
# the params here are params to be sent to the RPC and not the queryparams!
return SyncFilterRequestBuilder(
return SyncSelectRequestBuilder(
self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params
)