You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+55-61Lines changed: 55 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,28 @@
1
1
# openapi-python
2
2
3
-
`openapi-python` generates strongly typed Python API clients from OpenAPI specs, with route-literal dispatch and transport decoupling.
3
+
`openapi-python` generates strongly typed Python API clients from OpenAPI specs, with a developer-friendly and ergonomic string-literal-based interface strongly inspired by [openapi-typescript](https://openapi-ts.dev/).
4
4
5
5
## Installation
6
6
7
-
For protocol-only generated clients where you provide the transport:
8
-
9
-
```bash
10
-
uv add openapi-python
11
-
```
12
-
13
7
For generated clients that use the built-in `httpx` transport:
14
8
15
9
```bash
16
10
uv add "openapi-python[httpx]"
17
11
```
18
12
19
-
## CLI
13
+
For protocol-only generated clients where you provide the transport:
20
14
21
15
```bash
22
-
uv run openapi-python generate --spec ./openapi.json --out ./generated --package my_client
16
+
uv add openapi-python
23
17
```
24
18
25
-
You can also pass the OpenAPI document directly as JSON:
26
19
27
-
```bash
28
-
uv run openapi-python generate --spec-json "$OPENAPI_JSON" --out ./generated --package my_client
29
-
```
20
+
## CLI
30
21
31
-
For URL specs with self-signed certificates, disable verification explicitly:
22
+
Generate a client from an OpenAPI spec in `openapi.json`:
32
23
33
24
```bash
34
-
uv run openapi-python generate --spec https://example.local/openapi.json --out ./generated --no-ssl
25
+
uv run openapi-python generate --spec ./openapi.json --out ./generated --package my_client
35
26
```
36
27
37
28
## Programmatic API
@@ -50,56 +41,44 @@ result = generate_client(
50
41
)
51
42
```
52
43
53
-
To generate from an in-memory OpenAPI document, pass a JSON string instead of `spec_source`:
44
+
## Using generated clients
54
45
55
-
```python
56
-
import json
57
-
from pathlib import Path
46
+
Generated clients expose route-specific callables with typed `params`, `query`, `headers`, `body`, and return values.
58
47
59
-
from openapi_python import GenerationRequest, generate_client
48
+
With the built-in `httpx` transport:
60
49
61
-
result = generate_client(
62
-
GenerationRequest(
63
-
output_dir=Path("./generated"),
64
-
spec_json=json.dumps(app.openapi()),
65
-
package_name="my_client",
66
-
overwrite=True,
67
-
)
68
-
)
69
-
```
70
-
71
-
## Extensibility
72
-
73
-
`GeneratorExtensions` exposes two safe hooks:
50
+
```python
51
+
from generated.my_client import Client
74
52
75
-
-`normalize_hooks`: transform the normalized model before rendering.
76
-
-`render_context_hooks`: transform rendered file content map before writing.
book = client.get("/books/{book_id}")(params={"book_id": 1})
55
+
```
77
56
78
-
Invalid extension outputs fail fast with explicit diagnostics.
57
+
For async APIs:
79
58
80
-
## Releases
59
+
```python
60
+
from generated.my_client import AsyncClient
81
61
82
-
Releases are published from the protected `releases` branch. The package version is set manually in `pyproject.toml`, and pushing a release commit to `releases` triggers the GitHub Actions release workflow. The workflow creates the matching `vX.Y.Z` tag after checks pass.
book = client.get("/books/{book_id}")(params={"book_id": 1})
73
+
```
91
74
92
-
Release steps:
93
75
94
-
```bash
95
-
# 1. Update project.version in pyproject.toml, then commit that change.
96
-
uv run python scripts/release.py --version 0.1.0
76
+
## Extensibility
97
77
98
-
# 2. If checks pass, push the current commit to the releases branch.
99
-
uv run python scripts/release.py --version 0.1.0 --push-release-branch
100
-
```
78
+
`GeneratorExtensions` exposes two safe hooks:
101
79
102
-
The release workflow verifies that the version tag does not already exist, runs checks, builds the distributions, validates them with `twine`, creates the release tag, publishes to PyPI, and creates a GitHub Release with generated notes.
80
+
-`normalize_hooks`: transform the normalized model before rendering.
81
+
-`render_context_hooks`: transform rendered file content map before writing.
book = client.get("/books/{book_id}")(params={"book_id": 1})
126
-
```
127
-
128
-
You can also supply preconfigured `httpx` clients:
98
+
You can supply preconfigured `httpx` clients:
129
99
130
100
```python
131
101
import httpx
@@ -199,3 +169,27 @@ client = Client(
199
169
)
200
170
book = client.get("/books/{book_id}")(params={"book_id": 1})
201
171
```
172
+
173
+
## Releases
174
+
175
+
Releases are published from the protected `releases` branch. The package version is set manually in `pyproject.toml`, and pushing a release commit to `releases` triggers the GitHub Actions release workflow. The workflow creates the matching `vX.Y.Z` tag after checks pass.
176
+
177
+
Before the first release, configure PyPI Trusted Publishing for this repository:
178
+
179
+
- PyPI project: `openapi-python`
180
+
- GitHub workflow: `release.yml`
181
+
- GitHub environment: `pypi`
182
+
183
+
The GitHub `pypi` environment should be limited to deployments from the `releases` branch.
184
+
185
+
Release steps:
186
+
187
+
```bash
188
+
# 1. Update project.version in pyproject.toml, then commit that change.
189
+
uv run python scripts/release.py --version 0.1.0
190
+
191
+
# 2. If checks pass, push the current commit to the releases branch.
192
+
uv run python scripts/release.py --version 0.1.0 --push-release-branch
193
+
```
194
+
195
+
The release workflow verifies that the version tag does not already exist, runs checks, builds the distributions, validates them with `twine`, creates the release tag, publishes to PyPI, and creates a GitHub Release with generated notes.
0 commit comments