Quick Start#

Create your models.#

Use the Pydantic models to create your custom models.

from pydantic import BaseModel
from pydantic.fields import Field

class Pet(BaseModel):
    identifier: int = Field(alias="id")
    name: str

Create your exceptions.#

Extend from lima_api.LimaException to create your custom error classes.

import lima_api

class PetNotFoundError(lima_api.LimaException):
    detail = "Pet not found"

class InvalidDataMessage(BaseModel):
    message: str
    code: str

class InvalidDataError(lima_api.LimaException):
    model = InvalidDataMessage

Create your client.#

Extend from lima_api.SyncLimaApi or lima_api.LimaApi to create you sync or async client.

import lima_api
...

class PetApi(lima_api.LimaApi):
    response_mapping = {
        404: PetNotFoundError,
    }

Caution

  • Synchronous clients only support synchronous functions, and in the same way with asynchronous.

  • You could see other code examples at docs/examples folder.

Create functions.#

In order to call to some entrypoint you should create your function with the proper decorator.

import lima_api
...

class PetApi(lima_api.LimaApi):
    ...

    @lima_api.get(
        "/pet/{petId}",
        response_mapping={
            400: InvalidDataError,
        }
    )
    async def get_pet(self, *, petId: int) -> Pet:
        ...

Attention

  • The Body param must be allways BaseModel class and only one is valid

  • Functions wrapped by lima_api always must use * (Keyword-Only), in order to force use keywords for calling functions.

  • All parameters must be typed.

Instance your client.#

For make connection to the remote server you need to start the connection. You could do that ussing with statement as below:

pet_client = PetApi("https://petstore.swagger.io/v2")
async with pet_client:
    pet = await pet_client.get_pet(pet_id=1)