Retry processor#

Added in version 1.4.0: Support for retry when exceptions are raised

All retry processors must be instanced of lima_api.core.LimaRetryProcessor.

That processors allow is adding automatic actions for your failed request, in order to you could fix the issue and retry.

Use it#

You could use at top level client or in a function by retry_mapping:

class AsyncClient(lima_api.LimaApi):
    retry_mapping = {UNAUTHORIZED: lima_api.retry_processors.AutoLoginProcessor}

    @lima_api.get(
        "/healthcheck",
        retry_mapping={TOO_MANY_REQUESTS: lima_api.retry_processors.RetryAfterProcessor},
    )
    async def healthcheck(self) -> None: ...

You could extend from lima_api.core.LimaRetryProcessor to create you own retry processors.

class lima_api.core.LimaRetryProcessor[source]#

Bases: object

Added in version 1.4.0: Support for retry when exceptions are raised

All retry processors must be instanced of lima_api.core.LimaRetryProcessor.

That processors allow is adding automatic actions for your failed request, in order to you could fix the issue and retry.

max_retry: int = 1#

Max number of retries before raise the exception.

__init__()[source]#
do_retry(client: LimaApi | SyncLimaApi, exception: LimaException) bool[source]#

Check before call process.

Increment the self.retry_count counter.

In case that False is returned self.process never will call and request will not be retried.

async process(client: LimaApi, exception: LimaException) bool[source]#

Only called in async clients. In sync clients process must be made on the self.do_retry method.

Do the process required and return True if you want retry.

How it works#

When a request is created if some exception is raised, the system will check the status code and the dict of retry_mapping.

  1. If exists the status a new class is created or reused (this instance is per request not for all client).

  2. Function do_retry is called and if returns False the exception will be raised in other case process will be called.

  3. If process returns False the exception will be raised in other case the request will be retried.

Note

In sync clients you must make the process flow on do_retry because process will not be called in a sync clients.

Builds processors#

RetryAfterProcessor#

Located at lima_api.retry_processors.RetryAfterProcessor.

class lima_api.retry_processors.RetryAfterProcessor[source]#

Bases: LimaRetryProcessor

HTTP_DATE = '%a, %d %b %Y %H:%M:%S %Z'#
max_retry: int = 5#

Max number of retries before raise the exception.

min_sleep_time = 5#
get_sleep_seconds(retry_after: str) int[source]#
do_retry(client: LimaApi | SyncLimaApi, exception: LimaException) bool[source]#

Check before call process.

Increment the self.retry_count counter.

In case that False is returned self.process never will call and request will not be retried.

async process(client: LimaApi, exception: LimaException) bool[source]#

Only called in async clients. In sync clients process must be made on the self.do_retry method.

Do the process required and return True if you want retry.

AutoLoginProcessor#

Located at lima_api.retry_processors.AutoLoginProcessor.

class lima_api.retry_processors.AutoLoginProcessor[source]#

Bases: LimaRetryProcessor

Will call to client.autologin() -> bool and retry based on the result

max_retry: int = 1#

Max number of retries before raise the exception.

do_retry(client: LimaApi | SyncLimaApi, exception: LimaException) bool[source]#

Check before call process.

Increment the self.retry_count counter.

In case that False is returned self.process never will call and request will not be retried.

async process(client: LimaApi, exception: LimaException) bool[source]#

Only called in async clients. In sync clients process must be made on the self.do_retry method.

Do the process required and return True if you want retry.