Utilities

Rate limiter classes.

These are basically callables that when called register that a request was issued. Depending on how they are configured that may cause a pause or exception if a rate limit has been exceeded. Obviously it is up to the calling code to ensure that these callables are invoked with every (successful?) call to the backend API. (There is probably a better way to hook these into the requests library directly … TBD.)

From the Strava docs:

Strava API usage is limited on a per-application basis using a short term, 15 minute, limit and a long term, daily, limit. The default rate limit allows 600 requests every 15 minutes, with up to 30,000 requests per day.

This limit allows applications to make 40 requests per minute for about half the day.

stravalib.util.limiter.total_seconds(td)[source]

Alternative to datetime.timedelta.total_seconds total_seconds() only available since Python 2.7 https://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds

class stravalib.util.limiter.RequestRate(short_usage, long_usage, short_limit, long_limit)

Bases: tuple

long_limit

Alias for field number 3

long_usage

Alias for field number 1

short_limit

Alias for field number 2

short_usage

Alias for field number 0

stravalib.util.limiter.get_rates_from_response_headers(headers)[source]

Returns a namedtuple with values for short - and long usage and limit rates found in provided HTTP response headers :param headers: HTTP response headers :type headers: dict :return: namedtuple with request rates or None if no rate-limit headers present in response. :rtype: Optional[RequestRate]

stravalib.util.limiter.get_seconds_until_next_quarter(now=None)[source]

Returns the number of seconds until the next quarter of an hour. This is the short-term rate limit used by Strava. :param now: A (utc) timestamp :type now: arrow.arrow.Arrow :return: the number of seconds until the next quarter, as int

stravalib.util.limiter.get_seconds_until_next_day(now=None)[source]

Returns the number of seconds until the next day (utc midnight). This is the long-term rate limit used by Strava. :param now: A (utc) timestamp :type now: arrow.arrow.Arrow :return: the number of seconds until next day, as int

class stravalib.util.limiter.XRateLimitRule(limits, force_limits=False)[source]

Bases: object

property limit_timeout
class stravalib.util.limiter.SleepingRateLimitRule(priority='high', short_limit=10000, long_limit=1000000, force_limits=False)[source]

Bases: object

A rate limit rule that can be prioritized and can dynamically adapt its limits based on API responses. Given its priority, it will enforce a variable “cool-down” period after each response. When rate limits are reached within their period, this limiter will wait until the end of that period. It will NOT raise any kind of exception in this case.

class stravalib.util.limiter.RateLimitRule(requests, seconds, raise_exc=False)[source]

Bases: object

class stravalib.util.limiter.RateLimiter[source]

Bases: object

class stravalib.util.limiter.DefaultRateLimiter[source]

Bases: stravalib.util.limiter.RateLimiter

Implements something similar to the default rate limit for Strava apps.

To do this correctly we would actually need to change our logic to reset the limit at midnight, etc. Will make this more complex in the future.

Strava API usage is limited on a per-application basis using a short term, 15 minute, limit and a long term, daily, limit. The default rate limit allows 600 requests every 15 minutes, with up to 30,000 requests per day.