Package handling tokens and URLs
Project description
cwtoken Technical Reference
cwtoken simplifies working with PostgREST APIs by:
- Automatically handling access token generation
- Provides a query constructor for URL generation
- Making authenticated GET requests to PostgREST endpoints
- Create and manage a lightweight backend that can schedule queries, refresh data on intervals, and expose custom API endpoints.
- Providing a CLI for quick testing and usage
- Including a user-friendly GUI for building and running queries without code
CWClient -- API Client
Represents an authenticated connection to the PostgREST API. All queries are created via this object.
Constructor:
client = CWClient(
api_token: str,
clubcode: str = None,
access_token: str = None,
base_url: str = "https://atukpostgrest.clubwise.com/"
)
Attributes:
- client.access_token — automatically fetched if not provided
- client.headers — dict of headers including Authorization
- client.clubcode — club code used
Methods:
-
client.table(endpoint: str) -> Query
Returns a query constructor object for building endpoint queries. Supports method chaining. -
client.raw_query(full_query: str) -> RawQuery
Returns a raw query object for executing a fully specified URL. -
client.get_endpoints() -> list[str]
Fetches available API endpoints from the PostgREST spec. -
client.last_response() -> dict | str | None
Returns the raw JSON or text of the last HTTP response made by the client.
Query -- Table-based Query Constructor
-
Created via client.table(endpoint)
-
Supports chained methods:
q = client.table("member")
.select("member_no", "first_name")
.filters("date_of_birth=gt.1980-01-01")
.order("first_name", desc=True)
.limit(10)
Methods:
- .select(*columns) — adds columns to select
- .filters(*filters) — raw PostgREST filter strings
- .order(*columns, desc=False) — orders results
- .limit(n) — limits results
- .fetch(to_df=True) -> pandas.DataFrame
- .fetch(to_df=False) -> dict — executes query
- .clear_filters(), .clear_orders(), .clear_params(), .clear_select(), .clear_limit() — reset parts of the query
Utility methods:
-
.compose_url() -> str
Builds the full query string (with filters, ordering, and parameters).
Does not send the request — only constructs the final URL. -
.get_columns() -> list[str]
Fetches one row (with?limit=1) and returns the list of available column names for that endpoint. -
.last_response() -> dict | str | None
Returns the most recent response payload (JSON if possible, else raw text).
RawQuery -- Direct URL Query
- Created via client.raw_query(full_query)
- Used for fully constructed query strings without method chaining.
Example:
df = client.raw_query("member?select=first_name&limit=10").fetch()
Methods:
-
.fetch(to_df=False) -> dict | list
Executes the raw URL request. Returns JSON by default, or pandas.DataFrame ifto_df=True. -
.get_columns() -> list[str]
Fetches one row (with?limit=1) and extracts available column names. -
.last_response() -> dict | str | None
Returns the JSON or raw text of the last executed request.
CWBackend -- Backend API
- Created via CWBackend(client, **kwargs)
- Each kwarg defines an endpoint: name=(function, interval_seconds)
- Functions are executed on schedule, results cached and served via backend
Example:
def example_function(client):
query = client.table("example_table")
data = query.fetch()
return data
backend = CWBackend(
client,
example_endpoint=(example_function, 300)
)
backend.run()
Endpoints:
- /example_endpoint -> returns output of example_function
- /overview -> returns a combined JSON of all endpoints
CLI Functions
test_connection()
Pings the API server to check if it's reachable.
Run from CLI:
cwtoken test
cwtoken gui
Launches the graphical interface for building and executing queries.
Run from CLI:
cwtoken gui
Notes on Usage
- Queries are always linked to a client.
- Method chaining is supported for the query object.
- Both Query and RawQuery store the last response in
.response, accessible via.last_response(). .get_endpoints()discovers all available PostgREST tables..get_columns()checks an endpoint for available column names..compose_url()lets you debug or log the full query string before execution.- Both Query and RawQuery can return results as pandas.DataFrame for analysis or dict/JSON for raw usage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cwtoken-0.5.0.tar.gz.
File metadata
- Download URL: cwtoken-0.5.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a310fe9ece19a2c11b393ceea7a5343cd641571ad3f06746b55ad67d7007fc2
|
|
| MD5 |
1363164497c98302c0e776c8176434f1
|
|
| BLAKE2b-256 |
e26438c0d1073181dca6c198b08134726032f95d6124f01561b4ad9b82bf2b89
|
File details
Details for the file cwtoken-0.5.0-py3-none-any.whl.
File metadata
- Download URL: cwtoken-0.5.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58d3430d3a6b1d76984cf9853d77f407e2d76769fa5eb99190f0fc2c674e0726
|
|
| MD5 |
2c36a756ea2dd89247576e8f3f179db3
|
|
| BLAKE2b-256 |
0e0248737a8a337a8ce15a13a4d6a7523624aa83ed4b5ecf71299251d7eb8972
|