Generate a typed HTTPX client from an OpenAPI specification.
Project description
OpenAPI HTTPX
Generate a typed HTTPX client from an OpenAPI specification. Provides an extremely thin wrapper around the HTTPX Client class that registers overloads for each operation and defines TypedDicts for parameters, request bodies and response bodies.
Installation
pip install openapi-httpx
# or
uv add openapi-httpx
# or
poetry add openapi-httpx
The generated client code imports httpx at runtime. Make sure to install it in any environment where you run the generated client:
pip install httpx
(or uv add httpx / poetry add httpx)
Usage
openapi-httpx --input <OpenAPI yaml/json/url> --output client.py
This produces a client class with overloads for each operation:
class GetUserByIdParams(TypedDict):
id: str
class User(TypedDict):
id: str
name: str
email: str
class PostFileUploadRequest(TypedDict):
name: str
description: str
class OpenApiClient(Client):
@overload
def GET(
self,
path: Literal["/user/{id}"],
*,
params: GetUserByIdParams,
parse_as: Literal["json"] = "json",
**kwargs: Unpack[RequestKwargs]
) -> User:
"""Get a user by ID"""
pass
@overload
def POST(
self,
path: Literal["/user"],
*,
params: None = None,
json: User,
data: None = None,
content: None = None,
files: None = None,
parse_as: Literal["json"] = "json",
**kwargs: Unpack[RequestKwargs]
) -> User:
"""Create a user"""
pass
@overload
def POST(
self,
path: Literal["/file"],
*,
params: None = None,
json: None = None,
data: PostFileUploadRequest,
files: dict[Literal["file"], FileTypes] | list[tuple[Literal["file"], FileTypes]],
content: None = None,
parse_as: Literal["json"] = "json",
**kwargs: Unpack[RequestKwargs]
) -> None:
"""Upload a file"""
pass
CLI Options
| Flag | Description |
|---|---|
--input |
OpenAPI specification file path or URL (required) |
--output |
Output file path (required) |
--target-python-version |
Target Python version (default: 3.11) |
--client-class-name |
Name of the generated client class (default: OpenApiClient) |
--async-client |
Generate an async client based on httpx.AsyncClient |
--use-closed-typed-dict |
Generate closed TypedDicts (PEP 728, closed=True) |
Python API
For more control, use the library directly:
from openapi_httpx import Config, generate
result = generate("path/to/openapi.yaml", Config(
async_client=True,
client_class_name="MyApiClient",
))
with open("client.py", "w") as f:
f.write(result)
See the Config class for all available options.
Differences to plain HTTPX
This library is an extremely thin wrapper around the HTTPX Client class. But there are a few notable differences:
- Path parameters are passed to the
paramsargument alongside query parameters. You cannot use f-strings to interpolate them, or the type checking will break! - The methods don't return
[Response](https://www.python-httpx.org/api/#response)s but instead return the parsed response body. The response is automatically parsed based on theparse_asargument which defaults to'json'. It can also be set to'text'to return astrorNoneto return rawbytes. - The library automatically calls
[raise_for_status](https://www.python-httpx.org/quickstart/#response-status-codes)on the response, which means that any unsuccessful status code will raise an exception. To customize exceptions, you can pass[event_hooks](https://www.python-httpx.org/advanced/event-hooks/)at client registration time.
Prior Art
Inspired by OpenAPI fetch for TypeScript.
Built on top of datamodel-code-generator which handles most of the heavy lifting of parsing the OpenAPI specification.
Development
See CONTRIBUTING.md for development setup and guidelines.
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 openapi_httpx-0.1.1.tar.gz.
File metadata
- Download URL: openapi_httpx-0.1.1.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea6725a3ce924f6a0e23b99da90c8660e50e914de83dc3b3c580d3973bfd8d8e
|
|
| MD5 |
f36a6a477c451f4f2cad485f11255557
|
|
| BLAKE2b-256 |
b1feb77a8f437de6d1a24e721f6a9caaa283c3ad7eafce2ee3fb8d60a564fead
|
Provenance
The following attestation bundles were made for openapi_httpx-0.1.1.tar.gz:
Publisher:
release.yml on MrLoh/openapi-httpx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openapi_httpx-0.1.1.tar.gz -
Subject digest:
ea6725a3ce924f6a0e23b99da90c8660e50e914de83dc3b3c580d3973bfd8d8e - Sigstore transparency entry: 1322305444
- Sigstore integration time:
-
Permalink:
MrLoh/openapi-httpx@ed2caee14c5c4a4dad373ee5e595881279edf58c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/MrLoh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ed2caee14c5c4a4dad373ee5e595881279edf58c -
Trigger Event:
push
-
Statement type:
File details
Details for the file openapi_httpx-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openapi_httpx-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d1ca9bd795f279927d1c5f4c8ec31a2ef70482960628007a96f4b0381a440c6
|
|
| MD5 |
572cd683a30d0ce84d42c35641b49335
|
|
| BLAKE2b-256 |
0452be5f77a6625c5bdf5fb6e7949b3833924ed0b68cb68db570ac565e60c3ec
|
Provenance
The following attestation bundles were made for openapi_httpx-0.1.1-py3-none-any.whl:
Publisher:
release.yml on MrLoh/openapi-httpx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openapi_httpx-0.1.1-py3-none-any.whl -
Subject digest:
4d1ca9bd795f279927d1c5f4c8ec31a2ef70482960628007a96f4b0381a440c6 - Sigstore transparency entry: 1322305927
- Sigstore integration time:
-
Permalink:
MrLoh/openapi-httpx@ed2caee14c5c4a4dad373ee5e595881279edf58c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/MrLoh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ed2caee14c5c4a4dad373ee5e595881279edf58c -
Trigger Event:
push
-
Statement type: