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.
Features
- Request and response models based on pydantic
- Declarative services using
Protocol
- Exception classes automatically derived from error models
- Built-in backends:
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.1.0rc4.tar.gz
(22.2 kB
view hashes)
Built Distribution
Close
Hashes for combadge-0.1.0rc4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 155bb79a29967ae9e0e81d9fedabf2506854f137f63dc96f4e258db889c9cee8 |
|
MD5 | cb3cad81805c1dca6f5d328beb67fa7c |
|
BLAKE2b-256 | 1c0cda5110e3de0441ee49ff9c96b68542ce58c6a85a7cc51f67c13cd55a04dc |