Skip to main content

A framework for building API clients with minimal boilerplate

Project description

ClientFactory

A Python framework for building API clients with minimal boilerplate while maintaining full configurability and extensibility.

Features

  • Declarative API Definition: Define your API structure using Python classes and decorators
  • Multiple Authentication Methods: Built-in support for:
    • API Key authentication (header or query parameter)
    • OAuth 2.0 (client credentials, authorization code)
    • Session-based authentication with browser automation
    • Basic HTTP authentication
    • Token-based authentication
    • Custom authentication handlers
  • Resource Management:
    • Organize endpoints into logical resource groups
    • Support for nested resources
    • Automatic URL construction
    • Path parameter handling
  • Request Processing:
    • Pre-processing hooks for request modification
    • Post-processing hooks for response transformation
    • Automatic retries with configurable backoff
    • File upload support with progress tracking
  • Session Management:
    • Persistent sessions with encryption
    • Cookie handling
    • Proxy support
    • Custom header management
  • Type Safety: Full type hinting support for better IDE integration
  • Extensibility: Every component is designed to be extended and customized

Installation

pip install clientfactory

Quick Start

Basic Usage

from clientfactory import Client, resource, get, post, ApiKeyAuth

class GitHub(Client):
    baseurl = "https://api.github.com"
    auth = ApiKeyAuth.header("your-token", "Authorization", prefix="Bearer")

    @resource
    class Repos:
        @get("user/repos")
        def list_repos(self): pass

        @post("user/repos")
        def create_repo(self, name: str, private: bool = False):
            return {"name": name, "private": private}

# Use the client
github = GitHub()
repos = github.repos.list_repos()

Request Processing

from clientfactory import Client, resource, get, preprocess, postprocess
from clientfactory.utils import Request, Response

class DataAPI(Client):
    baseurl = "https://api.example.com"

    @resource
    class Data:
        @preprocess
        def add_timestamp(self, request: Request) -> Request:
            """Add timestamp to all requests"""
            return request.WITH(
                headers={"X-Timestamp": str(time.time())}
            )

        @postprocess
        def extract_data(self, response: Response) -> dict:
            """Extract data field from response"""
            return response.json()["data"]

        @get("data/{id}")
        def get_data(self, id: str): pass

File Uploads

from clientfactory import Client, resource, post, UploadConfig
from clientfactory.utils import FileUpload

class Storage(Client):
    baseurl = "https://storage.example.com"

    @resource
    class Files:
        def progress(self, current: int, total: int):
            print(f"Uploaded {current}/{total} bytes")

        @post("upload")
        def upload(self, file: str):
            uploader = FileUpload(
                config=UploadConfig(
                    progresscallback=self.progress
                )
            )
            return uploader.multipart(
                url=self.url + "/upload",
                files={"file": file}
            )

OAuth Authentication

from clientfactory import Client, OAuth2Auth, resource, get

class ServiceAPI(Client):
    baseurl = "https://api.service.com"
    auth = OAuth2Auth.clientcredentials(
        clientid="your-client-id",
        clientsecret="your-client-secret",
        tokenurl="https://auth.service.com/token"
    )

    @resource
    class Users:
        @get("users/me")
        def me(self): pass

Builder Pattern

from clientfactory import ClientBuilder, ApiKeyAuth

# Create client programmatically
client = (ClientBuilder()
    .baseurl("https://api.example.com")
    .auth(ApiKeyAuth.header("your-key"))
    .sessioncfg(verify=False)  # Disable SSL verification
    .requestconfig(timeout=30.0)
    .headers({
        "User-Agent": "MyApp/1.0",
        "Accept": "application/json"
    })
    .build())

Session Persistence

from clientfactory import Client, DiskPersist, PersistConfig

class WebApp(Client):
    baseurl = "https://webapp.example.com"

    def __init__(self):
        # Setup encrypted session persistence
        self.persist = DiskPersist(
            config=PersistConfig(
                path="~/.myapp/session",
                encrypt=True
            )
        )
        super().__init__()

Advanced Usage

For more advanced usage examples, including:

  • Custom authentication handlers
  • Complex request/response processing
  • Browser automation for web apps
  • Request retries and backoff strategies
  • Resource hierarchies
  • Error handling

Visit our Advanced Usage Guide.

Development

# Clone the repository
git clone https://github.com/schizoprada/clientfactory.git
cd clientfactory

# Create a virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install development dependencies
pip install -e ".[test,docs]"

# Run tests
pytest

# Build documentation
cd docs
make html

License

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

Support

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

clientfactory-0.5.4.tar.gz (47.6 kB view details)

Uploaded Source

Built Distribution

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

clientfactory-0.5.4-py3-none-any.whl (41.1 kB view details)

Uploaded Python 3

File details

Details for the file clientfactory-0.5.4.tar.gz.

File metadata

  • Download URL: clientfactory-0.5.4.tar.gz
  • Upload date:
  • Size: 47.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.7

File hashes

Hashes for clientfactory-0.5.4.tar.gz
Algorithm Hash digest
SHA256 f455f57a1d5509ee475c4730fc2c7a7b288896f40898d2edfb853ae0f75afcff
MD5 ecbed140b8d43ff67e7383d374f9998f
BLAKE2b-256 5f1fbdb3789d4a735c006a3d12ec2a68cec59dc868cd6b89f8ff5125477eb363

See more details on using hashes here.

File details

Details for the file clientfactory-0.5.4-py3-none-any.whl.

File metadata

  • Download URL: clientfactory-0.5.4-py3-none-any.whl
  • Upload date:
  • Size: 41.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.7

File hashes

Hashes for clientfactory-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b1876a334839f739589fc04ff61b769081e9972b5209098c99f7906a5a1196ce
MD5 47840839b865df9fb6d0d2a4a1b98a43
BLAKE2b-256 b2d0019911da0894fde07b72811672f38a31e1dd9e4513cac230d54cc17f7292

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