Skip to content

Commit 6493154

Browse files
authored
Namespace change (#114)
* docs: add rtd config * chore: move to the postgrest namespace * chore: move constants to its own file * chore: pass headers/params down builders We were earlier modifying session.headers/session.params for every query. Instead of this we follow what postgrest-js does and add headers and params as arguments to the query builders, and pass them down the chain of builders, and finally pass it to the execute method. * docs: add examples * fix: order of filters in examples * docs: add example for closing the client
1 parent 29e91a2 commit 6493154

39 files changed

+435
-227
lines changed

.readthedocs.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 2
2+
3+
build:
4+
os: ubuntu-20.04
5+
tools:
6+
python: "3.9"
7+
8+
python:
9+
install:
10+
- requirements: docs/requirements.txt
11+
12+
13+
- method: pip
14+
path: .

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ clean_infra:
2525
run_tests: tests
2626

2727
build_sync:
28-
poetry run unasync postgrest_py tests
28+
poetry run unasync postgrest tests

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pip install postgrest-py
5454

5555
```py
5656
import asyncio
57-
from postgrest_py import AsyncPostgrestClient
57+
from postgrest import AsyncPostgrestClient
5858

5959
async def main():
6060
async with AsyncPostgrestClient("http://localhost:3000") as client:
@@ -74,24 +74,27 @@ await client.from_("countries").insert({ "name": "Việt Nam", "capital": "Hà N
7474

7575
```py
7676
r = await client.from_("countries").select("id", "name").execute()
77-
countries = r.json()
77+
countries = r.data
7878
```
7979

8080
### Update
8181

8282
```py
83-
await client.from_("countries").eq("name", "Việt Nam").update({"capital": "Hà Nội"}).execute()
83+
await client.from_("countries").update({"capital": "Hà Nội"}).eq("name", "Việt Nam").execute()
8484
```
8585

8686
### Delete
8787

8888
```py
89-
await client.from_("countries").eq("name", "Việt Nam").delete().execute()
89+
await client.from_("countries").delete().eq("name", "Việt Nam").execute()
9090
```
9191

9292
### General filters
9393

9494
### Stored procedures (RPC)
95+
```py
96+
await client.rpc("foobar", {"arg1": "value1", "arg2": "value2"}).execute()
97+
```
9598

9699
## DEVELOPMENT
97100

docs/api/client.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ To run any queries, the first step is to construct a client.
55

66
The library offers both synchronous and asynchronous clients.
77

8-
.. autoclass:: postgrest_py.AsyncPostgrestClient
8+
.. autoclass:: postgrest.AsyncPostgrestClient
99
:members:
1010
:inherited-members:
1111

12-
.. autoclass:: postgrest_py.SyncPostgrestClient
12+
.. autoclass:: postgrest.SyncPostgrestClient
1313
:members:
1414
:inherited-members:

docs/api/exceptions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Exceptions
22
==========
33

4-
.. autoexception:: postgrest_py.APIError
4+
.. autoexception:: postgrest.APIError
55
:members:

docs/api/filters.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ filter data during queries.
1818
All the filter methods return a modified instance of the filter builder, allowing fluent chaining of filters.
1919

2020

21-
.. autoclass:: postgrest_py.AsyncFilterRequestBuilder
21+
.. autoclass:: postgrest.AsyncFilterRequestBuilder
2222
:members:
23+
:undoc-members:
2324
:inherited-members:
25+
:member-order: bysource
2426

25-
.. autoclass:: postgrest_py.SyncFilterRequestBuilder
27+
.. autoclass:: postgrest.SyncFilterRequestBuilder
2628
:members:
29+
:undoc-members:
2730
:inherited-members:
31+
:member-order: bysource

docs/api/request_builders.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ Request Builders
88
.. warning::
99
These classes are not meant to be constructed by the user.
1010

11-
.. autoclass:: postgrest_py.AsyncRequestBuilder
11+
.. autoclass:: postgrest.AsyncRequestBuilder
1212
:members:
1313
:inherited-members:
1414

15-
.. autoclass:: postgrest_py.AsyncSelectRequestBuilder
15+
.. autoclass:: postgrest.AsyncSelectRequestBuilder
1616
:members:
1717
:inherited-members:
1818

19-
.. autoclass:: postgrest_py.AsyncQueryRequestBuilder
19+
.. autoclass:: postgrest.AsyncQueryRequestBuilder
2020
:members:
2121
:inherited-members:
2222

23-
.. autoclass:: postgrest_py.SyncRequestBuilder
23+
.. autoclass:: postgrest.SyncRequestBuilder
2424
:members:
2525
:inherited-members:
2626

27-
.. autoclass:: postgrest_py.SyncSelectRequestBuilder
27+
.. autoclass:: postgrest.SyncSelectRequestBuilder
2828
:members:
2929
:inherited-members:
3030

31-
.. autoclass:: postgrest_py.SyncQueryRequestBuilder
31+
.. autoclass:: postgrest.SyncQueryRequestBuilder
3232
:members:
3333
:inherited-members:

docs/api/responses.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Responses
22
=========
33

4-
Once a query is run, the postgrest_py parses the server's response into an APIResponse object.
4+
Once a query is run, the library parses the server's response into an APIResponse object.
55

6-
.. autoclass:: postgrest_py.APIResponse
6+
.. autoclass:: postgrest.APIResponse
77
:members:

docs/api/types.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ Types
33

44
Some type aliases and enums used in the library.
55

6-
.. autoclass:: postgrest_py.types.CountMethod
6+
.. autoclass:: postgrest.types.CountMethod
77
:members:
88

9-
.. autoclass:: postgrest_py.types.Filters
9+
.. autoclass:: postgrest.types.Filters
1010
:members:
1111

12-
.. autoclass:: postgrest_py.types.RequestMethod
12+
.. autoclass:: postgrest.types.RequestMethod
1313
:members:
1414

15-
.. autoclass:: postgrest_py.types.ReturnMethod
15+
.. autoclass:: postgrest.types.ReturnMethod
1616
:members:

docs/conf.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717

1818
# -- Project information -----------------------------------------------------
19-
import postgrest_py
19+
import postgrest
2020

2121
project = "postgrest-py"
22-
version = postgrest_py.__version__
22+
version = postgrest.__version__
2323
release = version
2424
copyright = (
2525
"2022, Anand Krishna, Daniel Reinón García, Joel Lee, Leynier Gutiérrez González"
@@ -64,4 +64,4 @@
6464
# Add any paths that contain custom static files (such as style sheets) here,
6565
# relative to this directory. They are copied after the builtin static files,
6666
# so a file named "default.css" will overwrite the builtin "default.css".
67-
html_static_path = ["_static"]
67+
html_static_path = []

docs/examples/basic_queries.rst

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Getting Started
2+
===============
3+
4+
We connect to the API and authenticate, and fetch some data.
5+
6+
.. code-block:: python
7+
:linenos:
8+
9+
import asyncio
10+
from postgrest import AsyncPostgrestClient
11+
12+
async def main():
13+
async with AsyncPostgrestClient("http://localhost:3000") as client:
14+
client.auth("Bearer <token>")
15+
r = await client.from_("countries").select("*").execute()
16+
countries = r.data
17+
18+
asyncio.run(main())
19+
20+
21+
**CRUD**
22+
23+
.. code-block:: python
24+
25+
await client.from_("countries").insert({ "name": "Việt Nam", "capital": "Hà Nội" }).execute()
26+
27+
28+
.. code-block:: python
29+
30+
r = await client.from_("countries").select("id", "name").execute()
31+
countries = r.data
32+
33+
34+
.. code-block:: python
35+
36+
await client.from_("countries").update({"capital": "Hà Nội"}).eq("name", "Việt Nam").execute()
37+
38+
.. code-block:: python
39+
40+
await client.from_("countries").delete().eq("name", "Việt Nam").execute()
41+
42+
**Calling RPCs**
43+
44+
.. code-block:: python
45+
46+
await client.rpc("foo").execute()
47+
48+
.. code-block:: python
49+
50+
await client.rpc("bar", {"arg1": "value1", "arg2": "value2"}).execute()
51+
52+
53+
**Closing the connection**
54+
55+
Once you have finished running your queries, close the connection:
56+
57+
.. code-block:: python
58+
59+
await client.aclose()
60+
61+
62+
You can also use the client with a context manager, which will close the client for you.
63+
64+
.. code-block:: python
65+
66+
async with AsyncPostgrestClient("url") as client:
67+
# run queries
68+
# the client is closed when the async with block ends

docs/examples/index.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
Examples
22
========
33

4-
Stay tuned! Examples are coming soon.
4+
.. note::
5+
The library offers both synchronous and asynchronous clients. In the examples, we use the
6+
async client. However, they should work the same for the sync client as well.
7+
8+
9+
.. toctree::
10+
:maxdepth: 1
11+
:caption: More examples:
12+
13+
Basic Queries <basic_queries>
14+
Logging Requests <logging>

docs/examples/logging.rst

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Logging Requests
2+
================
3+
4+
While debugging, you might want to see the API requests that are being sent for every query.
5+
To do this, just set the logging level to "DEBUG":
6+
7+
.. code-block:: python
8+
:linenos:
9+
10+
from logging import basicConfig, DEBUG
11+
from postgrest import SyncPostgrestClient
12+
13+
basicConfig(level=DEBUG)
14+
15+
client = SyncPostgrestClient(...)
16+
17+
client.from_("test").select("*").eq("a", "b").execute()
18+
client.from_("test").select("*").eq("foo", "bar").eq("baz", "spam").execute()
19+
20+
Output:
21+
22+
.. code-block::
23+
24+
DEBUG:httpx._client:HTTP Request: GET https://<URL>/rest/v1/test?select=%2A&a=eq.b "HTTP/1.1 200 OK"
25+
DEBUG:httpx._client:HTTP Request: GET https://<URL>/rest/v1/test?select=%2A&foo=eq.bar&baz=eq.spam "HTTP/1.1 200 OK"

docs/index.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ Requirements:
1313
- Python >= 3.7
1414

1515
**With pip:**
16+
::
17+
1618
pip install postgrest-py
1719

1820
**With poetry:**
21+
::
22+
1923
poetry add postgrest-py
2024

2125

2226
.. toctree::
23-
:maxdepth: 3
27+
:maxdepth: 2
2428
:caption: Contents:
2529

2630
API Reference </api/index>

docs/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
furo >= 2022.4.7
2+
Sphinx == 4.3.2

postgrest_py/__init__.py renamed to postgrest/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
SyncRequestBuilder,
1919
SyncSelectRequestBuilder,
2020
)
21-
from .base_client import DEFAULT_POSTGREST_CLIENT_HEADERS
2221
from .base_request_builder import APIResponse
22+
from .constants import DEFAULT_POSTGREST_CLIENT_HEADERS
2323
from .deprecated_client import Client, PostgrestClient
2424
from .deprecated_get_request_builder import GetRequestBuilder
2525
from .exceptions import APIError
File renamed without changes.

postgrest_py/_async/client.py renamed to postgrest/_async/client.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from typing import Dict, Union, cast
44

55
from deprecation import deprecated
6-
from httpx import Timeout
6+
from httpx import Headers, QueryParams, Timeout
77

88
from .. import __version__
9-
from ..base_client import (
9+
from ..base_client import BasePostgrestClient
10+
from ..constants import (
1011
DEFAULT_POSTGREST_CLIENT_HEADERS,
1112
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
12-
BasePostgrestClient,
1313
)
1414
from ..utils import AsyncClient
1515
from .request_builder import AsyncFilterRequestBuilder, AsyncRequestBuilder
@@ -73,7 +73,7 @@ def table(self, table: str) -> AsyncRequestBuilder:
7373

7474
@deprecated("0.2.0", "1.0.0", __version__, "Use self.from_() instead")
7575
def from_table(self, table: str) -> AsyncRequestBuilder:
76-
"""Alias to self.from_()."""
76+
"""Alias to :meth:`from_`."""
7777
return self.from_(table)
7878

7979
async def rpc(self, func: str, params: dict) -> AsyncFilterRequestBuilder:
@@ -84,5 +84,16 @@ async def rpc(self, func: str, params: dict) -> AsyncFilterRequestBuilder:
8484
params: The parameters to be passed to the remote procedure.
8585
Returns:
8686
:class:`AsyncFilterRequestBuilder`
87+
Example:
88+
.. code-block:: python
89+
90+
await client.rpc("foobar", {"arg": "value"}).execute()
91+
92+
.. versionchanged:: 0.11.0
93+
This method now returns a :class:`AsyncFilterRequestBuilder` which allows you to
94+
filter on the RPC's resultset.
8795
"""
88-
return AsyncFilterRequestBuilder(self.session, f"/rpc/{func}", "POST", params)
96+
# the params here are params to be sent to the RPC and not the queryparams!
97+
return AsyncFilterRequestBuilder(
98+
self.session, f"/rpc/{func}", "POST", Headers(), QueryParams(), json=params
99+
)

0 commit comments

Comments
 (0)