Doist / todoist-api-python

A python wrapper for the Todoist REST API.
MIT License
184 stars 20 forks source link

Automatically backoff when rate limited #38

Open iloveitaly opened 2 years ago

iloveitaly commented 2 years ago

Enhancement description

Add option to automatically use a backoff when you've hit a rate limit.

The problem it solves

Intermittent failures due to rate limiting.

Alternatives

Re-running the script.

Use case / screenshots

I've hit 429s on this this project: https://github.com/iloveitaly/todoist-scheduler/commits/master

Additional information

iloveitaly commented 9 months ago

Ran into this again, would be great if this was built in.

iloveitaly commented 9 months ago

Here's how I did it:

# backoff 5xx errors
def patch_todoist_api():
    import todoist_api_python.http_requests
    import backoff
    import requests

    patch_targets = ["delete", "get", "json", "post"]
    for target in patch_targets:
        original_function = getattr(todoist_api_python.http_requests, target)

        setattr(
            todoist_api_python.http_requests,
            f"original_{target}",
            original_function,
        )

        patched_function = backoff.on_exception(
            backoff.expo, requests.exceptions.HTTPError
        )(original_function)

        setattr(
            todoist_api_python.http_requests,
            target,
            patched_function,
        )