How to properly check the rate limit vs usage

68 views
Skip to first unread message

Pieter Odink

unread,
Jun 27, 2017, 12:55:21 PM6/27/17
to Strava API
Hi,

I'm working on an application to provide running training schedules based on the users training history. For that, training history of some weeks is downloaded. For some users, this request results in an error. I assume it's because I'm not properly checking the rate limit vs the usage.

Using Pythin , I've something like:

for activity in activities_list:
  try:
    do some things
    message = 'done'
  except:
    message = 'rate_limit'

  if message == "rate_limit":
    self.redirect('/rate_limit') (--> a page which asks the user to come back in 15 minutes)

If a run the script though, the rate_limit page is never shown when the limit it there. It simply shows the error 500 page.

Any idea how to solve this properly?
Is it, for instance, possible to check within the library what the rate_limit status is?

Thanks in advance!
Pieter
  


gordon macmillan

unread,
Jul 10, 2017, 6:42:51 PM7/10/17
to Strava API
Hi Pieter: I thought I could perhaps help with this question. I'm not sure how you could get the number of calls you have made from strava without explicitly setting your own counter. What I would suggest is maybe implementing your own rate limiter. Something like this:

from stravalib.util.limiter import RateLimitRule, RateLimiter

class DefaultRateLimiter(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.
"""

def __init__(self):
super(DefaultRateLimiter, self).__init__()
self.rules.append(RateLimitRule(requests=40, seconds=60, raise_exc=False))
self.rules.append(RateLimitRule(requests=30000, seconds=(3600 * 24), raise_exc=True))


In order to use it, when you initialize your Strava client I would do something like this:

from stravalib.client import Client

class StravaUser(object):
'''
A strava user class.
'''
def __init__(self, access_token):
self.access_token = access_token

def get_client(self):
"""
The get_client method create a client object for making requests to the strava API. The Client class accepts an access_token and a rate_limiter object.
Inputs: None
Outputs: None
"""
self.client = Client(access_token=self.access_token, rate_limiter=DefaultRateLimiter())

Hope this helps!
Reply all
Reply to author
Forward
0 new messages