88from stravaclient .models .activity import UpdatableActivity
99
1010
11+ class RateLimitExceededError (Exception ):
12+ status_code = 429
13+
14+
1115class 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