A powerful tool that generates strongly-typed Python clients from OpenAPI 3.0 and Swagger 2.0 specifications
Project description
OpenAPI Python Client Generator
A powerful tool that generates strongly-typed Python clients from OpenAPI 3.0 and Swagger 2.0 specifications. This generator creates clean, maintainable code with full type annotations, automatic model serialization, and comprehensive error handling.
Features
- Strong Typing: Full type annotations for all generated code
- OpenAPI 3.0 & Swagger 2.0 Support: Works with both specification formats
- Model Generation: Automatically generates data models with properties and validation
- API Client Generation: Creates method stubs for all API endpoints
- Serialization Support: Built-in JSON serialization/deserialization
- Error Handling: Robust error handling with logging
- Clean Code: Well-structured, readable generated code
Installation
From PyPI (Recommended)
pip install openapi-client-python
From Source
git clone https://github.com/autoocto/openapi-client-python.git
cd openapi-client-python
pip install -r requirements.txt
Quick Start
Basic Usage
Generate a client from an OpenAPI specification:
openapi-client-python --spec samples/pet_store.json --output ./generated --service-name petstore
Command Line Options
--spec: Path to your OpenAPI/Swagger specification file (JSON format)--output: Output directory for the generated client--service-name: Name for your service (used for class and directory names)
Getting Help
To see all available options:
openapi-client-python --help
Example
# Generate from the included Pet Store example
openapi-client-python --spec samples/pet_store.json --output ./my_clients --service-name pet_store
# This creates:
# ./my_clients/pet_store/
# ├── __init__.py
# ├── PetStoreAPIs.py
# └── models/
# ├── __init__.py
# ├── Pet.py
# ├── User.py
# └── Order.py
Using the Generated Client
Once generated, you can use your client like this:
from my_clients.pet_store import PetStoreAPIs
from my_clients.pet_store.models.Pet import Pet
# Initialize the client
client = PetStoreAPIs(
base_url="https://petstore.swagger.io/v2",
auth_token="your-api-token"
)
# Create a new pet
new_pet = Pet({
"id": 123,
"name": "Fluffy",
"status": "available"
})
# Use the API
created_pet = client.post_pet(payload=new_pet)
print(f"Created pet: {created_pet.name}")
# Get pets by status
available_pets = client.get_pets_find_by_status(status="available")
for pet in available_pets:
print(f"Pet: {pet.name} (ID: {pet.id})")
Generated Code Structure
The generator creates a well-organized structure:
your_service/
├── __init__.py # Main package exports
├── YourServiceAPIs.py # Main API client class
└── models/ # Data models
├── __init__.py # Model exports
├── ModelName.py # Individual model classes
└── ...
API Client Features
The generated API client includes:
- Authentication Support: Bearer token authentication
- Base URL Management: Configurable base URL
- Request/Response Handling: Automatic serialization/deserialization
- Error Handling: Comprehensive error handling with logging
- Type Safety: Full type annotations for all methods
Model Features
Generated models include:
- Property Access: Clean property getters/setters
- Type Annotations: Full typing support
- Serialization:
to_dict(),to_json(),from_dict(),from_json()methods - Validation: Basic type validation
Development
Project Structure
openapi-client-python/
├── src/
│ ├── main.py # CLI entry point
│ └── openapi_client_generator/ # Main package
│ ├── __init__.py # Package exports
│ ├── generator.py # Main orchestrator
│ ├── spec_loader.py # Specification loader
│ ├── model_generator.py # Model generation
│ ├── api_generator.py # API client generation
│ └── utils.py # Utility functions
├── tests/ # Unit tests
│ ├── __init__.py # Test runner
│ ├── test_spec_loader.py # Spec loader tests
│ ├── test_model_generator.py # Model generator tests
│ ├── test_api_generator.py # API generator tests
│ ├── test_generator.py # Main generator tests
│ └── test_utils.py # Utility tests
├── samples/ # Example specifications
├── requirements.txt # Dependencies
└── README.md # This file
Running Tests
Run the comprehensive test suite:
# Run all tests
python -m pytest tests/
# Run with coverage
python -m pytest tests/ --cov=src/openapi_client_generator
# Run specific test module
python -m pytest tests/test_generator.py -v
Dependencies
requests: HTTP client library- Standard library modules:
json,pathlib,typing,re,argparse
Supported Specifications
OpenAPI 3.0
- ✅ Path operations (GET, POST, PUT, DELETE, PATCH)
- ✅ Request/response models
- ✅ Path parameters
- ✅ Query parameters
- ✅ Request bodies
- ✅ Schema references (
#/components/schemas/) - ✅ Array responses
- ✅ Nested models
Swagger 2.0
- ✅ Path operations
- ✅ Model definitions
- ✅ Path parameters
- ✅ Query parameters
- ✅ Body parameters
- ✅ Schema references (
#/definitions/) - ✅ Array responses
Examples
Check the samples/ directory for example specifications:
pet_store.json: OpenAPI 3.0 Pet Store examplepet_store_swagger.json: Swagger 2.0 Pet Store examplesimple_api_overview.json: Simple API example
Generate from Pet Store
# OpenAPI 3.0 version
openapi-client-python --spec samples/pet_store.json --output ./clients --service-name petstore
# Swagger 2.0 version
openapi-client-python --spec samples/pet_store_swagger.json --output ./clients --service-name petstore_v2
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes
- Add tests for new functionality
- Run the test suite:
python -m pytest tests/ - Commit your changes:
git commit -am 'Add feature' - Push to the branch:
git push origin feature-name - Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
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_client_python-1.0.0.tar.gz.
File metadata
- Download URL: openapi_client_python-1.0.0.tar.gz
- Upload date:
- Size: 43.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76cffbba6aea1f39288d569776a5c4a3739ddb4c72a0e70027ca5e9796d913da
|
|
| MD5 |
e8553ebcc8efb423458aeaee3e8429ba
|
|
| BLAKE2b-256 |
e3d37ba7e9fe932270ee6c7c0723831637c7c1d45832da6074d7bb1b508be972
|
File details
Details for the file openapi_client_python-1.0.0-py3-none-any.whl.
File metadata
- Download URL: openapi_client_python-1.0.0-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6504b519d24e6fa53cdb150e395da2ed31e5a7ae2d4673493278fd15ffe49426
|
|
| MD5 |
f4ec07729aaacab6d1f7d4beadcef728
|
|
| BLAKE2b-256 |
00ef429159fa7b2be8702a2d3ebd6906d3a95141f0d23187503abf9738725589
|