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 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.4.1.tar.gz
(24.5 kB
view details)
Built Distribution
combadge-4.4.1-py3-none-any.whl
(38.0 kB
view details)
File details
Details for the file combadge-4.4.1.tar.gz
.
File metadata
- Download URL: combadge-4.4.1.tar.gz
- Upload date:
- Size: 24.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f13ae8036e1de59766625011079410c567d7d5594e9d188cef458b3a809fd83 |
|
MD5 | cda6cca2c21af772cb283a365e8c5c82 |
|
BLAKE2b-256 | 6757601639467a96f84bf918e0b620bd84ed6ae95a759de055bae8581ae809f5 |
File details
Details for the file combadge-4.4.1-py3-none-any.whl
.
File metadata
- Download URL: combadge-4.4.1-py3-none-any.whl
- Upload date:
- Size: 38.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f454073f4b83841d6b026068a0881ac6173fb7229bd7b68d216a1b20da57d273 |
|
MD5 | 808f102e405733409418ef5b61a1a264 |
|
BLAKE2b-256 | 88fd3b08689eee9add99457c6a55904550e13dc0e51dadf09f1c0b89ce6bd214 |