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.
Documentation
Sneak peek
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.5.0.tar.gz
(24.6 kB
view details)
Built Distribution
combadge-4.5.0-py3-none-any.whl
(38.1 kB
view details)
File details
Details for the file combadge-4.5.0.tar.gz
.
File metadata
- Download URL: combadge-4.5.0.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82e46040c020c22896934cd9fd2edc3f5f53be521e9811563489288304802f6f |
|
MD5 | b7d71dd78fa81ffbd9acfd714ce95217 |
|
BLAKE2b-256 | 1db8caf01508b353ca0b8553fbfa9f20cdb7aaf58e3f316a4269b8f478b3eadb |
File details
Details for the file combadge-4.5.0-py3-none-any.whl
.
File metadata
- Download URL: combadge-4.5.0-py3-none-any.whl
- Upload date:
- Size: 38.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54d9fdfbe7caea10e4c97f87a52dd651ef1be8d6ab0c4de48b80053b735e9d35 |
|
MD5 | 29c8aae97b83e0b3f562e7785bd0c78f |
|
BLAKE2b-256 | 435a5bce4343a7a1ead459ade3f97c8d3bffbc9513a7d8058be1b59cbc7642af |