dropbox / dropbox-sdk-python

The Official Dropbox API V2 SDK for Python
https://www.dropbox.com/developers
MIT License
935 stars 317 forks source link

Query for rate limit on Dropbox Business / Teams account via Python SDK? #449

Closed luckman212 closed 2 years ago

luckman212 commented 2 years ago

Is it possible to query for the current # of API calls (and API limit) a Dbx Advanced/Teams account has made within the month?

Versions

greg-db commented 2 years ago

The Dropbox API doesn't offer a way to query a team's currently monthly data transport limit usage, but I'll pass this along as a feature request. I can't promise if or when that might be implemented though. You can query the team's data transport limit itself though, using team_features_get_values, like this:

dbx = dropbox.DropboxTeam(ACCESS_TOKEN)

print(dbx.team_features_get_values(features=[dropbox.team.Feature.upload_api_rate_limit]))

(Note that this is unrelated to the general rate limiting system that applies to all account types and operates on much shorter time scales. It is not possible to query the usages/limits for that general system.)

luckman212 commented 2 years ago

Ok thanks @greg-db

Actually it's the latter (the short term rate limiting system) data that I was after, I was hoping to have a way to check when our account was being rate-limited. Is there a reason that data is not exposed anywhere?

greg-db commented 2 years ago

The general rate limiting system operates on much shorter time frames, generally just seconds or minutes, and there is a system of limits, not just one single one, so the Dropbox API does not offer a way to query for that or have specific rate numbers documented. Apps should instead be written to handle these rate limit responses automatically. Also note that not all responses with a 429 or 503 status code indicate explicit rate limiting, but in any case that you get a response with 429 or 503 status code the best practice is to retry the request, respecting the Retry-After header if given in the response, or using an exponential back-off, if not. I recommend referring to the error documentation and Error Handling Guide for more information.

The Python SDK in particular returns the Retry-After value in the backoff in the RateLimitError exception, and also offers some automatic retrying behavior that you can control using the max_retries_on_rate_limit parameter Dropbox and DropboxTeam.

luckman212 commented 2 years ago

Okay, thank you for the help.