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.
Features
- Request and response models based on pydantic
- Declarative services using
Protocol - Exception classes automatically derived from error models
- Built-in backends:
Documentation
Sneak peek
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.support.http.aliases import StatusCode
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: StatusCode[HTTPStatus]
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.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-2.0.1.tar.gz
(23.9 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-2.0.1-py3-none-any.whl
(36.9 kB
view details)
File details
Details for the file combadge-2.0.1.tar.gz.
File metadata
- Download URL: combadge-2.0.1.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b212393521c6ff54aabbee761ad962a3e0f24ffdca1283133e7db7262cba3674
|
|
| MD5 |
ae6b31dce84898ff28234fe4bef7b41f
|
|
| BLAKE2b-256 |
1a1fd6ab1d11875b884b85e1e192858ab1c1f49663ac531f14590d0f1d682af6
|
File details
Details for the file combadge-2.0.1-py3-none-any.whl.
File metadata
- Download URL: combadge-2.0.1-py3-none-any.whl
- Upload date:
- Size: 36.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1a9f77d5af6a86827e9ba29a4d0512771750fc5ae53822586200539d1b1f1c8
|
|
| MD5 |
0d1571cd65325dedfe6bfff25aba6cdc
|
|
| BLAKE2b-256 |
7f8f4327900bfd5b280f8712f0548b0e761070b1fbca25d5437b32cf2cc7c41d
|