Generic API client based on Pydantic
Project description
Combadge
📻 Application to the service, please respond!
Features
- Pydantic-based request and response models
- Automatically derived exception classes
- Using Protocols to define service classes
- Built-in backends:
- Pluggable backends
Documentation
🚀 Quick example
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.core.binder import bind
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: Annotated[HTTPStatus, Field(alias="__status_code__")]
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:
backend = HttpxBackend(Client(base_url="https://wttr.in"))
service = bind(SupportsWttrIn, backend)
# 🚀 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.0a1.tar.gz
(19.7 kB
view hashes)
Built Distribution
combadge-0.1.0a1-py3-none-any.whl
(30.8 kB
view hashes)
Close
Hashes for combadge-0.1.0a1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d141ab0ac88b62e75a1d24b50794dd025a43f7aca5c5da4f589fbdc559dcc7e9 |
|
MD5 | 9e4a1ded1482aaf8dd9bcea8dfe348d0 |
|
BLAKE2b-256 | 7322d5d69545559a3a8212ef7666e15ee75d18175db22c1bfd1e629b3320e0a2 |