Skip to content

Generate a Python httpx- and Pydantic-based client off an OpenAPI file.

License

Notifications You must be signed in to change notification settings

sennder/python-client-generator

Repository files navigation

python-client-generator

Python package to generate an httpx- and pydantic-based async (or sync) client off an OpenAPI spec >= 3.0 (partial specification support).

flowchart LR
    generator["python-client-generator"]
    app["REST API App"]
    package["App HTTP client"]

    app -- "OpenAPI JSON" --> generator
    generator -- "Generates" --> package
Loading

⚠️ Currently does not support OpenAPI 2.x (PRs for < 3.0 support are welcome).

Only partial OpenAPI specification support, if you encounter unsupported spec. please feel free to contribute to the project.

Using the generator

Install the package (see on PyPi):

pip install python-client-generator

Run:

python -m python_client_generator --open-api openapi.json --package-name foo_bar --project-name foo-bar --outdir clients

This will produce a Python package with the following structure:

clients
├── foo_bar
│   ├── __init__.py
│   ├── apis.py
│   ├── base_client.py
│   └── models.py
└── pyproject.toml

Using PATCH functions from the generator

When calling one of the generated update functions that uses an HTTP PATCH method, you'll probably want to pass the additional argument body_serializer_args={"exclude_unset": True}. This will ensure that only the fields that are set in the update object are sent to the API. Example:

await api_client.update_contact_v1_contacts__contact_id__patch(
    body=patch_body,
    contact_id=contact.id,
    tenant=tenant,
    body_serializer_args={"exclude_unset": True}
)

Contributing

Please refer to CONTRIBUTING.md.

About

Generate a Python httpx- and Pydantic-based client off an OpenAPI file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages