Skip to main content

A dynamic and flexible API client for OpenAPI and Postman specs.

Project description

SmartAPI Client 🚀

A Python HTTP client for REST APIs with dynamic endpoint registration, retry logic, mock responses, and OpenAPI/Postman integration.


🔧 Features

  • Dynamic Endpoint Registration – Generate API methods from OpenAPI/Postman specs
  • Mock Responses – Test APIs without real network calls
  • Retry Logic – Configurable retries for failed requests
  • CLI Tool – Convert Postman/OpenAPI specs to client-ready code
  • Type Hints – Full IDE support
  • Error Handling – 40+ HTTP status-specific exceptions
  • Parameter Validation – Path/query params auto-detection
  • Business Error Detection – Catch errors in successful responses
  • SSL Verification – Built-in security checks

📦 Installation

pip install smartapi-client

⚡ Quick Start

from smartapi import ApiClient, EndpointConfig

# Initialize client
client = ApiClient(
    "ExampleAPI",
    base_url="https://api.example.com",
    api_key="your_key_here",
    retry_options={"retries": 3}
)

# Register endpoints
client.register_endpoints([
    EndpointConfig(
        method="GET",
        path="/users/{user_id}",
        params=["user_id"],
        description="Get user details"
    )
])

# Call dynamically generated method
user = client.get_users(user_id=1)

🛠️ CLI Usage Convert Postman/OpenAPI specs to client-ready endpoints:

smartapi generate \
  -i postman_collection.json \
  -t postman \
  -o endpoints.json \
  --base-url https://api.example.com

📚 Detailed Usage

  1. Dynamic Endpoints
client.register_endpoints([
    EndpointConfig(
        method="POST",
        path="/orders",
        params=["items", "shipping_address"],
        description="Create new order"
    ),
    EndpointConfig(
        method="GET",
        path="/orders/{order_id}",
        params=["order_id"],
        description="Retrieve order details"
    )
])

new_order = client.create_orders(
    items=["item1", "item2"],
    shipping_address="123 Main St"
)

order_details = client.get_orders(order_id="ORD-123")
  1. Mock Responses
client = ApiClient(
 "TestAPI",
 test_mode=True,
 mock_responses={
     "GET /users/1": (200, {"id": 1, "name": "Test User"}),
     "POST /orders": {"status": "created"}
 }
)

# Returns mock data
user = client.get_users(user_id=1)  # Returns {"id": 1, "name": "Test User"}
  1. Error Handling
try:
    client.get_users(user_id=999)
except ApiNotFoundError as e:
    print(f"Error {e.status_code}: {e}")
except ApiRateLimitError as e:
    print(f"Retry after {e.retry_after} seconds")
except ApiClientError as e:
    print(f"Generic error: {e}")
  1. Retry Configuration
from smartapi import RetryConfig

client = ApiClient(
    "RetryDemo",
    retry_options=RetryConfig(
        retries=5,
        status_forcelist=[500, 502, 503, 504],
        backoff_factor=1.5
    )
)
  1. Request/Response Hooks
def add_tracing(headers):
    headers["X-Trace-ID"] = "trace_123"
    return headers

client = ApiClient(
    "TracedAPI",
    before_request=lambda method, url, params, headers, data: {
        "headers": add_tracing(headers)
    },
    after_response=lambda response, duration: 
        print(f"Request took {duration:.2f}s")
)

🔄 Convert API Specs Postman Collections

from smartapi import ApiClient

client = ApiClient.from_postman_collection(
    collection_path="postman_collection.json",
    base_url="https://api.example.com",
    save_endpoints=True,
    output_format="yaml"
)

OpenAPI Specifications

client = ApiClient.from_openapi_spec(
    spec_path="openapi.yaml",
    base_url="https://api.example.com",
    api_name="SwaggerAPI"
)

🤝 Contributing 1.Fork the repository

2.Create a feature branch:

git checkout -b feature/awesome-feature

3.Commit your changes:

git commit -m 'Add awesome feature'

4.Push to your branch:

git push origin feature/awesome-feature

📄 License MIT License – See LICENSE for details.

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

smartapi_client-0.1.15.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

smartapi_client-0.1.15-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file smartapi_client-0.1.15.tar.gz.

File metadata

  • Download URL: smartapi_client-0.1.15.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.12 Linux/6.8.0-59-generic

File hashes

Hashes for smartapi_client-0.1.15.tar.gz
Algorithm Hash digest
SHA256 dad80f6f6a70cf97e237889d4fd17d8db81595dcb12c29da86b43bf1eda3f636
MD5 879644c5f06bef839694efb7b527151c
BLAKE2b-256 0ab2467a06a3540aab67279f42433c5fab7325f7aec87d72c2eecd4771ecdbb8

See more details on using hashes here.

File details

Details for the file smartapi_client-0.1.15-py3-none-any.whl.

File metadata

  • Download URL: smartapi_client-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.12 Linux/6.8.0-59-generic

File hashes

Hashes for smartapi_client-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 c234d1e4ad6745f3859d3f9c7e6cb4e4ab971a5fd470a3a71f9a30f06ea2f5d6
MD5 4cfcac99fb2b07e84d2bbc73faa1af27
BLAKE2b-256 14ea6a81ad4bbb0e2422f809e532e3ae1e2977a328e1905d64edb2519ea3f18c

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