openapi-client-python


Nameopenapi-client-python JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/autoocto/openapi-client-python
SummaryA powerful tool that generates strongly-typed Python clients from OpenAPI 3.0 and Swagger 2.0 specifications
upload_time2025-10-16 11:52:38
maintainerNone
docs_urlNone
authorautoocto
requires_python>=3.7
licenseMIT
keywords openapi swagger client generator api rest python typing code-generation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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
- **Test Coverage**: Comprehensive unit tests

## Installation

### From PyPI (Recommended)

```bash
pip install openapi-client-python
```

### From Source

```bash
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:

```bash
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:

```bash
openapi-client-python --help
```

### Example

```bash
# 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:

```python
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:

```bash
# 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 example
- `pet_store_swagger.json`: Swagger 2.0 Pet Store example
- `simple_api_overview.json`: Simple API example

### Generate from Pet Store

```bash
# 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

1. Fork the repository
2. Create a feature branch: `git checkout -b feature-name`
3. Make your changes
4. Add tests for new functionality
5. Run the test suite: `python -m pytest tests/`
6. Commit your changes: `git commit -am 'Add feature'`
7. Push to the branch: `git push origin feature-name`
8. Submit a pull request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Changelog

### v0.1.0
- Initial release
- OpenAPI 3.0 and Swagger 2.0 support
- Strongly-typed model generation
- API client generation
- Comprehensive test suite
- Clean, maintainable code structure

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/autoocto/openapi-client-python",
    "name": "openapi-client-python",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "AutoOcto <contact@autoocto.com>",
    "keywords": "openapi, swagger, client, generator, api, rest, python, typing, code-generation",
    "author": "autoocto",
    "author_email": "AutoOcto <contact@autoocto.com>",
    "download_url": "https://files.pythonhosted.org/packages/ec/f2/3adc2f3337a95e0f78c570a72a824118fead5ae779db2d17a18743b96ca7/openapi_client_python-0.1.2.tar.gz",
    "platform": null,
    "description": "# OpenAPI Python Client Generator\r\n\r\nA 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.\r\n\r\n## Features\r\n\r\n- **Strong Typing**: Full type annotations for all generated code\r\n- **OpenAPI 3.0 & Swagger 2.0 Support**: Works with both specification formats\r\n- **Model Generation**: Automatically generates data models with properties and validation\r\n- **API Client Generation**: Creates method stubs for all API endpoints\r\n- **Serialization Support**: Built-in JSON serialization/deserialization\r\n- **Error Handling**: Robust error handling with logging\r\n- **Clean Code**: Well-structured, readable generated code\r\n- **Test Coverage**: Comprehensive unit tests\r\n\r\n## Installation\r\n\r\n### From PyPI (Recommended)\r\n\r\n```bash\r\npip install openapi-client-python\r\n```\r\n\r\n### From Source\r\n\r\n```bash\r\ngit clone https://github.com/autoocto/openapi-client-python.git\r\ncd openapi-client-python\r\npip install -r requirements.txt\r\n```\r\n\r\n## Quick Start\r\n\r\n### Basic Usage\r\n\r\nGenerate a client from an OpenAPI specification:\r\n\r\n```bash\r\nopenapi-client-python --spec samples/pet_store.json --output ./generated --service-name petstore\r\n```\r\n\r\n### Command Line Options\r\n\r\n- `--spec`: Path to your OpenAPI/Swagger specification file (JSON format)\r\n- `--output`: Output directory for the generated client\r\n- `--service-name`: Name for your service (used for class and directory names)\r\n\r\n### Getting Help\r\n\r\nTo see all available options:\r\n\r\n```bash\r\nopenapi-client-python --help\r\n```\r\n\r\n### Example\r\n\r\n```bash\r\n# Generate from the included Pet Store example\r\nopenapi-client-python --spec samples/pet_store.json --output ./my_clients --service-name pet_store\r\n\r\n# This creates:\r\n# ./my_clients/pet_store/\r\n#   \u251c\u2500\u2500 __init__.py\r\n#   \u251c\u2500\u2500 PetStoreAPIs.py\r\n#   \u2514\u2500\u2500 models/\r\n#       \u251c\u2500\u2500 __init__.py\r\n#       \u251c\u2500\u2500 Pet.py\r\n#       \u251c\u2500\u2500 User.py\r\n#       \u2514\u2500\u2500 Order.py\r\n```\r\n\r\n## Using the Generated Client\r\n\r\nOnce generated, you can use your client like this:\r\n\r\n```python\r\nfrom my_clients.pet_store import PetStoreAPIs\r\nfrom my_clients.pet_store.models.Pet import Pet\r\n\r\n# Initialize the client\r\nclient = PetStoreAPIs(\r\n    base_url=\"https://petstore.swagger.io/v2\",\r\n    auth_token=\"your-api-token\"\r\n)\r\n\r\n# Create a new pet\r\nnew_pet = Pet({\r\n    \"id\": 123,\r\n    \"name\": \"Fluffy\",\r\n    \"status\": \"available\"\r\n})\r\n\r\n# Use the API\r\ncreated_pet = client.post_pet(payload=new_pet)\r\nprint(f\"Created pet: {created_pet.name}\")\r\n\r\n# Get pets by status\r\navailable_pets = client.get_pets_find_by_status(status=\"available\")\r\nfor pet in available_pets:\r\n    print(f\"Pet: {pet.name} (ID: {pet.id})\")\r\n```\r\n\r\n## Generated Code Structure\r\n\r\nThe generator creates a well-organized structure:\r\n\r\n```\r\nyour_service/\r\n\u251c\u2500\u2500 __init__.py                 # Main package exports\r\n\u251c\u2500\u2500 YourServiceAPIs.py          # Main API client class\r\n\u2514\u2500\u2500 models/                     # Data models\r\n    \u251c\u2500\u2500 __init__.py             # Model exports\r\n    \u251c\u2500\u2500 ModelName.py            # Individual model classes\r\n    \u2514\u2500\u2500 ...\r\n```\r\n\r\n### API Client Features\r\n\r\nThe generated API client includes:\r\n\r\n- **Authentication Support**: Bearer token authentication\r\n- **Base URL Management**: Configurable base URL\r\n- **Request/Response Handling**: Automatic serialization/deserialization\r\n- **Error Handling**: Comprehensive error handling with logging\r\n- **Type Safety**: Full type annotations for all methods\r\n\r\n### Model Features\r\n\r\nGenerated models include:\r\n\r\n- **Property Access**: Clean property getters/setters\r\n- **Type Annotations**: Full typing support\r\n- **Serialization**: `to_dict()`, `to_json()`, `from_dict()`, `from_json()` methods\r\n- **Validation**: Basic type validation\r\n\r\n## Development\r\n\r\n### Project Structure\r\n\r\n```\r\nopenapi-client-python/\r\n\u251c\u2500\u2500 src/\r\n\u2502   \u251c\u2500\u2500 main.py                           # CLI entry point\r\n\u2502   \u2514\u2500\u2500 openapi_client_generator/         # Main package\r\n\u2502       \u251c\u2500\u2500 __init__.py                   # Package exports\r\n\u2502       \u251c\u2500\u2500 generator.py                  # Main orchestrator\r\n\u2502       \u251c\u2500\u2500 spec_loader.py                # Specification loader\r\n\u2502       \u251c\u2500\u2500 model_generator.py            # Model generation\r\n\u2502       \u251c\u2500\u2500 api_generator.py              # API client generation\r\n\u2502       \u2514\u2500\u2500 utils.py                      # Utility functions\r\n\u251c\u2500\u2500 tests/                                # Unit tests\r\n\u2502   \u251c\u2500\u2500 __init__.py                       # Test runner\r\n\u2502   \u251c\u2500\u2500 test_spec_loader.py               # Spec loader tests\r\n\u2502   \u251c\u2500\u2500 test_model_generator.py           # Model generator tests\r\n\u2502   \u251c\u2500\u2500 test_api_generator.py             # API generator tests\r\n\u2502   \u251c\u2500\u2500 test_generator.py                 # Main generator tests\r\n\u2502   \u2514\u2500\u2500 test_utils.py                     # Utility tests\r\n\u251c\u2500\u2500 samples/                              # Example specifications\r\n\u251c\u2500\u2500 requirements.txt                      # Dependencies\r\n\u2514\u2500\u2500 README.md                             # This file\r\n```\r\n\r\n### Running Tests\r\n\r\nRun the comprehensive test suite:\r\n\r\n```bash\r\n# Run all tests\r\npython -m pytest tests/\r\n\r\n# Run with coverage\r\npython -m pytest tests/ --cov=src/openapi_client_generator\r\n\r\n# Run specific test module\r\npython -m pytest tests/test_generator.py -v\r\n```\r\n\r\n### Dependencies\r\n\r\n- `requests`: HTTP client library\r\n- Standard library modules: `json`, `pathlib`, `typing`, `re`, `argparse`\r\n\r\n## Supported Specifications\r\n\r\n### OpenAPI 3.0\r\n- \u2705 Path operations (GET, POST, PUT, DELETE, PATCH)\r\n- \u2705 Request/response models\r\n- \u2705 Path parameters\r\n- \u2705 Query parameters\r\n- \u2705 Request bodies\r\n- \u2705 Schema references (`#/components/schemas/`)\r\n- \u2705 Array responses\r\n- \u2705 Nested models\r\n\r\n### Swagger 2.0\r\n- \u2705 Path operations\r\n- \u2705 Model definitions\r\n- \u2705 Path parameters\r\n- \u2705 Query parameters\r\n- \u2705 Body parameters\r\n- \u2705 Schema references (`#/definitions/`)\r\n- \u2705 Array responses\r\n\r\n## Examples\r\n\r\nCheck the `samples/` directory for example specifications:\r\n\r\n- `pet_store.json`: OpenAPI 3.0 Pet Store example\r\n- `pet_store_swagger.json`: Swagger 2.0 Pet Store example\r\n- `simple_api_overview.json`: Simple API example\r\n\r\n### Generate from Pet Store\r\n\r\n```bash\r\n# OpenAPI 3.0 version\r\nopenapi-client-python --spec samples/pet_store.json --output ./clients --service-name petstore\r\n\r\n# Swagger 2.0 version\r\nopenapi-client-python --spec samples/pet_store_swagger.json --output ./clients --service-name petstore_v2\r\n```\r\n\r\n## Contributing\r\n\r\n1. Fork the repository\r\n2. Create a feature branch: `git checkout -b feature-name`\r\n3. Make your changes\r\n4. Add tests for new functionality\r\n5. Run the test suite: `python -m pytest tests/`\r\n6. Commit your changes: `git commit -am 'Add feature'`\r\n7. Push to the branch: `git push origin feature-name`\r\n8. Submit a pull request\r\n\r\n## License\r\n\r\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\r\n\r\n## Changelog\r\n\r\n### v0.1.0\r\n- Initial release\r\n- OpenAPI 3.0 and Swagger 2.0 support\r\n- Strongly-typed model generation\r\n- API client generation\r\n- Comprehensive test suite\r\n- Clean, maintainable code structure\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A powerful tool that generates strongly-typed Python clients from OpenAPI 3.0 and Swagger 2.0 specifications",
    "version": "0.1.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/autoocto/openapi-client-python/issues",
        "Changelog": "https://github.com/autoocto/openapi-client-python#changelog",
        "Documentation": "https://github.com/autoocto/openapi-client-python#readme",
        "Homepage": "https://github.com/autoocto/openapi-client-python",
        "Repository": "https://github.com/autoocto/openapi-client-python"
    },
    "split_keywords": [
        "openapi",
        " swagger",
        " client",
        " generator",
        " api",
        " rest",
        " python",
        " typing",
        " code-generation"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "e6a34f1a5517a3eec9bb16fe3aac349d1f6686d94776b874e975aaa2ad1886f9",
                "md5": "f7ef4db0a6ec3c8290e3890ddf9a22fb",
                "sha256": "2d6232c50c526405b21a42a71364298ee5c895e68b924449487d61b313189f11"
            },
            "downloads": -1,
            "filename": "openapi_client_python-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f7ef4db0a6ec3c8290e3890ddf9a22fb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 27070,
            "upload_time": "2025-10-16T11:52:37",
            "upload_time_iso_8601": "2025-10-16T11:52:37.236715Z",
            "url": "https://files.pythonhosted.org/packages/e6/a3/4f1a5517a3eec9bb16fe3aac349d1f6686d94776b874e975aaa2ad1886f9/openapi_client_python-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ecf23adc2f3337a95e0f78c570a72a824118fead5ae779db2d17a18743b96ca7",
                "md5": "4e2608331360f60ab00c5a3e63c2295a",
                "sha256": "22a9f1b4628ee4bdff8c4bede6c04540f79de367942d38fa6148b1651e18aacc"
            },
            "downloads": -1,
            "filename": "openapi_client_python-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "4e2608331360f60ab00c5a3e63c2295a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 41698,
            "upload_time": "2025-10-16T11:52:38",
            "upload_time_iso_8601": "2025-10-16T11:52:38.959721Z",
            "url": "https://files.pythonhosted.org/packages/ec/f2/3adc2f3337a95e0f78c570a72a824118fead5ae779db2d17a18743b96ca7/openapi_client_python-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-16 11:52:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "autoocto",
    "github_project": "openapi-client-python",
    "github_not_found": true,
    "lcname": "openapi-client-python"
}
        
Elapsed time: 1.58399s