Skip to main content

No project description provided

Project description

RestCodeGen

PyPI version Python versions GitHub Actions Coverage Status License: MIT Downloads Code style: ruff

Generate Python clients from OpenAPI specifications with ease

🚀 Overview

RestCodeGen is a powerful tool for automatically generating Python client libraries from OpenAPI 3 specifications. It streamlines the process of interacting with REST APIs, allowing developers and testers to quickly integrate with services that provide OpenAPI documentation.

✨ Key Features

  • Easy Client Generation: Create Python clients with a single command
  • Async Support: Generate both synchronous and asynchronous clients
  • Selective API Generation: Choose specific API tags to include
  • Built-in Logging: Integrated with structlog for comprehensive request/response tracking
  • Customizable: Use your own HTTPX client for advanced configurations
  • Type Hints: All generated code includes proper type annotations

📦 Installation

RestCodeGen requires Python 3.11 or higher. Install it using pip:

pip install restcodegen

Or with Poetry:

poetry add restcodegen

🔧 Usage

Basic Command

restcodegen generate -u "http://example.com/openapi.json" -s "my-service" -a false

Command Parameters

Parameter Short Description Required Default
--url -u URL of the OpenAPI specification Yes -
--service-name -s Name of the service Yes -
--async-mode -a Enable asynchronous client generation No false
--api-tags -t Comma-separated list of API tags to generate No All APIs

Example

Generate a client for the Petstore API:

restcodegen generate -u "https://petstore3.swagger.io/api/v3/openapi.json" -s "petstore" -a false

📁 Generated Structure

After successful execution, a client library will be created with the following structure:

└── clients                      
     └── http     
        ├── schemas               # OpenAPI 3.0.0 schemas for all generated APIs                   
        └── service_name          # Service name     
            ├── apis              # API client classes                    
            └── models            # Pydantic models   

💻 Using the Generated Client

The generated client includes built-in logging with structlog and supports custom HTTPX clients:

from restcodegen.restclient import Client, Configuration
from clients.http.petstore import PetApi
import structlog

# Configure logging
structlog.configure(
    processors=[
        structlog.processors.JSONRenderer(
            indent=4,
            ensure_ascii=True,
        )
    ]
)

# Create and use the client
if __name__ == '__main__':
    # Configure the base URL
    configuration = Configuration(host="https://petstore3.swagger.io/api/v3")
    
    # Use the built-in client
    api_client = Client(configuration)
    
    # Or use your custom HTTPX client
    # import httpx
    # api_client = httpx.Client()  # or httpx.AsyncClient() for async mode
    
    # Initialize the API
    pet_api = PetApi(api_client)
    
    # Make API calls
    response = pet_api.get_pet_pet_id(pet_id=1)
    print(response)

🔄 Development Workflow

  1. Install development dependencies:

    poetry install
    
  2. Run tests:

    poetry run pytest
    
  3. Check code quality:

    poetry run ruff check .
    poetry run mypy .
    

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


🤝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

📬 Contact

For questions or feedback, please open an issue in the repository.


RestCodeGen - Making API integration simple and efficient

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

restcodegen-1.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

restcodegen-1.1.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file restcodegen-1.1.0.tar.gz.

File metadata

  • Download URL: restcodegen-1.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for restcodegen-1.1.0.tar.gz
Algorithm Hash digest
SHA256 50f3372ca1846937f800829d7ccfff7f5d79a2415ac89138b737c55a49782b6f
MD5 6b67cb944e0e29ec66fedfc5918d4b4f
BLAKE2b-256 f22631d6f52354ea7c45cf6f38efaa07429a8d15061d655e69092d842bbe1185

See more details on using hashes here.

File details

Details for the file restcodegen-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: restcodegen-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for restcodegen-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90ced16eda0bdaec699063866b94d7973adda25c8f4144ca1761fa99133c23a0
MD5 586ccf72afef90f9da2ed8f92c337fd3
BLAKE2b-256 3809f7dfaa22ad2199ede7f289fa55235234bfb0eb220beac15f53b56aa01c28

See more details on using hashes here.

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