otterapi 0.1.14__tar.gz → 0.2.1__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.1}/PKG-INFO +253 -17
- {otterapi-0.1.14 → otterapi-0.2.1}/README.md +250 -12
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/__init__.py +1 -1
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/cli.py +1 -1
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/_body_builder.py +1 -6
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/_features.py +17 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/_pep695.py +1 -1
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/ast_utils.py +72 -27
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/client.py +33 -54
- otterapi-0.2.1/otterapi/codegen/client_layout.py +1009 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/codegen.py +263 -818
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/dataframes.py +3 -0
- otterapi-0.2.1/otterapi/codegen/emit.py +1051 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/endpoints.py +92 -42
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/export.py +127 -17
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_export.py +85 -41
- otterapi-0.2.1/otterapi/codegen/runtime/_query.py +185 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_serialization.py +87 -6
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/schema.py +4 -4
- otterapi-0.2.1/otterapi/codegen/splitting.py +1065 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/types.py +392 -59
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/utils.py +88 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/config.py +72 -13
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v2/v2.py +9 -9
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3/v3.py +1 -1
- otterapi-0.2.1/otterapi/tests/fixtures/golden/envelope/spec.yaml +34 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/envelope_optional/spec.yaml +37 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/nested/spec.yaml +63 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/petstore/spec.json +1 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/petstore_v2/spec.json +1054 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/real_world_patterns/spec.yaml +82 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/shadow/spec.yaml +17 -0
- otterapi-0.2.1/otterapi/tests/fixtures/golden/tagged/spec.yaml +83 -0
- otterapi-0.2.1/otterapi/tests/fixtures/real_specs/README.md +13 -0
- otterapi-0.2.1/otterapi/tests/fixtures/real_specs/github.openapi.json.gz +0 -0
- otterapi-0.2.1/otterapi/tests/fixtures/real_specs/stripe.openapi.json.gz +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_auth.py +6 -6
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_body_flatten.py +6 -6
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_client_retry_lifecycle.py +30 -30
- otterapi-0.2.1/otterapi/tests/test_client_style.py +1812 -0
- otterapi-0.2.1/otterapi/tests/test_codegen_consolidation.py +138 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_codegen_edge_cases.py +2 -2
- otterapi-0.2.1/otterapi/tests/test_codegen_regressions.py +215 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_dataframe.py +4 -4
- otterapi-0.2.1/otterapi/tests/test_discriminated_union_runtime.py +256 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_error_response_types.py +9 -9
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_export.py +36 -18
- otterapi-0.2.1/otterapi/tests/test_generated_correctness.py +562 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_generated_hygiene.py +1 -1
- otterapi-0.2.1/otterapi/tests/test_generated_typecheck.py +165 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_openapi_support.py +2 -2
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_pep695.py +3 -3
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_per_status_errors.py +7 -7
- otterapi-0.2.1/otterapi/tests/test_real_specs_smoke.py +136 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_smoke_generated_client.py +10 -10
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_splitting_resolver.py +19 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_wire_format.py +156 -16
- {otterapi-0.1.14 → otterapi-0.2.1}/pyproject.toml +10 -6
- otterapi-0.1.14/otterapi/codegen/splitting.py +0 -1801
- {otterapi-0.1.14 → otterapi-0.2.1}/.gitignore +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/LICENSE +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/__main__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/_openapi_adapter.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/auth.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/pagination.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_client_stub.py.tpl +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_concurrency.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_dataframe.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_models_mixin.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_pagination.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/codegen/runtime/_retry.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/exceptions.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/_lenient.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/constants.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v2/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v2/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3_1/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3_1/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3_1/v3_1.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3_2/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3_2/spec.json +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/v3_2/v3_2.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/openapi/warnings.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/py.typed +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/__init__.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/golden/constraints/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/golden/dataframe/config.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/golden/dataframe/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/golden/discriminator/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/golden/paginated/config.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/fixtures/golden/paginated/spec.yaml +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_ast_utils.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_atomic_generation.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_body_builder.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_codegen.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_codegen_structure.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_concurrency_runtime.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_config_errors.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_discriminated_unions.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_empty_schema.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_exceptions.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_extra_forbid.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_feature_modules.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_html_repr.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_init_scaffold.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_lenient.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_media_type_parameters.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_not_schema.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_nullable_upgrade.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_openapi_adapter.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_openapi_upgrade.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_pagination.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_public_api.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_recursive_schema.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_retry_runtime.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_runtime_guards.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_schema_constraints.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_splitting_config.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_splitting_integration.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/otterapi/tests/test_splitting_tree.py +0 -0
- {otterapi-0.1.14 → otterapi-0.2.1}/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.1
|
|
3
|
+
Version: 0.2.1
|
|
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,14 @@ 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`. To
|
|
65
|
+
generate clients that use the original `httpx` package instead, set
|
|
66
|
+
`http_library: httpx` (see [Document Options](#document-options)).
|
|
67
|
+
|
|
62
68
|
### Basic Usage
|
|
63
69
|
|
|
64
70
|
1. Create an `otter.yml` configuration file:
|
|
@@ -176,10 +182,14 @@ Each entry under `documents:` supports these fields:
|
|
|
176
182
|
| `generate_async` | bool | `true` | Generate async endpoint functions |
|
|
177
183
|
| `generate_sync` | bool | `true` | Generate sync endpoint functions |
|
|
178
184
|
| `client_class_name` | string | from API title | Override the generated client class name |
|
|
185
|
+
| `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)) |
|
|
186
|
+
| `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 |
|
|
187
|
+
| `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
188
|
| `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
189
|
| `include_paths` | list | `null` | Glob patterns — only matching paths are generated |
|
|
181
190
|
| `exclude_paths` | list | `null` | Glob patterns — matching paths are skipped (applied after `include_paths`) |
|
|
182
|
-
| `target_python` | `3.
|
|
191
|
+
| `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 |
|
|
192
|
+
| `http_library` | `httpx2` \| `httpx` | `httpx2` | HTTP library the generated client imports. Use `httpx` if your project already depends on the original package (install it yourself; otterapi only ships `httpx2`) |
|
|
183
193
|
| `pydantic_version` | `1` \| `2` | `2` | Target Pydantic version for generated models and helpers |
|
|
184
194
|
|
|
185
195
|
#### Function Naming
|
|
@@ -215,7 +225,7 @@ Patterns follow standard glob syntax (`*` = single segment, `**` = any depth).
|
|
|
215
225
|
|
|
216
226
|
#### Target Python Version
|
|
217
227
|
|
|
218
|
-
Generated code defaults to running on Python 3.
|
|
228
|
+
Generated code defaults to running on Python 3.11, so the runtime helpers
|
|
219
229
|
(`_pagination.py`, `_concurrency.py`) declare their generics with module-level
|
|
220
230
|
`TypeVar`s:
|
|
221
231
|
|
|
@@ -438,7 +448,7 @@ module_split:
|
|
|
438
448
|
| `min_endpoints` | int | `2` | Minimum endpoints per module before consolidating into fallback |
|
|
439
449
|
| `flat_structure` | bool | `false` | Flat files instead of nested directories |
|
|
440
450
|
| `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 |
|
|
451
|
+
| `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
452
|
| `module_map` | object | `{}` | Custom module-to-path mappings |
|
|
443
453
|
| `split_models` | bool | `false` | Generate per-module model files instead of one shared `models.py` |
|
|
444
454
|
| `shared_models_module` | string | `_models` | Module name for shared models when `split_models` is `true` |
|
|
@@ -708,14 +718,35 @@ export:
|
|
|
708
718
|
|
|
709
719
|
### Usage
|
|
710
720
|
|
|
721
|
+
Every endpoint returning a list of models gets an `<endpoint>_export` function
|
|
722
|
+
(and an `async_<endpoint>_export` twin) that mirrors the endpoint's own
|
|
723
|
+
parameters and adds `output_path` first, plus keyword-only `format` and
|
|
724
|
+
`batch_size`. Extra keyword arguments are passed through to the file writer.
|
|
725
|
+
A list of a union of models -- such as a `oneOf` with a `discriminator` --
|
|
726
|
+
exports too: the file has every variant's columns, and each row fills its own
|
|
727
|
+
(CSV / TSV cells and Parquet values of the others stay empty; JSONL keeps each
|
|
728
|
+
row's shape). Lists of plain values (`list[str]`) have no columns, so they get
|
|
729
|
+
no export function.
|
|
730
|
+
|
|
711
731
|
```python
|
|
712
|
-
from client import
|
|
732
|
+
from client import list_users_export, async_list_users_export
|
|
713
733
|
|
|
714
|
-
#
|
|
715
|
-
|
|
716
|
-
|
|
734
|
+
rows = list_users_export("output/users.csv", status="active") # returns the row count
|
|
735
|
+
list_users_export("s3://my-bucket/users.parquet", format="parquet") # UPath remote targets work too
|
|
736
|
+
list_users_export("out.csv", format="csv", delimiter=";") # writer options pass through
|
|
717
737
|
```
|
|
718
738
|
|
|
739
|
+
For a [paginated](#-pagination) endpoint the export wrapper streams through the
|
|
740
|
+
endpoint's `_iter` variant and declares exactly its knobs -- the starting
|
|
741
|
+
position (`offset` / `cursor` / `page`), `page_size` and `max_items` -- so
|
|
742
|
+
`list_users_export("out.csv", offset=100)` starts where
|
|
743
|
+
`list_users_iter(offset=100)` does.
|
|
744
|
+
|
|
745
|
+
If an endpoint has its own parameter named `format`, `output_path` or
|
|
746
|
+
`batch_size`, the wrapper exposes it as `format_` (etc.) and forwards it to the
|
|
747
|
+
API under its real name; with `result_objects`, `Query.export()` applies the
|
|
748
|
+
same aliasing for you.
|
|
749
|
+
|
|
719
750
|
---
|
|
720
751
|
|
|
721
752
|
## 🔓 Response Unwrapping
|
|
@@ -794,8 +825,11 @@ from client.orders import list_orders, get_order
|
|
|
794
825
|
|
|
795
826
|
### Using the Client Class
|
|
796
827
|
|
|
828
|
+
In the default `functions` style the `Client` carries configuration and the
|
|
829
|
+
connection pool; you pass it to each endpoint function via `client=`:
|
|
830
|
+
|
|
797
831
|
```python
|
|
798
|
-
from client import Client
|
|
832
|
+
from client import Client, get_user, async_get_user
|
|
799
833
|
|
|
800
834
|
client = Client(
|
|
801
835
|
base_url='https://api.example.com',
|
|
@@ -803,12 +837,201 @@ client = Client(
|
|
|
803
837
|
headers={'Authorization': 'Bearer your-token'},
|
|
804
838
|
)
|
|
805
839
|
|
|
806
|
-
user =
|
|
840
|
+
user = get_user(user_id=123, client=client)
|
|
807
841
|
|
|
808
842
|
async def main():
|
|
809
|
-
user = await
|
|
843
|
+
user = await async_get_user(user_id=123, client=client)
|
|
844
|
+
```
|
|
845
|
+
|
|
846
|
+
Prefer a method-based API (`client.get_user(...)`)? Set
|
|
847
|
+
[`client_style`](#-client-style).
|
|
848
|
+
|
|
849
|
+
### 🎛 Client Style
|
|
850
|
+
|
|
851
|
+
`client_style` controls the shape of the generated public API.
|
|
852
|
+
|
|
853
|
+
**`functions` (default)** — a standalone function per endpoint and variant, plus
|
|
854
|
+
an infrastructure `Client` you pass in:
|
|
855
|
+
|
|
856
|
+
```python
|
|
857
|
+
from client import get_user, async_get_user, list_users_df
|
|
858
|
+
|
|
859
|
+
user = get_user(user_id=123)
|
|
860
|
+
df = list_users_df()
|
|
810
861
|
```
|
|
811
862
|
|
|
863
|
+
**`client`** — additionally generates `Client` and `AsyncClient` classes whose
|
|
864
|
+
methods delegate to those functions, giving a class-namespaced surface with clean
|
|
865
|
+
names (no `async_` prefix):
|
|
866
|
+
|
|
867
|
+
```yaml
|
|
868
|
+
documents:
|
|
869
|
+
- source: https://api.example.com/openapi.json
|
|
870
|
+
output: ./client
|
|
871
|
+
client_style: client
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
```python
|
|
875
|
+
from client import Client, AsyncClient
|
|
876
|
+
|
|
877
|
+
user = Client(base_url='https://api.example.com').get_user(user_id=123)
|
|
878
|
+
|
|
879
|
+
async def main():
|
|
880
|
+
async with AsyncClient() as api:
|
|
881
|
+
user = await api.get_user(user_id=123) # no async_ prefix
|
|
882
|
+
df = await api.list_users_df()
|
|
883
|
+
async for u in api.list_users_iter():
|
|
884
|
+
...
|
|
885
|
+
```
|
|
886
|
+
|
|
887
|
+
**`resource`** — groups the methods into resource sub-clients, derived from the
|
|
888
|
+
same strategy as [module splitting](#-module-splitting) (tags/path/custom). Only
|
|
889
|
+
the grouping changes: every method keeps its function's name (or, with
|
|
890
|
+
`resource_naming: operation_id`, the name its operationId declares), so adding
|
|
891
|
+
an operation to the spec never renames or reshapes a method you already call:
|
|
892
|
+
|
|
893
|
+
```yaml
|
|
894
|
+
documents:
|
|
895
|
+
- source: https://api.example.com/openapi.json
|
|
896
|
+
output: ./client
|
|
897
|
+
client_style: resource
|
|
898
|
+
```
|
|
899
|
+
|
|
900
|
+
```python
|
|
901
|
+
from client import Client, AsyncClient
|
|
902
|
+
|
|
903
|
+
client = Client(base_url='https://api.example.com')
|
|
904
|
+
user = client.users.get_user(user_id=123)
|
|
905
|
+
users = client.users.list_users()
|
|
906
|
+
order = client.orders.get_order(order_id=7)
|
|
907
|
+
|
|
908
|
+
async with AsyncClient() as api:
|
|
909
|
+
user = await api.users.get_user(user_id=123)
|
|
910
|
+
```
|
|
911
|
+
|
|
912
|
+
`resource_naming` controls how the sub-clients are derived and can **nest**:
|
|
913
|
+
|
|
914
|
+
```yaml
|
|
915
|
+
client_style: resource
|
|
916
|
+
resource_naming: path # or: operation_id
|
|
917
|
+
```
|
|
918
|
+
|
|
919
|
+
- `tag` (default) — one level, from the module-split strategy.
|
|
920
|
+
- `path` — nests by URL segments, one sub-client per segment:
|
|
921
|
+
`GET /identity/users/{id}` (operationId `getUser`) →
|
|
922
|
+
`client.identity.users.get_user(id)`, and `/pet/findByStatus` →
|
|
923
|
+
`client.pet.find_by_status.find_pets_by_status(...)`.
|
|
924
|
+
- `operation_id` — nests by a dotted (or `/`-separated) operationId, whose last
|
|
925
|
+
part is the method name the spec declares: `identity.users.get` →
|
|
926
|
+
`client.identity.users.get()` (variants keep their suffix: `get_df`). If two
|
|
927
|
+
operations in one resource declare the same name, both keep their full
|
|
928
|
+
function names. A flat operationId (no separator) stays on the client.
|
|
929
|
+
|
|
930
|
+
Segments are made into valid attribute names (`/2fa` → `client.n2fa`, a keyword
|
|
931
|
+
gets a trailing underscore: `/import` → `client.import_`).
|
|
932
|
+
|
|
933
|
+
```python
|
|
934
|
+
user = client.identity.users.get_user(user_id=123)
|
|
935
|
+
invoices = client.billing.invoices.list_invoices()
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
#### Result objects
|
|
939
|
+
|
|
940
|
+
With `result_objects: true` (with `client_style: client` or `resource`), every
|
|
941
|
+
**list** endpoint (one that returns `list[X]`) returns a deferred `Query` instead
|
|
942
|
+
of exposing separate `_df` / `_pl` / `_iter` / `_export` methods. Whether a method
|
|
943
|
+
returns a `Query` depends only on the endpoint's return type, never on which
|
|
944
|
+
features are enabled, so switching on DataFrames or export only adds terminals. The endpoint's arguments are captured
|
|
945
|
+
once; a terminal method materializes it. `Query` / `AsyncQuery` are exported from
|
|
946
|
+
the package for type hints:
|
|
947
|
+
|
|
948
|
+
```yaml
|
|
949
|
+
client_style: resource
|
|
950
|
+
result_objects: true
|
|
951
|
+
```
|
|
952
|
+
|
|
953
|
+
```python
|
|
954
|
+
q = client.users.list_users(status="active") # nothing sent yet
|
|
955
|
+
rows = q.all() # list[User]
|
|
956
|
+
df = q.to_pandas() # pandas DataFrame
|
|
957
|
+
for u in q.iter(): ... # streamed, page by page
|
|
958
|
+
q.export("users.csv") # write to a file
|
|
959
|
+
|
|
960
|
+
async with AsyncClient() as api:
|
|
961
|
+
rows = await api.users.list_users().all()
|
|
962
|
+
async for u in api.users.list_users().iter(): ...
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
Terminals whose feature wasn't enabled at generation time raise a clear error;
|
|
966
|
+
every other endpoint keeps returning its model directly, with its variants as
|
|
967
|
+
separate methods. Iterating a
|
|
968
|
+
result object directly (`for row in q` / `async for row in q`) streams pages
|
|
969
|
+
when pagination is enabled and otherwise walks the fetched list.
|
|
970
|
+
|
|
971
|
+
#### Composing your own SDK
|
|
972
|
+
|
|
973
|
+
To ship a user-facing SDK of your own, **compose** over a generated `Client`
|
|
974
|
+
rather than subclassing it: hold the generated client privately and expose
|
|
975
|
+
only the methods you choose. You own the public names, so a renamed
|
|
976
|
+
`operationId` in the spec stays an internal change instead of a breaking one,
|
|
977
|
+
and nothing the generator emits leaks into your API by accident.
|
|
978
|
+
|
|
979
|
+
The `client` style is the layout to compose over. Its flat `Client` /
|
|
980
|
+
`AsyncClient` reach every operation the `resource` style does, on both the sync
|
|
981
|
+
and the async side (the test suite asserts this), and with `result_objects: true`
|
|
982
|
+
each list endpoint hands you a `Query` whose terminals cover every enabled
|
|
983
|
+
feature -- so the facade loses nothing.
|
|
984
|
+
|
|
985
|
+
```yaml
|
|
986
|
+
documents:
|
|
987
|
+
- source: https://api.example.com/openapi.json
|
|
988
|
+
output: ./mysdk/_generated # private to your package
|
|
989
|
+
client_style: client
|
|
990
|
+
result_objects: true
|
|
991
|
+
pagination: { enabled: true }
|
|
992
|
+
dataframe: { enabled: true }
|
|
993
|
+
export: { enabled: true }
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
```python
|
|
997
|
+
from ._generated import AsyncClient, BaseAPIError, Client, Query, User
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
class DirectoryError(Exception): ...
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
class UserDirectory:
|
|
1004
|
+
"""Your public API. Only what is defined here is visible to users."""
|
|
1005
|
+
|
|
1006
|
+
def __init__(self, base_url: str, token: str):
|
|
1007
|
+
self._api = Client(base_url=base_url, headers={"Authorization": f"Bearer {token}"})
|
|
1008
|
+
|
|
1009
|
+
def active(self) -> Query[User]: # .all() / .iter() / .to_pandas() / .export()
|
|
1010
|
+
return self._api.list_users(status="active")
|
|
1011
|
+
|
|
1012
|
+
def by_id(self, user_id: int) -> User:
|
|
1013
|
+
try:
|
|
1014
|
+
return self._api.get_user(user_id)
|
|
1015
|
+
except BaseAPIError as e: # translate, don't leak
|
|
1016
|
+
raise DirectoryError(e.status_code) from e
|
|
1017
|
+
```
|
|
1018
|
+
|
|
1019
|
+
The async facade is the same shape around `AsyncClient`, returning
|
|
1020
|
+
`AsyncQuery[User]`. Inject an `httpx2` client (`Client(http_client=...)`) to test
|
|
1021
|
+
the facade against a mock transport.
|
|
1022
|
+
|
|
1023
|
+
Two things to know when composing: `resource` is not a good base for a facade
|
|
1024
|
+
(its sub-client classes are private and rebuilt on every access, so they cannot
|
|
1025
|
+
be extended or injected), and a generated method whose name collides with a
|
|
1026
|
+
base-client member such as `close` is renamed with a trailing underscore --
|
|
1027
|
+
check the generated names before delegating to them.
|
|
1028
|
+
|
|
1029
|
+
The free functions remain as the implementation the methods call. `client` and
|
|
1030
|
+
`resource` compose with [module splitting](#-module-splitting): the functions are
|
|
1031
|
+
split across module files and each method routes to its function's module, while
|
|
1032
|
+
`Client` / `AsyncClient` stay the single public entry point. With `resource` the
|
|
1033
|
+
resources mirror the split modules.
|
|
1034
|
+
|
|
812
1035
|
### Working with Models
|
|
813
1036
|
|
|
814
1037
|
```python
|
|
@@ -947,6 +1170,19 @@ documents:
|
|
|
947
1170
|
place_order(body=Order(id=7)) # -> {"id": 7, "shipDate": null, "note": null}
|
|
948
1171
|
```
|
|
949
1172
|
|
|
1173
|
+
### Multipart bodies
|
|
1174
|
+
|
|
1175
|
+
A `multipart/form-data` body is sent one part per property, following the
|
|
1176
|
+
OpenAPI Encoding Object defaults: `bytes` or file-like values become file
|
|
1177
|
+
uploads, arrays become one part per item under the same name, objects are sent
|
|
1178
|
+
as `application/json` parts, and everything else is sent as a plain text
|
|
1179
|
+
field. `None` values are left out, since multipart cannot express null.
|
|
1180
|
+
|
|
1181
|
+
```python
|
|
1182
|
+
upload(body=Upload(name='a', tags=['x', 'y'], file=b'...'))
|
|
1183
|
+
# -> parts: name=a, tags=x, tags=y, file (upload)
|
|
1184
|
+
```
|
|
1185
|
+
|
|
950
1186
|
### Flattening the body into parameters
|
|
951
1187
|
|
|
952
1188
|
By default a POST/PUT/PATCH takes the body as one model:
|