Skip to main content

A declarative, type-safe API client framework for Python

Project description

Declarative API Client

A declarative, type-safe API client framework for Python 3.12+.

Features

  • Declarative endpoint definitions using type annotations
  • Type-safe request and response handling with Pydantic
  • Sync and async client support
  • Pluggable backends (aiohttp, httpx, or custom)
  • Multiple response types: JSON, text, bytes, or no content

Installation

# Using uv
uv add clientcraft

# With aiohttp backend
uv add "clientcraft[aiohttp]"

# With all optional backends
uv add "clientcraft[all]"

Quick Start

Define your API

from typing import Literal
from pydantic import BaseModel
from clientcraft import Get, Post, Delete
from clientcraft.client import APIClient

# Define request/response models
class GetUserRequest(BaseModel):
    user_id: str

class User(BaseModel):
    id: str
    name: str
    email: str

class CreateUserRequest(BaseModel):
    name: str
    email: str

# Define your API client declaratively
class UserAPI(APIClient):
    get_user: Get[GetUserRequest, User, Literal["/users/{user_id}"]]
    create_user: Post[CreateUserRequest, User, Literal["/users"]]
    delete_user: Delete[GetUserRequest, None, Literal["/users/{user_id}"]]

Use your API

from clientcraft.backends import RequestsBackend

# Create client with a backend
backend = RequestsBackend()
client = UserAPI(base_url="https://api.example.com", backend=backend)

# Make requests - fully typed!
user = client.get_user(GetUserRequest(user_id="123"))
print(user.name)  # Type checker knows this is a User

# Create a user
new_user = client.create_user(CreateUserRequest(name="Alice", email="alice@example.com"))

Async Usage

from clientcraft import AsyncGet, AsyncPost
from clientcraft.async_client import AsyncAPIClient
from clientcraft.backends import AiohttpBackend

class AsyncUserAPI(AsyncAPIClient):
    get_user: AsyncGet[GetUserRequest, User, Literal["/users/{user_id}"]]
    create_user: AsyncPost[CreateUserRequest, User, Literal["/users"]]

async with AiohttpBackend() as backend:
    client = AsyncUserAPI(base_url="https://api.example.com", backend=backend)
    user = await client.get_user(GetUserRequest(user_id="123"))

Endpoint Types

Type HTTP Method Request Style Description
Get GET Query params Read operations
Post POST JSON body Create operations
Put PUT JSON body Full update
Patch PATCH JSON body Partial update
Delete DELETE Query params Delete operations

Response Types

from clientcraft import TextResponse, BytesResponse

class HealthAPI(APIClient):
    # JSON response (default)
    get_user: Get[Request, User, Literal["/users/{id}"]]
    
    # Text response
    health_check: Get[Request, TextResponse, Literal["/health"]]
    
    # Binary response
    download: Get[Request, BytesResponse, Literal["/files/{id}"]]
    
    # No response body (204 No Content)
    delete_user: Delete[Request, None, Literal["/users/{id}"]]

Development

# Clone and setup
cd clientcraft
uv sync --all-extras

# Run tests
uv run pytest

# Type checking
uv run mypy

# Linting
uv run ruff check .

License

MIT

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

clientcraft-0.1.0.tar.gz (100.1 kB view details)

Uploaded Source

Built Distribution

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

clientcraft-0.1.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file clientcraft-0.1.0.tar.gz.

File metadata

  • Download URL: clientcraft-0.1.0.tar.gz
  • Upload date:
  • Size: 100.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for clientcraft-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0468b9ba0115adaf5f24628e918adc16d087191623b11e34f331eadc76cacb4
MD5 17b84a3df9e5db515daa4dec6717d125
BLAKE2b-256 aa140416b24f53ccaa293a270ec0e957300bf58e807099a4d26ae0ac4e3d0717

See more details on using hashes here.

Provenance

The following attestation bundles were made for clientcraft-0.1.0.tar.gz:

Publisher: publish.yml on SunBright-Technologies/clientcraft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clientcraft-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: clientcraft-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for clientcraft-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43e6cd9a644e9d6397804858d40947daa485f9bf9049017d3c634411a3e32889
MD5 cff4f46fbe0d4df11eed218d49b06a6a
BLAKE2b-256 500adf9184f1eb544a6ae0aa82687ed63079d18f85e172d7811c29b2857921d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for clientcraft-0.1.0-py3-none-any.whl:

Publisher: publish.yml on SunBright-Technologies/clientcraft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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