Skip to main content

Generic API client based on Pydantic

Project description

Combadge

Combadge generates a service client implementation from a user service interface declared by a protocol class or an abstract base class.

Checks Coverage Code style Python Version License

Features

  • Request and response models based on pydantic
  • Declarative services using Protocol
  • Exception classes automatically derived from error models
  • Built-in backends:
    • HTTPX – sync and async
    • Zeep – sync and async

Documentation

Documentation

Sneak peek

from http import HTTPStatus
from typing import List

from httpx import Client
from pydantic import BaseModel, Field
from typing_extensions import Annotated, Protocol

from combadge.support.http.aliases import StatusCode
from combadge.support.http.markers import QueryParam, http_method, path
from combadge.support.httpx.backends.sync import HttpxBackend


# 1️⃣ Declare the response models:
class CurrentCondition(BaseModel):
    humidity: int
    temperature: Annotated[float, Field(alias="temp_C")]


class Weather(BaseModel):
    status: StatusCode[HTTPStatus]
    current: Annotated[List[CurrentCondition], Field(alias="current_condition")]


# 2️⃣ Declare the protocol:
class SupportsWttrIn(Protocol):
    @http_method("GET")
    @path("/{in_}")
    def get_weather(
        self,
        *,
        in_: str,
        format_: Annotated[str, QueryParam("format")] = "j1",
    ) -> Weather:
        raise NotImplementedError


# 3️⃣ Bind the service:
with HttpxBackend(Client(base_url="https://wttr.in"))[SupportsWttrIn] as service:
    # 🚀 Call the service:
    response = service.get_weather(in_="amsterdam")

assert response.status == HTTPStatus.OK
assert response.current[0].humidity == 71
assert response.current[0].temperature == 8.0

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

combadge-0.2.1.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

combadge-0.2.1-py3-none-any.whl (35.6 kB view details)

Uploaded Python 3

File details

Details for the file combadge-0.2.1.tar.gz.

File metadata

  • Download URL: combadge-0.2.1.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1041-azure

File hashes

Hashes for combadge-0.2.1.tar.gz
Algorithm Hash digest
SHA256 85a74d63c825eef80a0cb3b5db7935cb25dec859e00a89c7ba1fb3b40bd25d25
MD5 b3db31b8c0a5af16b3ce1b53c7a33efc
BLAKE2b-256 e8a5ee5e788e7eec7d37f37b19cb174c6fc8bd16e15fd2380a33ecfb0c11ec45

See more details on using hashes here.

File details

Details for the file combadge-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: combadge-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 35.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1041-azure

File hashes

Hashes for combadge-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 42aeb8f85076801615418d49974148a5c970db77fdb2f8b7a133b912a30d58e6
MD5 24f3566985cdef0e1dc89e717a4fc824
BLAKE2b-256 25ed9030c9faafae8a6ec734ff46e367c16ccba8a5524376a3e772fff3be776c

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