Skip to content

Commit 083f192

Browse files
authored
Merge pull request #2 from JozefTkocz/check_rate_limit_exception
Check rate limit exception
2 parents 035b308 + 34c9639 commit 083f192

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def read_requirements(requirements_file):
1616
setup(
1717
name="stravaclient",
1818
python_requires=">=3.8,<4.0",
19-
version="0.0.5",
19+
version="0.0.6",
2020
author="Jozef Tkocz",
2121
author_email="[email protected]",
2222
description=("Python client for the Strava API"),

stravaclient/endpoint_methods.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88
from stravaclient.models.activity import UpdatableActivity
99

1010

11+
class RateLimitExceededError(Exception):
12+
status_code = 429
13+
14+
1115
class StravaClient:
1216
def __init__(self, authorisation: OAuthHandler):
1317
self.authorisation = authorisation
1418

19+
@staticmethod
20+
def _validate_response_code(response: requests.Response):
21+
if response.status_code == RateLimitExceededError.status_code:
22+
raise RateLimitExceededError
23+
24+
if not response.ok:
25+
response.raise_for_status()
26+
1527
def get_athlete_info(self, athlete_id: int) -> Dict:
1628
auth_header = self.authorisation.generate_authorisation_header(athlete_id)
1729
response = requests.get(endpoints.athlete_info, headers=auth_header)
30+
self._validate_response_code(response)
1831
return response.json()
1932

2033
def list_activities(self, athlete_id: int, page: int = 1, per_page: int = 30) -> Dict:
@@ -23,16 +36,19 @@ def list_activities(self, athlete_id: int, page: int = 1, per_page: int = 30) ->
2336

2437
auth_header = self.authorisation.generate_authorisation_header(athlete_id)
2538
response = requests.get(endpoints.athlete_activities, headers=auth_header, params=request_params)
39+
self._validate_response_code(response)
2640
return response.json()
2741

2842
def get_activity(self, athlete_id: int, activity_id: int) -> Dict:
2943
auth_header = self.authorisation.generate_authorisation_header(athlete_id)
3044
response = requests.get(endpoints.activity(activity_id), headers=auth_header)
45+
self._validate_response_code(response)
3146
return response.json()
3247

3348
def update_activity(self, athlete_id, activity_id, updatable_activity: UpdatableActivity) -> Dict:
3449
auth_header = self.authorisation.generate_authorisation_header(athlete_id)
3550
response = requests.put(endpoints.activity(activity_id), headers=auth_header, json=updatable_activity.to_dict())
51+
self._validate_response_code(response)
3652
return response.json()
3753

3854
def get_activity_stream_set(self, athlete_id: int, activity_id: int, streams: List[str],
@@ -53,6 +69,7 @@ def get_activity_stream_set(self, athlete_id: int, activity_id: int, streams: Li
5369
params = {'keys': stream_keys_string,
5470
'key_by_type': 'true'}
5571
response = requests.get(endpoints.activity_streams(activity_id), headers=auth_header, params=params)
72+
self._validate_response_code(response)
5673
return response.json()
5774

5875
@staticmethod

0 commit comments

Comments
 (0)