otterapi 0.1.14__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {otterapi-0.1.14 → otterapi-0.2.0}/PKG-INFO +237 -17
- {otterapi-0.1.14 → otterapi-0.2.0}/README.md +234 -12
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/__init__.py +1 -1
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/cli.py +1 -1
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/_body_builder.py +1 -6
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/_features.py +17 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/_pep695.py +1 -1
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/ast_utils.py +72 -27
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/client.py +33 -54
- otterapi-0.2.0/otterapi/codegen/client_layout.py +1009 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/codegen.py +257 -818
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/dataframes.py +3 -0
- otterapi-0.2.0/otterapi/codegen/emit.py +1045 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/endpoints.py +82 -41
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/export.py +127 -17
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_export.py +85 -41
- otterapi-0.2.0/otterapi/codegen/runtime/_query.py +185 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_serialization.py +10 -6
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/schema.py +4 -4
- otterapi-0.2.0/otterapi/codegen/splitting.py +1059 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/types.py +392 -59
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/utils.py +44 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/config.py +62 -13
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v2/v2.py +9 -9
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3/v3.py +1 -1
- otterapi-0.2.0/otterapi/tests/fixtures/golden/envelope/spec.yaml +34 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/envelope_optional/spec.yaml +37 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/nested/spec.yaml +63 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/petstore/spec.json +1 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/petstore_v2/spec.json +1054 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/real_world_patterns/spec.yaml +82 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/shadow/spec.yaml +17 -0
- otterapi-0.2.0/otterapi/tests/fixtures/golden/tagged/spec.yaml +83 -0
- otterapi-0.2.0/otterapi/tests/fixtures/real_specs/README.md +13 -0
- otterapi-0.2.0/otterapi/tests/fixtures/real_specs/github.openapi.json.gz +0 -0
- otterapi-0.2.0/otterapi/tests/fixtures/real_specs/stripe.openapi.json.gz +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_auth.py +6 -6
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_body_flatten.py +6 -6
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_client_retry_lifecycle.py +30 -30
- otterapi-0.2.0/otterapi/tests/test_client_style.py +1812 -0
- otterapi-0.2.0/otterapi/tests/test_codegen_consolidation.py +138 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_codegen_edge_cases.py +2 -2
- otterapi-0.2.0/otterapi/tests/test_codegen_regressions.py +215 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_dataframe.py +4 -4
- otterapi-0.2.0/otterapi/tests/test_discriminated_union_runtime.py +256 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_error_response_types.py +9 -9
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_export.py +36 -18
- otterapi-0.2.0/otterapi/tests/test_generated_correctness.py +562 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_generated_hygiene.py +1 -1
- otterapi-0.2.0/otterapi/tests/test_generated_typecheck.py +165 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_openapi_support.py +2 -2
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_pep695.py +3 -3
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_per_status_errors.py +7 -7
- otterapi-0.2.0/otterapi/tests/test_real_specs_smoke.py +136 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_smoke_generated_client.py +10 -10
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_splitting_resolver.py +19 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_wire_format.py +16 -16
- {otterapi-0.1.14 → otterapi-0.2.0}/pyproject.toml +10 -6
- otterapi-0.1.14/otterapi/codegen/splitting.py +0 -1801
- {otterapi-0.1.14 → otterapi-0.2.0}/.gitignore +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/LICENSE +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/__main__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/_openapi_adapter.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/auth.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/pagination.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_client_stub.py.tpl +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_concurrency.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_dataframe.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_models_mixin.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_pagination.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/codegen/runtime/_retry.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/exceptions.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/_lenient.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/constants.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v2/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v2/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3_1/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3_1/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3_1/v3_1.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3_2/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3_2/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/v3_2/v3_2.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/openapi/warnings.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/py.typed +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/golden/constraints/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/golden/dataframe/config.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/golden/dataframe/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/golden/discriminator/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/golden/paginated/config.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/fixtures/golden/paginated/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_ast_utils.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_atomic_generation.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_body_builder.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_codegen.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_codegen_structure.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_concurrency_runtime.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_config_errors.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_discriminated_unions.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_empty_schema.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_exceptions.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_extra_forbid.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_feature_modules.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_html_repr.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_init_scaffold.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_lenient.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_media_type_parameters.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_not_schema.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_nullable_upgrade.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_openapi_adapter.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_openapi_upgrade.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_pagination.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_public_api.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_recursive_schema.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_retry_runtime.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_runtime_guards.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_schema_constraints.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_splitting_config.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_splitting_integration.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_splitting_tree.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.0}/otterapi/tests/test_unused_imports.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: otterapi
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A cute otter that turns OpenAPI & Swagger specs into type-safe, async Python clients — powered by httpx and Pydantic.
|
|
5
5
|
Project-URL: Source, https://github.com/danplischke/otterapi
|
|
6
6
|
Author: Dan Plischke
|
|
@@ -13,18 +13,16 @@ Classifier: Operating System :: POSIX
|
|
|
13
13
|
Classifier: Operating System :: Unix
|
|
14
14
|
Classifier: Programming Language :: Python
|
|
15
15
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
17
16
|
Classifier: Programming Language :: Python :: 3.11
|
|
18
17
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
19
|
Classifier: Programming Language :: Python :: 3.14
|
|
21
20
|
Classifier: Topic :: Utilities
|
|
22
|
-
Requires-Python: >=3.
|
|
23
|
-
Requires-Dist:
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: httpx2<3,>=2.16.2
|
|
24
23
|
Requires-Dist: pydantic-settings<3,>=2.11
|
|
25
24
|
Requires-Dist: pydantic[email]<3,>=2.12
|
|
26
25
|
Requires-Dist: pyyaml<7,>=6.0.3
|
|
27
|
-
Requires-Dist: tomli>=2.0; python_version < '3.11'
|
|
28
26
|
Requires-Dist: typer<1,>=0.20
|
|
29
27
|
Requires-Dist: universal-pathlib<0.4,>=0.3.4
|
|
30
28
|
Provides-Extra: parquet
|
|
@@ -33,9 +31,9 @@ Description-Content-Type: text/markdown
|
|
|
33
31
|
|
|
34
32
|
# 🦦 OtterAPI
|
|
35
33
|
|
|
36
|
-
> *A cute otter that turns OpenAPI & Swagger specs into type-safe, async Python clients — powered by
|
|
34
|
+
> *A cute otter that turns OpenAPI & Swagger specs into type-safe, async Python clients — powered by [httpx2](https://github.com/pydantic/httpx2) and Pydantic.*
|
|
37
35
|
|
|
38
|
-
**OtterAPI** is a sleek Python library that transforms OpenAPI specifications into clean, type-safe client code with Pydantic models and
|
|
36
|
+
**OtterAPI** is a sleek Python library that transforms OpenAPI specifications into clean, type-safe client code with Pydantic models and httpx2-based HTTP clients.
|
|
39
37
|
|
|
40
38
|
## ✨ Features
|
|
41
39
|
|
|
@@ -59,6 +57,12 @@ Description-Content-Type: text/markdown
|
|
|
59
57
|
pip install otterapi
|
|
60
58
|
```
|
|
61
59
|
|
|
60
|
+
Generated clients use [httpx2](https://github.com/pydantic/httpx2), pydantic's
|
|
61
|
+
maintained fork of httpx (same API, package renamed), installed alongside
|
|
62
|
+
otterapi. A custom `http_client` / `async_http_client` you pass in must be an
|
|
63
|
+
`httpx2.Client` / `httpx2.AsyncClient`. Note that httpx2 verifies TLS against
|
|
64
|
+
the operating system's trust store (`truststore`) rather than `certifi`.
|
|
65
|
+
|
|
62
66
|
### Basic Usage
|
|
63
67
|
|
|
64
68
|
1. Create an `otter.yml` configuration file:
|
|
@@ -176,10 +180,13 @@ Each entry under `documents:` supports these fields:
|
|
|
176
180
|
| `generate_async` | bool | `true` | Generate async endpoint functions |
|
|
177
181
|
| `generate_sync` | bool | `true` | Generate sync endpoint functions |
|
|
178
182
|
| `client_class_name` | string | from API title | Override the generated client class name |
|
|
183
|
+
| `client_style` | `functions` \| `client` \| `resource` | `functions` | Shape of the public API. `functions` exposes a standalone function per endpoint/variant; `client` adds `Client` / `AsyncClient` classes with a flat method per endpoint; `resource` groups those methods into resource sub-clients (see [Client Style](#-client-style)) |
|
|
184
|
+
| `resource_naming` | `tag` \| `path` \| `operation_id` | `tag` | How resource sub-clients are derived for `client_style: resource`. `tag` uses the module-split strategy (one level); `path` nests by URL segments; `operation_id` nests by the dotted operationId hierarchy |
|
|
185
|
+
| `result_objects` | bool | `false` | With `client_style: client` or `resource`, a list endpoint returns a deferred `Query` object instead of separate `_df`/`_pl`/`_iter`/`_export` methods (see [Client Style](#-client-style)) |
|
|
179
186
|
| `function_naming` | `operation_id` \| `path` | `operation_id` | How to name endpoint functions. `path` derives names from the HTTP method and URL path — use it for specs that reuse one `operationId` across many paths |
|
|
180
187
|
| `include_paths` | list | `null` | Glob patterns — only matching paths are generated |
|
|
181
188
|
| `exclude_paths` | list | `null` | Glob patterns — matching paths are skipped (applied after `include_paths`) |
|
|
182
|
-
| `target_python` | `3.
|
|
189
|
+
| `target_python` | `3.11` … `3.14` | `3.11` | Oldest Python the generated code has to run on. `3.12`+ emits PEP 695 type parameters in the runtime helpers |
|
|
183
190
|
| `pydantic_version` | `1` \| `2` | `2` | Target Pydantic version for generated models and helpers |
|
|
184
191
|
|
|
185
192
|
#### Function Naming
|
|
@@ -215,7 +222,7 @@ Patterns follow standard glob syntax (`*` = single segment, `**` = any depth).
|
|
|
215
222
|
|
|
216
223
|
#### Target Python Version
|
|
217
224
|
|
|
218
|
-
Generated code defaults to running on Python 3.
|
|
225
|
+
Generated code defaults to running on Python 3.11, so the runtime helpers
|
|
219
226
|
(`_pagination.py`, `_concurrency.py`) declare their generics with module-level
|
|
220
227
|
`TypeVar`s:
|
|
221
228
|
|
|
@@ -438,7 +445,7 @@ module_split:
|
|
|
438
445
|
| `min_endpoints` | int | `2` | Minimum endpoints per module before consolidating into fallback |
|
|
439
446
|
| `flat_structure` | bool | `false` | Flat files instead of nested directories |
|
|
440
447
|
| `path_depth` | int | `1` | Path segments used for `path` strategy (1–5) |
|
|
441
|
-
| `global_strip_prefixes` | list | `/api`, `/api/v1`, `/api/v2`, `/api/v3` | Prefixes stripped from all paths before matching |
|
|
448
|
+
| `global_strip_prefixes` | list | `/api`, `/api/v1`, `/api/v2`, `/api/v3` | Prefixes stripped from all paths before matching (whole segments only: `/api` strips `/api/users` but leaves `/apikeys` alone) |
|
|
442
449
|
| `module_map` | object | `{}` | Custom module-to-path mappings |
|
|
443
450
|
| `split_models` | bool | `false` | Generate per-module model files instead of one shared `models.py` |
|
|
444
451
|
| `shared_models_module` | string | `_models` | Module name for shared models when `split_models` is `true` |
|
|
@@ -708,14 +715,35 @@ export:
|
|
|
708
715
|
|
|
709
716
|
### Usage
|
|
710
717
|
|
|
718
|
+
Every endpoint returning a list of models gets an `<endpoint>_export` function
|
|
719
|
+
(and an `async_<endpoint>_export` twin) that mirrors the endpoint's own
|
|
720
|
+
parameters and adds `output_path` first, plus keyword-only `format` and
|
|
721
|
+
`batch_size`. Extra keyword arguments are passed through to the file writer.
|
|
722
|
+
A list of a union of models -- such as a `oneOf` with a `discriminator` --
|
|
723
|
+
exports too: the file has every variant's columns, and each row fills its own
|
|
724
|
+
(CSV / TSV cells and Parquet values of the others stay empty; JSONL keeps each
|
|
725
|
+
row's shape). Lists of plain values (`list[str]`) have no columns, so they get
|
|
726
|
+
no export function.
|
|
727
|
+
|
|
711
728
|
```python
|
|
712
|
-
from client import
|
|
729
|
+
from client import list_users_export, async_list_users_export
|
|
713
730
|
|
|
714
|
-
#
|
|
715
|
-
|
|
716
|
-
|
|
731
|
+
rows = list_users_export("output/users.csv", status="active") # returns the row count
|
|
732
|
+
list_users_export("s3://my-bucket/users.parquet", format="parquet") # UPath remote targets work too
|
|
733
|
+
list_users_export("out.csv", format="csv", delimiter=";") # writer options pass through
|
|
717
734
|
```
|
|
718
735
|
|
|
736
|
+
For a [paginated](#-pagination) endpoint the export wrapper streams through the
|
|
737
|
+
endpoint's `_iter` variant and declares exactly its knobs -- the starting
|
|
738
|
+
position (`offset` / `cursor` / `page`), `page_size` and `max_items` -- so
|
|
739
|
+
`list_users_export("out.csv", offset=100)` starts where
|
|
740
|
+
`list_users_iter(offset=100)` does.
|
|
741
|
+
|
|
742
|
+
If an endpoint has its own parameter named `format`, `output_path` or
|
|
743
|
+
`batch_size`, the wrapper exposes it as `format_` (etc.) and forwards it to the
|
|
744
|
+
API under its real name; with `result_objects`, `Query.export()` applies the
|
|
745
|
+
same aliasing for you.
|
|
746
|
+
|
|
719
747
|
---
|
|
720
748
|
|
|
721
749
|
## 🔓 Response Unwrapping
|
|
@@ -794,8 +822,11 @@ from client.orders import list_orders, get_order
|
|
|
794
822
|
|
|
795
823
|
### Using the Client Class
|
|
796
824
|
|
|
825
|
+
In the default `functions` style the `Client` carries configuration and the
|
|
826
|
+
connection pool; you pass it to each endpoint function via `client=`:
|
|
827
|
+
|
|
797
828
|
```python
|
|
798
|
-
from client import Client
|
|
829
|
+
from client import Client, get_user, async_get_user
|
|
799
830
|
|
|
800
831
|
client = Client(
|
|
801
832
|
base_url='https://api.example.com',
|
|
@@ -803,12 +834,201 @@ client = Client(
|
|
|
803
834
|
headers={'Authorization': 'Bearer your-token'},
|
|
804
835
|
)
|
|
805
836
|
|
|
806
|
-
user =
|
|
837
|
+
user = get_user(user_id=123, client=client)
|
|
807
838
|
|
|
808
839
|
async def main():
|
|
809
|
-
user = await
|
|
840
|
+
user = await async_get_user(user_id=123, client=client)
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
Prefer a method-based API (`client.get_user(...)`)? Set
|
|
844
|
+
[`client_style`](#-client-style).
|
|
845
|
+
|
|
846
|
+
### 🎛 Client Style
|
|
847
|
+
|
|
848
|
+
`client_style` controls the shape of the generated public API.
|
|
849
|
+
|
|
850
|
+
**`functions` (default)** — a standalone function per endpoint and variant, plus
|
|
851
|
+
an infrastructure `Client` you pass in:
|
|
852
|
+
|
|
853
|
+
```python
|
|
854
|
+
from client import get_user, async_get_user, list_users_df
|
|
855
|
+
|
|
856
|
+
user = get_user(user_id=123)
|
|
857
|
+
df = list_users_df()
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
**`client`** — additionally generates `Client` and `AsyncClient` classes whose
|
|
861
|
+
methods delegate to those functions, giving a class-namespaced surface with clean
|
|
862
|
+
names (no `async_` prefix):
|
|
863
|
+
|
|
864
|
+
```yaml
|
|
865
|
+
documents:
|
|
866
|
+
- source: https://api.example.com/openapi.json
|
|
867
|
+
output: ./client
|
|
868
|
+
client_style: client
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
```python
|
|
872
|
+
from client import Client, AsyncClient
|
|
873
|
+
|
|
874
|
+
user = Client(base_url='https://api.example.com').get_user(user_id=123)
|
|
875
|
+
|
|
876
|
+
async def main():
|
|
877
|
+
async with AsyncClient() as api:
|
|
878
|
+
user = await api.get_user(user_id=123) # no async_ prefix
|
|
879
|
+
df = await api.list_users_df()
|
|
880
|
+
async for u in api.list_users_iter():
|
|
881
|
+
...
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
**`resource`** — groups the methods into resource sub-clients, derived from the
|
|
885
|
+
same strategy as [module splitting](#-module-splitting) (tags/path/custom). Only
|
|
886
|
+
the grouping changes: every method keeps its function's name (or, with
|
|
887
|
+
`resource_naming: operation_id`, the name its operationId declares), so adding
|
|
888
|
+
an operation to the spec never renames or reshapes a method you already call:
|
|
889
|
+
|
|
890
|
+
```yaml
|
|
891
|
+
documents:
|
|
892
|
+
- source: https://api.example.com/openapi.json
|
|
893
|
+
output: ./client
|
|
894
|
+
client_style: resource
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
```python
|
|
898
|
+
from client import Client, AsyncClient
|
|
899
|
+
|
|
900
|
+
client = Client(base_url='https://api.example.com')
|
|
901
|
+
user = client.users.get_user(user_id=123)
|
|
902
|
+
users = client.users.list_users()
|
|
903
|
+
order = client.orders.get_order(order_id=7)
|
|
904
|
+
|
|
905
|
+
async with AsyncClient() as api:
|
|
906
|
+
user = await api.users.get_user(user_id=123)
|
|
907
|
+
```
|
|
908
|
+
|
|
909
|
+
`resource_naming` controls how the sub-clients are derived and can **nest**:
|
|
910
|
+
|
|
911
|
+
```yaml
|
|
912
|
+
client_style: resource
|
|
913
|
+
resource_naming: path # or: operation_id
|
|
914
|
+
```
|
|
915
|
+
|
|
916
|
+
- `tag` (default) — one level, from the module-split strategy.
|
|
917
|
+
- `path` — nests by URL segments, one sub-client per segment:
|
|
918
|
+
`GET /identity/users/{id}` (operationId `getUser`) →
|
|
919
|
+
`client.identity.users.get_user(id)`, and `/pet/findByStatus` →
|
|
920
|
+
`client.pet.find_by_status.find_pets_by_status(...)`.
|
|
921
|
+
- `operation_id` — nests by a dotted (or `/`-separated) operationId, whose last
|
|
922
|
+
part is the method name the spec declares: `identity.users.get` →
|
|
923
|
+
`client.identity.users.get()` (variants keep their suffix: `get_df`). If two
|
|
924
|
+
operations in one resource declare the same name, both keep their full
|
|
925
|
+
function names. A flat operationId (no separator) stays on the client.
|
|
926
|
+
|
|
927
|
+
Segments are made into valid attribute names (`/2fa` → `client.n2fa`, a keyword
|
|
928
|
+
gets a trailing underscore: `/import` → `client.import_`).
|
|
929
|
+
|
|
930
|
+
```python
|
|
931
|
+
user = client.identity.users.get_user(user_id=123)
|
|
932
|
+
invoices = client.billing.invoices.list_invoices()
|
|
933
|
+
```
|
|
934
|
+
|
|
935
|
+
#### Result objects
|
|
936
|
+
|
|
937
|
+
With `result_objects: true` (with `client_style: client` or `resource`), every
|
|
938
|
+
**list** endpoint (one that returns `list[X]`) returns a deferred `Query` instead
|
|
939
|
+
of exposing separate `_df` / `_pl` / `_iter` / `_export` methods. Whether a method
|
|
940
|
+
returns a `Query` depends only on the endpoint's return type, never on which
|
|
941
|
+
features are enabled, so switching on DataFrames or export only adds terminals. The endpoint's arguments are captured
|
|
942
|
+
once; a terminal method materializes it. `Query` / `AsyncQuery` are exported from
|
|
943
|
+
the package for type hints:
|
|
944
|
+
|
|
945
|
+
```yaml
|
|
946
|
+
client_style: resource
|
|
947
|
+
result_objects: true
|
|
810
948
|
```
|
|
811
949
|
|
|
950
|
+
```python
|
|
951
|
+
q = client.users.list_users(status="active") # nothing sent yet
|
|
952
|
+
rows = q.all() # list[User]
|
|
953
|
+
df = q.to_pandas() # pandas DataFrame
|
|
954
|
+
for u in q.iter(): ... # streamed, page by page
|
|
955
|
+
q.export("users.csv") # write to a file
|
|
956
|
+
|
|
957
|
+
async with AsyncClient() as api:
|
|
958
|
+
rows = await api.users.list_users().all()
|
|
959
|
+
async for u in api.users.list_users().iter(): ...
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
Terminals whose feature wasn't enabled at generation time raise a clear error;
|
|
963
|
+
every other endpoint keeps returning its model directly, with its variants as
|
|
964
|
+
separate methods. Iterating a
|
|
965
|
+
result object directly (`for row in q` / `async for row in q`) streams pages
|
|
966
|
+
when pagination is enabled and otherwise walks the fetched list.
|
|
967
|
+
|
|
968
|
+
#### Composing your own SDK
|
|
969
|
+
|
|
970
|
+
To ship a user-facing SDK of your own, **compose** over a generated `Client`
|
|
971
|
+
rather than subclassing it: hold the generated client privately and expose
|
|
972
|
+
only the methods you choose. You own the public names, so a renamed
|
|
973
|
+
`operationId` in the spec stays an internal change instead of a breaking one,
|
|
974
|
+
and nothing the generator emits leaks into your API by accident.
|
|
975
|
+
|
|
976
|
+
The `client` style is the layout to compose over. Its flat `Client` /
|
|
977
|
+
`AsyncClient` reach every operation the `resource` style does, on both the sync
|
|
978
|
+
and the async side (the test suite asserts this), and with `result_objects: true`
|
|
979
|
+
each list endpoint hands you a `Query` whose terminals cover every enabled
|
|
980
|
+
feature -- so the facade loses nothing.
|
|
981
|
+
|
|
982
|
+
```yaml
|
|
983
|
+
documents:
|
|
984
|
+
- source: https://api.example.com/openapi.json
|
|
985
|
+
output: ./mysdk/_generated # private to your package
|
|
986
|
+
client_style: client
|
|
987
|
+
result_objects: true
|
|
988
|
+
pagination: { enabled: true }
|
|
989
|
+
dataframe: { enabled: true }
|
|
990
|
+
export: { enabled: true }
|
|
991
|
+
```
|
|
992
|
+
|
|
993
|
+
```python
|
|
994
|
+
from ._generated import AsyncClient, BaseAPIError, Client, Query, User
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
class DirectoryError(Exception): ...
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
class UserDirectory:
|
|
1001
|
+
"""Your public API. Only what is defined here is visible to users."""
|
|
1002
|
+
|
|
1003
|
+
def __init__(self, base_url: str, token: str):
|
|
1004
|
+
self._api = Client(base_url=base_url, headers={"Authorization": f"Bearer {token}"})
|
|
1005
|
+
|
|
1006
|
+
def active(self) -> Query[User]: # .all() / .iter() / .to_pandas() / .export()
|
|
1007
|
+
return self._api.list_users(status="active")
|
|
1008
|
+
|
|
1009
|
+
def by_id(self, user_id: int) -> User:
|
|
1010
|
+
try:
|
|
1011
|
+
return self._api.get_user(user_id)
|
|
1012
|
+
except BaseAPIError as e: # translate, don't leak
|
|
1013
|
+
raise DirectoryError(e.status_code) from e
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
The async facade is the same shape around `AsyncClient`, returning
|
|
1017
|
+
`AsyncQuery[User]`. Inject an `httpx2` client (`Client(http_client=...)`) to test
|
|
1018
|
+
the facade against a mock transport.
|
|
1019
|
+
|
|
1020
|
+
Two things to know when composing: `resource` is not a good base for a facade
|
|
1021
|
+
(its sub-client classes are private and rebuilt on every access, so they cannot
|
|
1022
|
+
be extended or injected), and a generated method whose name collides with a
|
|
1023
|
+
base-client member such as `close` is renamed with a trailing underscore --
|
|
1024
|
+
check the generated names before delegating to them.
|
|
1025
|
+
|
|
1026
|
+
The free functions remain as the implementation the methods call. `client` and
|
|
1027
|
+
`resource` compose with [module splitting](#-module-splitting): the functions are
|
|
1028
|
+
split across module files and each method routes to its function's module, while
|
|
1029
|
+
`Client` / `AsyncClient` stay the single public entry point. With `resource` the
|
|
1030
|
+
resources mirror the split modules.
|
|
1031
|
+
|
|
812
1032
|
### Working with Models
|
|
813
1033
|
|
|
814
1034
|
```python
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# 🦦 OtterAPI
|
|
2
2
|
|
|
3
|
-
> *A cute otter that turns OpenAPI & Swagger specs into type-safe, async Python clients — powered by
|
|
3
|
+
> *A cute otter that turns OpenAPI & Swagger specs into type-safe, async Python clients — powered by [httpx2](https://github.com/pydantic/httpx2) and Pydantic.*
|
|
4
4
|
|
|
5
|
-
**OtterAPI** is a sleek Python library that transforms OpenAPI specifications into clean, type-safe client code with Pydantic models and
|
|
5
|
+
**OtterAPI** is a sleek Python library that transforms OpenAPI specifications into clean, type-safe client code with Pydantic models and httpx2-based HTTP clients.
|
|
6
6
|
|
|
7
7
|
## ✨ Features
|
|
8
8
|
|
|
@@ -26,6 +26,12 @@
|
|
|
26
26
|
pip install otterapi
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
Generated clients use [httpx2](https://github.com/pydantic/httpx2), pydantic's
|
|
30
|
+
maintained fork of httpx (same API, package renamed), installed alongside
|
|
31
|
+
otterapi. A custom `http_client` / `async_http_client` you pass in must be an
|
|
32
|
+
`httpx2.Client` / `httpx2.AsyncClient`. Note that httpx2 verifies TLS against
|
|
33
|
+
the operating system's trust store (`truststore`) rather than `certifi`.
|
|
34
|
+
|
|
29
35
|
### Basic Usage
|
|
30
36
|
|
|
31
37
|
1. Create an `otter.yml` configuration file:
|
|
@@ -143,10 +149,13 @@ Each entry under `documents:` supports these fields:
|
|
|
143
149
|
| `generate_async` | bool | `true` | Generate async endpoint functions |
|
|
144
150
|
| `generate_sync` | bool | `true` | Generate sync endpoint functions |
|
|
145
151
|
| `client_class_name` | string | from API title | Override the generated client class name |
|
|
152
|
+
| `client_style` | `functions` \| `client` \| `resource` | `functions` | Shape of the public API. `functions` exposes a standalone function per endpoint/variant; `client` adds `Client` / `AsyncClient` classes with a flat method per endpoint; `resource` groups those methods into resource sub-clients (see [Client Style](#-client-style)) |
|
|
153
|
+
| `resource_naming` | `tag` \| `path` \| `operation_id` | `tag` | How resource sub-clients are derived for `client_style: resource`. `tag` uses the module-split strategy (one level); `path` nests by URL segments; `operation_id` nests by the dotted operationId hierarchy |
|
|
154
|
+
| `result_objects` | bool | `false` | With `client_style: client` or `resource`, a list endpoint returns a deferred `Query` object instead of separate `_df`/`_pl`/`_iter`/`_export` methods (see [Client Style](#-client-style)) |
|
|
146
155
|
| `function_naming` | `operation_id` \| `path` | `operation_id` | How to name endpoint functions. `path` derives names from the HTTP method and URL path — use it for specs that reuse one `operationId` across many paths |
|
|
147
156
|
| `include_paths` | list | `null` | Glob patterns — only matching paths are generated |
|
|
148
157
|
| `exclude_paths` | list | `null` | Glob patterns — matching paths are skipped (applied after `include_paths`) |
|
|
149
|
-
| `target_python` | `3.
|
|
158
|
+
| `target_python` | `3.11` … `3.14` | `3.11` | Oldest Python the generated code has to run on. `3.12`+ emits PEP 695 type parameters in the runtime helpers |
|
|
150
159
|
| `pydantic_version` | `1` \| `2` | `2` | Target Pydantic version for generated models and helpers |
|
|
151
160
|
|
|
152
161
|
#### Function Naming
|
|
@@ -182,7 +191,7 @@ Patterns follow standard glob syntax (`*` = single segment, `**` = any depth).
|
|
|
182
191
|
|
|
183
192
|
#### Target Python Version
|
|
184
193
|
|
|
185
|
-
Generated code defaults to running on Python 3.
|
|
194
|
+
Generated code defaults to running on Python 3.11, so the runtime helpers
|
|
186
195
|
(`_pagination.py`, `_concurrency.py`) declare their generics with module-level
|
|
187
196
|
`TypeVar`s:
|
|
188
197
|
|
|
@@ -405,7 +414,7 @@ module_split:
|
|
|
405
414
|
| `min_endpoints` | int | `2` | Minimum endpoints per module before consolidating into fallback |
|
|
406
415
|
| `flat_structure` | bool | `false` | Flat files instead of nested directories |
|
|
407
416
|
| `path_depth` | int | `1` | Path segments used for `path` strategy (1–5) |
|
|
408
|
-
| `global_strip_prefixes` | list | `/api`, `/api/v1`, `/api/v2`, `/api/v3` | Prefixes stripped from all paths before matching |
|
|
417
|
+
| `global_strip_prefixes` | list | `/api`, `/api/v1`, `/api/v2`, `/api/v3` | Prefixes stripped from all paths before matching (whole segments only: `/api` strips `/api/users` but leaves `/apikeys` alone) |
|
|
409
418
|
| `module_map` | object | `{}` | Custom module-to-path mappings |
|
|
410
419
|
| `split_models` | bool | `false` | Generate per-module model files instead of one shared `models.py` |
|
|
411
420
|
| `shared_models_module` | string | `_models` | Module name for shared models when `split_models` is `true` |
|
|
@@ -675,14 +684,35 @@ export:
|
|
|
675
684
|
|
|
676
685
|
### Usage
|
|
677
686
|
|
|
687
|
+
Every endpoint returning a list of models gets an `<endpoint>_export` function
|
|
688
|
+
(and an `async_<endpoint>_export` twin) that mirrors the endpoint's own
|
|
689
|
+
parameters and adds `output_path` first, plus keyword-only `format` and
|
|
690
|
+
`batch_size`. Extra keyword arguments are passed through to the file writer.
|
|
691
|
+
A list of a union of models -- such as a `oneOf` with a `discriminator` --
|
|
692
|
+
exports too: the file has every variant's columns, and each row fills its own
|
|
693
|
+
(CSV / TSV cells and Parquet values of the others stay empty; JSONL keeps each
|
|
694
|
+
row's shape). Lists of plain values (`list[str]`) have no columns, so they get
|
|
695
|
+
no export function.
|
|
696
|
+
|
|
678
697
|
```python
|
|
679
|
-
from client import
|
|
698
|
+
from client import list_users_export, async_list_users_export
|
|
680
699
|
|
|
681
|
-
#
|
|
682
|
-
|
|
683
|
-
|
|
700
|
+
rows = list_users_export("output/users.csv", status="active") # returns the row count
|
|
701
|
+
list_users_export("s3://my-bucket/users.parquet", format="parquet") # UPath remote targets work too
|
|
702
|
+
list_users_export("out.csv", format="csv", delimiter=";") # writer options pass through
|
|
684
703
|
```
|
|
685
704
|
|
|
705
|
+
For a [paginated](#-pagination) endpoint the export wrapper streams through the
|
|
706
|
+
endpoint's `_iter` variant and declares exactly its knobs -- the starting
|
|
707
|
+
position (`offset` / `cursor` / `page`), `page_size` and `max_items` -- so
|
|
708
|
+
`list_users_export("out.csv", offset=100)` starts where
|
|
709
|
+
`list_users_iter(offset=100)` does.
|
|
710
|
+
|
|
711
|
+
If an endpoint has its own parameter named `format`, `output_path` or
|
|
712
|
+
`batch_size`, the wrapper exposes it as `format_` (etc.) and forwards it to the
|
|
713
|
+
API under its real name; with `result_objects`, `Query.export()` applies the
|
|
714
|
+
same aliasing for you.
|
|
715
|
+
|
|
686
716
|
---
|
|
687
717
|
|
|
688
718
|
## 🔓 Response Unwrapping
|
|
@@ -761,8 +791,11 @@ from client.orders import list_orders, get_order
|
|
|
761
791
|
|
|
762
792
|
### Using the Client Class
|
|
763
793
|
|
|
794
|
+
In the default `functions` style the `Client` carries configuration and the
|
|
795
|
+
connection pool; you pass it to each endpoint function via `client=`:
|
|
796
|
+
|
|
764
797
|
```python
|
|
765
|
-
from client import Client
|
|
798
|
+
from client import Client, get_user, async_get_user
|
|
766
799
|
|
|
767
800
|
client = Client(
|
|
768
801
|
base_url='https://api.example.com',
|
|
@@ -770,12 +803,201 @@ client = Client(
|
|
|
770
803
|
headers={'Authorization': 'Bearer your-token'},
|
|
771
804
|
)
|
|
772
805
|
|
|
773
|
-
user =
|
|
806
|
+
user = get_user(user_id=123, client=client)
|
|
774
807
|
|
|
775
808
|
async def main():
|
|
776
|
-
user = await
|
|
809
|
+
user = await async_get_user(user_id=123, client=client)
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
Prefer a method-based API (`client.get_user(...)`)? Set
|
|
813
|
+
[`client_style`](#-client-style).
|
|
814
|
+
|
|
815
|
+
### 🎛 Client Style
|
|
816
|
+
|
|
817
|
+
`client_style` controls the shape of the generated public API.
|
|
818
|
+
|
|
819
|
+
**`functions` (default)** — a standalone function per endpoint and variant, plus
|
|
820
|
+
an infrastructure `Client` you pass in:
|
|
821
|
+
|
|
822
|
+
```python
|
|
823
|
+
from client import get_user, async_get_user, list_users_df
|
|
824
|
+
|
|
825
|
+
user = get_user(user_id=123)
|
|
826
|
+
df = list_users_df()
|
|
827
|
+
```
|
|
828
|
+
|
|
829
|
+
**`client`** — additionally generates `Client` and `AsyncClient` classes whose
|
|
830
|
+
methods delegate to those functions, giving a class-namespaced surface with clean
|
|
831
|
+
names (no `async_` prefix):
|
|
832
|
+
|
|
833
|
+
```yaml
|
|
834
|
+
documents:
|
|
835
|
+
- source: https://api.example.com/openapi.json
|
|
836
|
+
output: ./client
|
|
837
|
+
client_style: client
|
|
838
|
+
```
|
|
839
|
+
|
|
840
|
+
```python
|
|
841
|
+
from client import Client, AsyncClient
|
|
842
|
+
|
|
843
|
+
user = Client(base_url='https://api.example.com').get_user(user_id=123)
|
|
844
|
+
|
|
845
|
+
async def main():
|
|
846
|
+
async with AsyncClient() as api:
|
|
847
|
+
user = await api.get_user(user_id=123) # no async_ prefix
|
|
848
|
+
df = await api.list_users_df()
|
|
849
|
+
async for u in api.list_users_iter():
|
|
850
|
+
...
|
|
851
|
+
```
|
|
852
|
+
|
|
853
|
+
**`resource`** — groups the methods into resource sub-clients, derived from the
|
|
854
|
+
same strategy as [module splitting](#-module-splitting) (tags/path/custom). Only
|
|
855
|
+
the grouping changes: every method keeps its function's name (or, with
|
|
856
|
+
`resource_naming: operation_id`, the name its operationId declares), so adding
|
|
857
|
+
an operation to the spec never renames or reshapes a method you already call:
|
|
858
|
+
|
|
859
|
+
```yaml
|
|
860
|
+
documents:
|
|
861
|
+
- source: https://api.example.com/openapi.json
|
|
862
|
+
output: ./client
|
|
863
|
+
client_style: resource
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
```python
|
|
867
|
+
from client import Client, AsyncClient
|
|
868
|
+
|
|
869
|
+
client = Client(base_url='https://api.example.com')
|
|
870
|
+
user = client.users.get_user(user_id=123)
|
|
871
|
+
users = client.users.list_users()
|
|
872
|
+
order = client.orders.get_order(order_id=7)
|
|
873
|
+
|
|
874
|
+
async with AsyncClient() as api:
|
|
875
|
+
user = await api.users.get_user(user_id=123)
|
|
876
|
+
```
|
|
877
|
+
|
|
878
|
+
`resource_naming` controls how the sub-clients are derived and can **nest**:
|
|
879
|
+
|
|
880
|
+
```yaml
|
|
881
|
+
client_style: resource
|
|
882
|
+
resource_naming: path # or: operation_id
|
|
883
|
+
```
|
|
884
|
+
|
|
885
|
+
- `tag` (default) — one level, from the module-split strategy.
|
|
886
|
+
- `path` — nests by URL segments, one sub-client per segment:
|
|
887
|
+
`GET /identity/users/{id}` (operationId `getUser`) →
|
|
888
|
+
`client.identity.users.get_user(id)`, and `/pet/findByStatus` →
|
|
889
|
+
`client.pet.find_by_status.find_pets_by_status(...)`.
|
|
890
|
+
- `operation_id` — nests by a dotted (or `/`-separated) operationId, whose last
|
|
891
|
+
part is the method name the spec declares: `identity.users.get` →
|
|
892
|
+
`client.identity.users.get()` (variants keep their suffix: `get_df`). If two
|
|
893
|
+
operations in one resource declare the same name, both keep their full
|
|
894
|
+
function names. A flat operationId (no separator) stays on the client.
|
|
895
|
+
|
|
896
|
+
Segments are made into valid attribute names (`/2fa` → `client.n2fa`, a keyword
|
|
897
|
+
gets a trailing underscore: `/import` → `client.import_`).
|
|
898
|
+
|
|
899
|
+
```python
|
|
900
|
+
user = client.identity.users.get_user(user_id=123)
|
|
901
|
+
invoices = client.billing.invoices.list_invoices()
|
|
902
|
+
```
|
|
903
|
+
|
|
904
|
+
#### Result objects
|
|
905
|
+
|
|
906
|
+
With `result_objects: true` (with `client_style: client` or `resource`), every
|
|
907
|
+
**list** endpoint (one that returns `list[X]`) returns a deferred `Query` instead
|
|
908
|
+
of exposing separate `_df` / `_pl` / `_iter` / `_export` methods. Whether a method
|
|
909
|
+
returns a `Query` depends only on the endpoint's return type, never on which
|
|
910
|
+
features are enabled, so switching on DataFrames or export only adds terminals. The endpoint's arguments are captured
|
|
911
|
+
once; a terminal method materializes it. `Query` / `AsyncQuery` are exported from
|
|
912
|
+
the package for type hints:
|
|
913
|
+
|
|
914
|
+
```yaml
|
|
915
|
+
client_style: resource
|
|
916
|
+
result_objects: true
|
|
777
917
|
```
|
|
778
918
|
|
|
919
|
+
```python
|
|
920
|
+
q = client.users.list_users(status="active") # nothing sent yet
|
|
921
|
+
rows = q.all() # list[User]
|
|
922
|
+
df = q.to_pandas() # pandas DataFrame
|
|
923
|
+
for u in q.iter(): ... # streamed, page by page
|
|
924
|
+
q.export("users.csv") # write to a file
|
|
925
|
+
|
|
926
|
+
async with AsyncClient() as api:
|
|
927
|
+
rows = await api.users.list_users().all()
|
|
928
|
+
async for u in api.users.list_users().iter(): ...
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
Terminals whose feature wasn't enabled at generation time raise a clear error;
|
|
932
|
+
every other endpoint keeps returning its model directly, with its variants as
|
|
933
|
+
separate methods. Iterating a
|
|
934
|
+
result object directly (`for row in q` / `async for row in q`) streams pages
|
|
935
|
+
when pagination is enabled and otherwise walks the fetched list.
|
|
936
|
+
|
|
937
|
+
#### Composing your own SDK
|
|
938
|
+
|
|
939
|
+
To ship a user-facing SDK of your own, **compose** over a generated `Client`
|
|
940
|
+
rather than subclassing it: hold the generated client privately and expose
|
|
941
|
+
only the methods you choose. You own the public names, so a renamed
|
|
942
|
+
`operationId` in the spec stays an internal change instead of a breaking one,
|
|
943
|
+
and nothing the generator emits leaks into your API by accident.
|
|
944
|
+
|
|
945
|
+
The `client` style is the layout to compose over. Its flat `Client` /
|
|
946
|
+
`AsyncClient` reach every operation the `resource` style does, on both the sync
|
|
947
|
+
and the async side (the test suite asserts this), and with `result_objects: true`
|
|
948
|
+
each list endpoint hands you a `Query` whose terminals cover every enabled
|
|
949
|
+
feature -- so the facade loses nothing.
|
|
950
|
+
|
|
951
|
+
```yaml
|
|
952
|
+
documents:
|
|
953
|
+
- source: https://api.example.com/openapi.json
|
|
954
|
+
output: ./mysdk/_generated # private to your package
|
|
955
|
+
client_style: client
|
|
956
|
+
result_objects: true
|
|
957
|
+
pagination: { enabled: true }
|
|
958
|
+
dataframe: { enabled: true }
|
|
959
|
+
export: { enabled: true }
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
```python
|
|
963
|
+
from ._generated import AsyncClient, BaseAPIError, Client, Query, User
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
class DirectoryError(Exception): ...
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
class UserDirectory:
|
|
970
|
+
"""Your public API. Only what is defined here is visible to users."""
|
|
971
|
+
|
|
972
|
+
def __init__(self, base_url: str, token: str):
|
|
973
|
+
self._api = Client(base_url=base_url, headers={"Authorization": f"Bearer {token}"})
|
|
974
|
+
|
|
975
|
+
def active(self) -> Query[User]: # .all() / .iter() / .to_pandas() / .export()
|
|
976
|
+
return self._api.list_users(status="active")
|
|
977
|
+
|
|
978
|
+
def by_id(self, user_id: int) -> User:
|
|
979
|
+
try:
|
|
980
|
+
return self._api.get_user(user_id)
|
|
981
|
+
except BaseAPIError as e: # translate, don't leak
|
|
982
|
+
raise DirectoryError(e.status_code) from e
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
The async facade is the same shape around `AsyncClient`, returning
|
|
986
|
+
`AsyncQuery[User]`. Inject an `httpx2` client (`Client(http_client=...)`) to test
|
|
987
|
+
the facade against a mock transport.
|
|
988
|
+
|
|
989
|
+
Two things to know when composing: `resource` is not a good base for a facade
|
|
990
|
+
(its sub-client classes are private and rebuilt on every access, so they cannot
|
|
991
|
+
be extended or injected), and a generated method whose name collides with a
|
|
992
|
+
base-client member such as `close` is renamed with a trailing underscore --
|
|
993
|
+
check the generated names before delegating to them.
|
|
994
|
+
|
|
995
|
+
The free functions remain as the implementation the methods call. `client` and
|
|
996
|
+
`resource` compose with [module splitting](#-module-splitting): the functions are
|
|
997
|
+
split across module files and each method routes to its function's module, while
|
|
998
|
+
`Client` / `AsyncClient` stay the single public entry point. With `resource` the
|
|
999
|
+
resources mirror the split modules.
|
|
1000
|
+
|
|
779
1001
|
### Working with Models
|
|
780
1002
|
|
|
781
1003
|
```python
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
OtterAPI is a code generation library that creates fully-typed Python client
|
|
4
4
|
code from OpenAPI 3.x specifications. It generates Pydantic models for data
|
|
5
|
-
validation and
|
|
5
|
+
validation and httpx2-based endpoint functions for making API requests.
|
|
6
6
|
|
|
7
7
|
Quick Start:
|
|
8
8
|
>>> from otterapi import Codegen, DocumentConfig
|
|
@@ -48,7 +48,7 @@ def _commented_optional_sections() -> str:
|
|
|
48
48
|
#
|
|
49
49
|
# # Oldest Python the generated code must run on. From "3.12" on, the
|
|
50
50
|
# # runtime helpers use PEP 695 type parameters instead of TypeVars.
|
|
51
|
-
# target_python: "3.
|
|
51
|
+
# target_python: "3.11"
|
|
52
52
|
#
|
|
53
53
|
# # Pagination: emit *_iter() helpers that stream paginated endpoints.
|
|
54
54
|
# # auto_detect=true picks up offset/limit, cursor/limit, page/per_page
|