TALISMAN API adapter
Project description
📦 ptal‑api Repository Documentation
tl;dr This repository provides a Python client library (
ptal_api) for interacting with the Talisman GraphQL APIs, along with a generator script that automatically creates type‑safe client code from GraphQL schemas.
Table of Contents
- What the repository contains
- Prerequisites
- Installation (as a library)
- Project layout
- Generating API client code
- Using the generated client
- Example snippets
- Testing
- Code Style
What the repository contains
| Directory / File | Purpose |
|---|---|
ptal_api/ |
Core package that ships the runtime client (Client), authentication providers, and generated schema modules. |
ptal_api/schemas/ |
Destination for the generated GraphQL operation classes (mesh_schema, system_utils_schema, …). |
generator_script/ |
Helper utilities that turn GraphQL introspection files into ready‑to‑use Python modules. |
generator_script/generate_script.py |
Orchestrates the whole generation pipeline: runs ariadne‑codegen, moves files, patches imports, and cleans the code with Ruff. |
generator_script/async_base_client.py |
Base async client class shared by all generated modules. |
generator_script/toml_configs/ |
Ariadne‑codegen configuration (*.toml) for each API. |
examples/ |
Small, ready‑to‑run scripts showing how to use the client (adapter usage, mutation, query, raw execution). |
tests/ |
Test suite for the library. |
pyproject.toml / poetry.lock |
Poetry‑managed project metadata and dependencies. |
README.md (this file) |
High‑level documentation (you are reading it!). |
CHANGELOG.md |
Change history. |
Prerequisites
| Tool | Minimum version | Why you need it |
|---|---|---|
| Python | 3.12+ |
Required for the async‑first design and the typing features used throughout the code. |
| Poetry | 2.0+ |
Manages the virtual environment and project dependencies. |
| ariadne‑codegen | latest (install via pip or Poetry) |
Generates the type‑safe GraphQL client modules from schema files. |
| ruff | latest (install via pip or Poetry) |
Auto‑formatter and lint fixer applied to generated code. |
| httpx | any | Underlying HTTP client used by the generated Client. |
| keycloak (optional) | any | Required if you plan to use the KeycloakAuthProvider. |
| websockets (optional) | any | Needed only for GraphQL subscriptions. |
Install everything with Poetry:
poetry install
Or, if you prefer plain pip:
pip install ariadne-codegen ruff httpx keycloak websockets
Installation (as a library)
Once the generated schemas exist (see the Generating API client code section), you can install the package locally for development:
# From the repository root
poetry build
poetry install
Or use it directly from the source tree:
pip install -e .
Project layout
ptal-api/
│
├─ ptal_api/
│ ├─ __init__.py
│ ├─ client.py # High‑level GraphQL client wrapper
│ ├─ auth_provider.py # Abstract authentication base class
│ ├─ keycloak_provider.py # Keycloak‑based auth implementation
│ ├─ no_auth_provider.py # No‑auth placeholder
│ ├─ schemas/ # ← generated GraphQL modules
│ │ ├─ mesh_schema/
│ │ └─ system_utils_schema/
│ └─ ... # Additional utilities
│
├─ generator_script/
│ ├─ async_base_client.py # Shared async client base class
│ ├─ generate_script.py # Main generation entry point
│ └─ toml_configs/
│ ├─ mesh.toml
│ └─ system_utils.toml
│
├─ examples/
│ ├─ adapter_usage.py
│ ├─ cancel_job.py
│ ├─ get_periodic_job.py
│ └─ raw_execute.py
│
├─ tests/ # pytest test suite
│
├─ .gitlab-ci.yml # CI pipeline definition
├─ pyproject.toml # Poetry project definition
├─ poetry.lock
└─ README.md # (this file)
Key modules
| Module | What it provides |
|---|---|
ptal_api.client.Client |
Thin wrapper around httpx.AsyncClient that knows how to send GraphQL queries/mutations. |
ptal_api.auth_provider.AuthProvider |
Abstract base class for authentication providers. |
ptal_api.keycloak_provider.KeycloakAuthProvider |
Implements AuthProvider using Keycloak OIDC token flow. |
ptal_api.no_auth_provider.NoAuthProvider |
Simple provider returning empty headers (useful for public APIs). |
ptal_api.adapter.TalismanAPIAdapter (in examples) |
Convenience wrapper around the generated client exposing higher‑level domain methods. |
Generating API client code
The repository ships a generator script that automates the entire workflow for the two GraphQL APIs (mesh and
system_utils). The script:
- Validates that it runs from the correct directory.
- Calls ariadne‑codegen with the appropriate TOML configuration.
- Moves the generated package into
ptal_api/schemas/. - Patches imports so they point to the shared
ptal_apipackage (ptal_api.base_model,ptal_api.base_operation, etc.). - Runs Ruff to format the code and fix lint issues.
How to run it
cd ptal-api/generator_script
python generate_script.py
The script will output progress messages such as:
Generating mesh_schema
Ended generating mesh_schema
...
Generating system_utils_schema
Ended generating system_utils_schema
... (ruff output) ...
When it finishes, you’ll find the generated Python modules under:
ptal_api/schemas/mesh_schema/
ptal_api/schemas/system_utils_schema/
Note – If you add a new schema, create a matching
*.tomlfile ingenerator_script/toml_configs/and extend theapistuple ingenerate_script.py.
Using the generated client
Below is a minimal example that shows how to set up a client with Keycloak authentication and execute a simple query.
import os
import asyncio
from ptal_api.client import Client
from ptal_api.auth_providers import KeycloakAuthProvider
from ptal_api.schemas.mesh_schema.custom_queries import Query as MeshQuery
from ptal_api.schemas.mesh_schema.custom_fields import DocumentFields
async def main() -> None:
auth = KeycloakAuthProvider(
auth_url=os.getenv("KEYCLOAK_URL"),
realm=os.getenv("KEYCLOAK_REALM"),
client_id=os.getenv("KEYCLOAK_CLIENT_ID"),
client_secret=os.getenv("KEYCLOAK_CLIENT_SECRET"),
user=os.getenv("KEYCLOAK_USER"),
password=os.getenv("KEYCLOAK_PASSWORD"),
)
client = Client(url=os.getenv("GRAPHQL_URL"), auth_provider=auth)
# Example: fetch a document by ID
document_query = MeshQuery.document(id="12345")
document_query.fields(
DocumentFields.id,
DocumentFields.title,
DocumentFields.document_content_type,
)
response = await client.query(document_query, operation_name="GetDocument")
print(response)
if __name__ == "__main__":
asyncio.run(main())
Quick tips
| Situation | What to do |
|---|---|
| No auth required | Use NoAuthProvider() when constructing Client. |
| Custom headers | Subclass AuthProvider and return the desired Headers. |
| Subscriptions | Ensure websockets is installed and pass a ws:// endpoint to Client. |
| Adding new GraphQL operations | Edit the schema, regenerate with the script, and import the new generated classes under ptal_api.schemas.<api>_schema. |
Example snippets
The examples/ directory contains ready‑to‑run scripts that illustrate common patterns:
| File | Description |
|---|---|
adapter_usage.py |
Shows how to obtain a TalismanAPIAdapter and call a high‑level method (get_document). |
cancel_job.py |
Demonstrates a mutation (CancelJob) with custom fields. |
get_periodic_job.py |
Queries a periodic job using a chain of nested field selections. |
raw_execute.py |
Shows the low‑level client.execute() call for cases where the generated helpers are insufficient (not recommended for everyday use). |
You can run any example with:
cd examples
poetry run python <example_name>.py
Remember to set the required environment variables (GRAPHQL_URL, KEYCLOAK_*, etc.) beforehand.
Testing
The repository ships a pytest‑based test suite under tests/. To execute it locally:
poetry run pytest
The CI pipeline (see next section) runs the same command on every merge request, ensuring that new changes keep the library stable.
Code Style
- Follow the existing formatting (auto‑formatted by Ruff).
- Keep import statements absolute (
from ptal_api.base_model import …). - Prefer async functions and
awaitwherever possible.
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 ptal_api-0.15.20.0.tar.gz.
File metadata
- Download URL: ptal_api-0.15.20.0.tar.gz
- Upload date:
- Size: 150.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/5.13.0-28-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf69d0ee3951a49237bfbcac879941756a3220c6bbe1aaf70f704862c6487e5e
|
|
| MD5 |
69dee100eecf6a1492d3fd52c369576d
|
|
| BLAKE2b-256 |
0d5dd8f4d8528cddea26b3c78acb476c7c795b66efd5c3a83374506911e97765
|
File details
Details for the file ptal_api-0.15.20.0-py3-none-any.whl.
File metadata
- Download URL: ptal_api-0.15.20.0-py3-none-any.whl
- Upload date:
- Size: 156.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/5.13.0-28-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b80f9d0a4541832049c16fada09630ee9e26fd76431145f4c3dec4c54fcaf345
|
|
| MD5 |
bd10464f876fe402e3eb4c2be5b9caba
|
|
| BLAKE2b-256 |
6e58ac5ef68ec86636954dcc6013dc3de3c3bee80b646c755b12a4b359e0626f
|