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

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.0.3.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

combadge-4.0.3-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: combadge-4.0.3.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1015-azure

File hashes

Hashes for combadge-4.0.3.tar.gz
Algorithm Hash digest
SHA256 8d0e9267d77390843e324f0bada901dd34c96905b5b3f218cf2206e5c1cfec89
MD5 e1c6e3e3c2281fd02c08d3dc10f0a1ea
BLAKE2b-256 140f7f1d54cdd48f75ae83a9c123e4f8990ba83e5467e4d453aadeec6253fe02

See more details on using hashes here.

File details

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

File metadata

  • Download URL: combadge-4.0.3-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1015-azure

File hashes

Hashes for combadge-4.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 60c696d0a391f894bd5b15eb5de6e0b10acf9cfe377960aff6f8775a946415f2
MD5 34d86c7361d8d2969199e81ba0c68f4a
BLAKE2b-256 d5d0a9fbf1e6cecd5d31a4b94b434b7f926d2293c56e4a78a41d9887b97ad68a

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