Python DSL for Web API clients.
Also check lapidary-render, a command line program that generates Lapidary clients from OpenAPI.
- Write Web API clients declaratively
- Use pydantic models for JSON data
- Compatibility with OpenAPI 3.0 and 3.1
pip install lapidary
or with Poetry
poetry add lapidary
With Lapidary, you define an API client by creating a class that mirrors the API structure, akin to OpenAPI but through decorated and annotated Python methods. Calling these method handles making HTTP requests and transforming the responses back into Python objects.
from typing import Annotated, Self
from lapidary.runtime import *
# Define models
class Cat(ModelBase):
id: int
name: str
# Declare the client
class CatClient(ClientBase):
def __init__(
self,
base_url='http://localhost:8080/api',
):
super().__init__(base_url=base_url)
@get('/cat/{id}')
async def cat_get(
self: Self,
*,
id: Annotated[int, Path(style=ParamStyle.simple)],
) -> Annotated[Cat, Responses({
'2XX': {
'application/json': Cat
},
})]:
pass
# User code
async def main():
client = CatClient()
cat = await client.cat_get(id=7)
See this test file for a working example.
Also check the library of clients.