Skip to main content

Generate a typed HTTPX client from an OpenAPI specification.

Project description

OpenAPI HTTPX

PyPI Python License CI

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 params argument 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 the parse_as argument which defaults to 'json'. It can also be set to 'text' to return a str or None to return raw bytes.
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openapi_httpx-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openapi_httpx-0.1.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file openapi_httpx-0.1.0.tar.gz.

File metadata

  • Download URL: openapi_httpx-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openapi_httpx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5e28c0a008de20f922ed52e00c4a0a8e1a22f5bf4ab9339242ca0eb61108430f
MD5 33461ac10cbe3533b9ab519dc36f6aa8
BLAKE2b-256 2c3cbe9850fbdbc7dca7943d49fbeb71d8a8661b197dc10c2acd9688f56cc4c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for openapi_httpx-0.1.0.tar.gz:

Publisher: release.yml on MrLoh/openapi-httpx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openapi_httpx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: openapi_httpx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openapi_httpx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 88a50ca9eca4f68a1bcf832b0049b911f8ad54e9c45928e1425ff83a623f1bbb
MD5 40c8e7e4e64e45ff8992dcda14403201
BLAKE2b-256 261ed4cd0dea42577f1a2ea7c896a9f4bfc3169e7e2b21d5adb35c4ba141e8d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for openapi_httpx-0.1.0-py3-none-any.whl:

Publisher: release.yml on MrLoh/openapi-httpx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page