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-3.0.0.tar.gz
(23.6 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-3.0.0-py3-none-any.whl
(36.5 kB
view details)
File details
Details for the file combadge-3.0.0.tar.gz.
File metadata
- Download URL: combadge-3.0.0.tar.gz
- Upload date:
- Size: 23.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.10.12 Linux/6.2.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6512fdb42907ddff83c92082a5af892733e986f5be583c7ce7981aaa8a39929
|
|
| MD5 |
b0e52afe8a88725fc9ca5a0a1a3fa7d8
|
|
| BLAKE2b-256 |
2555bb21be710664b82a32db0a80de43710a5c5e408cd853bbeb4b05e1c7b198
|
File details
Details for the file combadge-3.0.0-py3-none-any.whl.
File metadata
- Download URL: combadge-3.0.0-py3-none-any.whl
- Upload date:
- Size: 36.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.10.12 Linux/6.2.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8aac77a110a7ba7b1bb2f617743a5735458afddd314b8b7e03edc449d9fd76b2
|
|
| MD5 |
e921b440e32cd539aebe941280c412ff
|
|
| BLAKE2b-256 |
ef5fa9a6684c180541514fb1c4e3bc1487aeec42c48f09a3d788a866c71b5445
|