runtime
¤
| MODULE | DESCRIPTION |
|---|---|
annotations |
|
auth |
|
client_base |
|
middleware |
|
model |
|
paging |
|
types_ |
|
| CLASS | DESCRIPTION |
|---|---|
Body |
Link content type headers with a python type. |
Cookie |
|
Header |
Mark parameter as HTTP Header |
Metadata |
Annotation for models that hold other WebArg fields. |
Path |
|
Query |
|
Responses |
Mapping between response code, headers, media type and body type. |
ClientBase |
Base for Client classes |
HttpxMiddleware |
HTTP middleware protocol, used to fix request or response objects. |
HttpErrorResponse |
Base error class for declared HTTP error responses - 4XX & 5XX. |
LapidaryResponseError |
Base class for errors that wrap the response |
UnexpectedResponse |
Raised when the remote server responded with code and content-type pair that wasn't declared in the method return annotation |
FormExplode |
|
| FUNCTION | DESCRIPTION |
|---|---|
iter_pages |
Take a function that returns a pageg response and return a function that returns an async iterator that iterates over the pages. |
Body
dataclass
¤
Bases: WebArg
Link content type headers with a python type. When used with a method parameter, it tells lapidary what content-type header to send for a given body type. When used in return annotation, it tells lapidary the type to process the response body as.
Example use with parameter:
body: Body({'application/json': BodyModel})
Cookie
¤
Cookie(alias: str | None = None, /, *, style: type[MultimapSerializationStyle] = FormExplode)
Bases: Param
| PARAMETER | DESCRIPTION |
|---|---|
alias
|
Cookie name, if different than the name of the annotated parameter
TYPE:
|
style
|
Serialization style
TYPE:
|
Source code in src/lapidary/runtime/annotations.py
84 85 86 87 88 89 90 91 92 93 94 95 96 | |
Header
¤
Bases: Param
Mark parameter as HTTP Header
| PARAMETER | DESCRIPTION |
|---|---|
alias
|
Header name, if different than the name of the annotated parameter
TYPE:
|
style
|
Serialization style
TYPE:
|
Source code in src/lapidary/runtime/annotations.py
68 69 70 71 72 73 74 75 76 77 78 79 80 | |
Metadata
¤
Bases: WebArg
Annotation for models that hold other WebArg fields. Can be used to group request parameters as an alternative to passing parameters directly.
Example:
class RequestMetadata(pydantic.BaseModel):
my_header: typing.Annotated[
str,
Header('my-header'),
]
class Client(ApiClient):
@get(...)
async def my_method(
headers: Annotated[RequestMetadata, Metadata]
):
Path
¤
Bases: Param
| PARAMETER | DESCRIPTION |
|---|---|
alias
|
Path parameter name, if different than the name of the annotated parameter
TYPE:
|
style
|
Serialization style
TYPE:
|
Source code in src/lapidary/runtime/annotations.py
100 101 102 103 104 105 106 107 108 109 110 111 112 | |
Query
¤
Query(alias: str | None = None, /, *, style: type[MultimapSerializationStyle] = FormExplode)
Bases: Param
| PARAMETER | DESCRIPTION |
|---|---|
alias
|
Query parameter name, if different than the name of the annotated parameter
TYPE:
|
style
|
Serialization style
TYPE:
|
Source code in src/lapidary/runtime/annotations.py
116 117 118 119 120 121 122 123 124 125 126 127 128 | |
Responses
dataclass
¤
Responses(responses: Mapping[StatusCodeRange, Response])
Bases: WebArg
Mapping between response code, headers, media type and body type. The simplified structure is:
response code => (
body: content type => body model type
headers model
)
The structure follows OpenAPI 3.
| ATTRIBUTE | DESCRIPTION |
|---|---|
responses |
Map of status code match to Response.
TYPE:
|
ClientBase
¤
ClientBase(client: AsyncClient | None = None, base_url: str | None = None, *, middlewares: Sequence[HttpxMiddleware] = (), auth: Auth | None = None)
Bases: ABC
Base for Client classes
| PARAMETER | DESCRIPTION |
|---|---|
client
|
the httpx client to use
TYPE:
|
middlewares
|
list of middlewares to process HTTP requests and responses
TYPE:
|
Source code in src/lapidary/runtime/client_base.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
HttpxMiddleware
¤
HttpErrorResponse
¤
HttpErrorResponse(status_code: int, headers: Headers, body: Body)
Bases: Generic[Body, Headers], LapidaryResponseError
Base error class for declared HTTP error responses - 4XX & 5XX. Python doesn't fully support parametrized exception types, but extending types can concretize it.
Source code in src/lapidary/runtime/model/error.py
32 33 34 35 36 | |
LapidaryResponseError
¤
Bases: LapidaryError
Base class for errors that wrap the response
UnexpectedResponse
¤
UnexpectedResponse(response: Response)
Bases: LapidaryResponseError
Raised when the remote server responded with code and content-type pair that wasn't declared in the method return annotation
Source code in src/lapidary/runtime/model/error.py
42 43 44 | |
FormExplode
¤
Bases: MultimapSerializationStyle
| METHOD | DESCRIPTION |
|---|---|
serialize_object |
Disregard name, return a map of {key: value} |
serialize_object
classmethod
¤
serialize_object(_name: str, value: ObjectType) -> Multimap
Disregard name, return a map of {key: value}
Source code in src/lapidary/runtime/model/param_serialization.py
189 190 191 192 | |
iter_pages
¤
iter_pages(fn: Callable[P, Awaitable[R]], cursor_param_name: str, get_cursor: Callable[[R], C | None]) -> Callable[P, AsyncIterable[R]]
Take a function that returns a pageg response and return a function that returns an async iterator that iterates over the pages.
The returned function can be called with the same parameters as :param:fn (except for the cursor parameter),
and returns an async iterator that yields results from :param:fn, handling pagination automatically.
The function :param:fn will be called initially without the cursor parameter and then called with the cursor parameter
as long as :param:get_cursor can extract a cursor from the result.
Example:
async for page in iter_pages(client.fn, 'cursor', extractor_fn)(parameter=value):
# Process page
Typically, an API will use the same paging pattern for all operations supporting it, so it's a good idea to write a shortcut function:
from lapidary.runtime import iter_pages as _iter_pages
def iter_pages[P, R](fn: Callable[P, Awaitable[R]]) -> Callable[P, AsyncIterable[R]]:
return _iter_pages(fn, 'cursor', lambda result: ...)
| PARAMETER | DESCRIPTION |
|---|---|
fn
|
An async function that retrieves a page of data. |
cursor_param_name
|
The name of the cursor parameter in :param:
TYPE:
|
get_cursor
|
A function that extracts a cursor value from the result of :param:
TYPE:
|
Source code in src/lapidary/runtime/paging.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |