Skip to main content

A framework for building API clients with minimal boilerplate

Project description

ClientFactory

A declarative framework for building API clients with minimal boilerplate that supports multiple protocols and authentication methods.

Features

Declarative API Definition

from clientfactory import Client, resource, get, post

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

    @resource
    class Users:
        @get("{id}")
        def get_user(self, id): pass

        @post
        def create_user(self, **data): pass

Multi-Protocol Support

GraphQL

from clientfactory import searchresource
from clientfactory.decorators import graphql
from clientfactory.shorthands import PL, P

@graphql
class BACKEND:
    operation = "MyQuery"
    query = """
        query MyQuery($input: SearchInput!) {
            search(input: $input) {
                items { id name }
            }
        }
    """

class GraphQLClient(Client):
    @searchresource
    class Search:
        backend = BACKEND
        payload = PL(
            query=P(required=True),
            filter=P(type=PT.OBJECT)
        )

Algolia Search

from clientfactory.decorators import algolia

@algolia
class BACKEND:
    appid = "YOUR_APP_ID"
    apikey = "YOUR_API_KEY"
    indices = ["primary_index", "secondary_index"]

class AlgoliaClient(Client):
    @searchresource
    class Search:
        backend = BACKEND
        payload = PL(
            query=P(required=True),
            page=P(default=1),
            hitsPerPage=P(default=20)
        )

Comprehensive Auth Support

Basic Auth

from clientfactory.auth import BasicAuth

client = Client(
    auth=BasicAuth(username="user", password="pass")
)

Bearer Token

from clientfactory.auth import TokenAuth

client = Client(
    auth=TokenAuth.Bearer("my-token")
)

OAuth 2.0

from clientfactory.auth import OAuthAuth

auth = OAuthAuth.ClientCredentials(
    clientid="id",
    clientsecret="secret",
    tokenurl="https://auth.example.com/token"
)
client = Client(auth=auth)

Enhanced Session Management

from clientfactory.session import EnhancedSession, Headers
from clientfactory.decorators import session

@session
class SESSION:
    headers = Headers(
        static={"User-Agent": "MyClient/1.0"},
        dynamic={
            "X-Timestamp": lambda: str(int(time.time()))
        }
    )
    persistcookies = True

client = Client(session=SESSION)

Parameter Validation & Transformation

from clientfactory.shorthands import PL, P, PT

PAYLOAD = PL(
    query=P(required=True),
    limit=P(type=PT.NUMBER, default=20),
    sort=P(choices=["asc", "desc"]),
    fields=P(type=PT.ARRAY, transform=lambda x: ",".join(x))
)

Installation

pip install clientfactory

Documentation

under construction

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file 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

clientfactory-0.6.9.tar.gz (56.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.6.9-py3-none-any.whl (77.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: clientfactory-0.6.9.tar.gz
  • Upload date:
  • Size: 56.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for clientfactory-0.6.9.tar.gz
Algorithm Hash digest
SHA256 d6ac8e7a631a18a6f0ad8cc0faffbb9f7d7bc02fe31e3d8527a7fefd2271137f
MD5 04de570704dc8d60c0ef8dd1c1fc943f
BLAKE2b-256 07a79e36b820acb03d9e5838a1ed55756fe39a979d3b31e7ada41e4c4477ac34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clientfactory-0.6.9-py3-none-any.whl
  • Upload date:
  • Size: 77.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for clientfactory-0.6.9-py3-none-any.whl
Algorithm Hash digest
SHA256 dbf3337eff94d8105e0deb79f4536d6b1ba1079cd518269c9649bbc0081005ea
MD5 8072d1a9117ce67a92cfee2943a79676
BLAKE2b-256 75d9dc554f720309a23d5d2a6320baa3dd765d4853a73c2b74ae7e38c47b9d65

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