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

Documentation

Documentation

Sneak peek

from typing import List

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

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):
    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.current[0].humidity == 71
assert response.current[0].temperature == 8.0

Project details


Release history Release notifications | RSS feed

This version

4.4.2

Download files

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

Source Distribution

combadge-4.4.2.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

combadge-4.4.2-py3-none-any.whl (38.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: combadge-4.4.2.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1014-azure

File hashes

Hashes for combadge-4.4.2.tar.gz
Algorithm Hash digest
SHA256 29df79e2f0fc8a515073cd60406e3485a8ea1977206fc2a64d428d9c212f50eb
MD5 d4606ba00533a24cc52f5f08f4c34816
BLAKE2b-256 02d0f438a7015a99b580da0231699f3adb1eddd7b08eb86d272a4a9d2079b6dd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: combadge-4.4.2-py3-none-any.whl
  • Upload date:
  • Size: 38.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1014-azure

File hashes

Hashes for combadge-4.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fc196db66a0db8ad56124d2cf4430d888aedde89a4228c6745f5143d6429d774
MD5 f8e55f17af0adafd65432ad729f59c28
BLAKE2b-256 c337923790c0e32bb72416327c1ce1c792423ff6189d2096856a37e5943fc068

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page