A Python-based OpenAPI client generator that creates type-safe, async Python clients from OpenAPI specifications.
- ✨ Generate fully typed Python clients from OpenAPI 3.x specifications
- 🔄 Async/await support with aiohttp
- 🛡️ Type safety with Pydantic models
- 🧩 Smart handling of circular dependencies
- 📝 Proper model inheritance and composition
- 🔍 Comprehensive query parameter handling
- 🚨 Built-in error handling with custom exceptions
- 🔐 Support for various authentication methods
- 📦 Automatic dependency management
- Clone the repository:
git clone https://github.com/thinpo/openapi_client_generator.git
cd openapi_client_generator- Install dependencies:
pip install -r requirements.txt- Start the service:
uvicorn src.openapi_consumer.main:app --reload- Generate a client from your OpenAPI spec:
curl -X POST http://localhost:8000/generate \
-F "spec_file=@your_api.yaml" \
-F "package_name=your_client"- Use the generated client:
from your_client import YourAPIClient
async with YourAPIClient("https://api.example.com", api_key="your-key") as client:
# List resources with query parameters
items = await client.listItems(
category=["electronics"],
price_range={"min": 0, "max": 1000},
sort="price_asc",
page=1
)
# Create a resource
new_item = await client.createItem(
ItemCreate(name="Test Item", price=99.99)
).
├── src/
│ └── openapi_consumer/
│ ├── __init__.py
│ ├── main.py # FastAPI application
│ ├── generator/
│ │ ├── __init__.py
│ │ └── client_generator.py # Client generation logic
│ └── models/
│ ├── __init__.py
│ └── schemas.py # Request/Response models
├── scripts/
│ ├── README.md
│ └── update_dependencies.py # Dependency management script
└── tests/
└── test_generator.py
The generated client includes:
- Type-Safe Models: All models are generated as Pydantic classes
- Smart Query Parameters: Handles complex query parameters (arrays, objects) correctly
- Error Handling: Custom ApiError class with status code and error details
- Resource Management: Proper cleanup with async context manager
- Authentication: Support for API key, Bearer token, and OAuth2
- Forward References: Handles circular dependencies between models
- Model Inheritance: Proper handling of allOf, oneOf, anyOf schemas
Use the provided dependency management script to check and update package versions:
./scripts/update_dependencies.pyThe script will:
- Show current vs latest versions of all packages
- Update requirements.txt with latest versions if desired
- Handle version conflicts and dependencies
When the service is running, access:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.