Skip to main content

A Python client for OpenAPI specifications using httpx

Project description

OpenAPI Client for Python

A Python implementation inspired by openapi-client-axios that provides a dynamic client for OpenAPI specifications. This implementation uses httpx for HTTP requests and Python's metaprogramming capabilities to dynamically generate a client with an API design similar to httpx.

Installation

pip install openapi-httpx-client

Usage

The client supports both synchronous and asynchronous usage patterns through a familiar context manager interface.

Asynchronous Usage

from openapiclient import OpenAPIClient
import asyncio

async def main():
    # Initialize the API factory with the OpenAPI definition
    api = OpenAPIClient(definition="https://petstore3.swagger.io/api/v3/openapi.json")
    
    # Use the async client with context manager
    async with api.AsyncClient() as client:
        # Show available operations
        print("Operations:", client.operations)
        print("Available functions:", client.functions)
        
        # Call operations directly as methods
        pet = await client.getPetById(petId=1)
        print(f"Status: {pet['status']}")
        print(f"Pet data: {pet['data']}")

        # Call operations directly as methods, using positional arguments, can using in path and query
        pet = await client.getPetById(1)
        print(f"Status: {pet['status']}")
        print(f"Pet data: {pet['data']}")
        
        # Alternative way to call methods
        pet = await client("getPetById", petId=2)
        print(f"Another pet: {pet['data']}")
        
        # Access AI tools definition for integration with LLMs
        print(f"AI tools: {client.tools}")

if __name__ == "__main__":
    asyncio.run(main())

Synchronous Usage

from openapiclient import OpenAPIClient

# Initialize the API factory
api = OpenAPIClient(definition="https://petstore3.swagger.io/api/v3/openapi.json")

# Use the synchronous client with context manager
with api.Client() as client:
    # Show available operations
    print("Operations:", client.operations)
    
    # Call operations directly
    pet = client.getPetById(petId=1)
    print(f"Pet name: {pet['data'].get('name')}")
    
    # Call operations using dictionary-like access
    store_inventory = client["getInventory"]()
    print(f"Store inventory: {store_inventory['data']}")

Advanced Options

You can pass any httpx client options when creating a client:

# With timeout and custom headers
with api.Client(timeout=30, headers={"API-Key": "your-api-key"}) as client:
    result = client.someOperation()

# With proxy configuration
async with api.AsyncClient(proxies="http://localhost:8080") as client:
    result = await client.someOperation()

Features

  • Intuitive API design similar to httpx with context managers
  • Support for both synchronous and asynchronous operations
  • Dynamic client generation using Python metaprogramming
  • Compatible with OpenAPI 3.0 and 3.1 specifications
  • Support for loading specifications from URL, file, or dictionary (JSON/YAML)
  • Response format similar to axios (data, status, headers, config)
  • AI tools generation for integration with LLMs and AI assistants

Client Properties

Each client instance provides these properties:

  • operations: List of all available operation IDs
  • paths: List of API paths defined in the specification
  • functions: Dictionary of all operation methods mapped by name
  • tools: List of AI function calling definitions for LLM integration

Response Format

All API responses are returned in a dictionary format with the following keys:

  • data: The parsed response body (JSON or text)
  • status: HTTP status code
  • headers: Response headers
  • config: Original request configuration

Author

lloydzhou

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

openapi-httpx-client-0.4.2.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

openapi_httpx_client-0.4.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file openapi-httpx-client-0.4.2.tar.gz.

File metadata

  • Download URL: openapi-httpx-client-0.4.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.2

File hashes

Hashes for openapi-httpx-client-0.4.2.tar.gz
Algorithm Hash digest
SHA256 7d88df5310fb821d9c0856d49b18ed5f08bcafa32cd9d52b0bae42d7db0c2de4
MD5 52b4017f51d3289cfe649570cdc493c4
BLAKE2b-256 787741e5d3c298cb492cd12942bc8dba7121e862794812cce9cee1e8387fc96a

See more details on using hashes here.

File details

Details for the file openapi_httpx_client-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_httpx_client-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a437ac75befd99bb303dc590ab8ca11d890fc3c1e0e3def07383186c49cae52f
MD5 ff66387ee91c6f144d8d4a5586d53d25
BLAKE2b-256 b11ac10976b67aa88bd109c557fdadc71667a36fe7581817e3b5056252154f05

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