https://github.com/kpn/combadge
Generic API clients based on Pydantic and protocols
https://github.com/kpn/combadge
annotations api api-client httpx pydantic python python3 soap soap-client typed zeep
Last synced: 3 months ago
JSON representation
Generic API clients based on Pydantic and protocols
- Host: GitHub
- URL: https://github.com/kpn/combadge
- Owner: kpn
- License: apache-2.0
- Created: 2023-01-26T17:09:19.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-10T20:00:24.000Z (3 months ago)
- Last Synced: 2025-04-10T21:19:04.627Z (3 months ago)
- Topics: annotations, api, api-client, httpx, pydantic, python, python3, soap, soap-client, typed, zeep
- Language: Python
- Homepage: https://kpn.github.io/combadge
- Size: 1010 KB
- Stars: 12
- Watchers: 4
- Forks: 2
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: CODEOWNERS
- Support: docs/support/backends/httpx.md
Awesome Lists containing this project
README
# Combadge
Combadge generates a service client implementation from a user service interface
declared by a [protocol](https://peps.python.org/pep-0544/) class or an abstract base class.[](https://github.com/kpn/combadge/actions/workflows/check.yaml)
[](https://codecov.io/gh/kpn/combadge)

[](https://pypi.org/project/combadge/)
[](LICENSE)## Documentation
## Sneak peek
```python title="quickstart_httpx.py"
from httpx import Client
from pydantic import BaseModel, Field
from typing_extensions import Annotated, Protocolfrom 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
```