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-5.0.0.tar.gz
(25.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
combadge-5.0.0-py3-none-any.whl
(38.9 kB
view details)
File details
Details for the file combadge-5.0.0.tar.gz.
File metadata
- Download URL: combadge-5.0.0.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6548f35d8100ea3f125678fc73d76d48025f393e21533f79685f9741bf5d597
|
|
| MD5 |
361f60a1143417006ab7fccb3fd4f90d
|
|
| BLAKE2b-256 |
6fd39f1a7a7ca7e37e2fe1154c1a9830428d4ec34e941a7c99e11f9c4074b2da
|
File details
Details for the file combadge-5.0.0-py3-none-any.whl.
File metadata
- Download URL: combadge-5.0.0-py3-none-any.whl
- Upload date:
- Size: 38.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
957435e5e9879fbc428afa7071a5211ec0a89deeafca1eeff099930f8cda15d6
|
|
| MD5 |
fd7819587c3259fa7911e7e26ced6fa1
|
|
| BLAKE2b-256 |
92ba0f70577472a590774436f746062a310dfb7799dfd711cd7bee3740f24e8a
|