Skip to content

Commit a9c186a

Browse files
authored
Add convenience methods for get_throttle_limit and get_remaining_throttle_limit
1 parent 53c4d22 commit a9c186a

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

adafruit_io/adafruit_io.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,74 @@ def receive_random_data(self, generator_id: int):
855855
path = self._compose_path("integrations/words/{0}".format(generator_id))
856856
return self._get(path)
857857

858-
def receive_user_info(self):
858+
def get_user_info(self):
859859
"""
860860
Get detailed account information for the current user.
861861
862862
See https://io.adafruit.com/api/docs/#get-user-info
863863
"""
864864
return self._get("https://io.adafruit.com/api/v2/user")
865865

866-
def receive_user_rate_info(self):
866+
def get_user_rate_info(self):
867867
"""
868868
Get rate limit and usage information for the current user.
869869
870870
See https://io.adafruit.com/api/docs/#get-detailed-user-info
871+
872+
Exampple output:
873+
```
874+
{
875+
"data_rate_limit": 30,
876+
"active_data_rate": 0,
877+
"authentication_rate": 0,
878+
"subscribe_authorization_rate": 0,
879+
"publish_authorization_rate": 0,
880+
"hourly_ban_rate": 0,
881+
"mqtt_ban_error_message": null,
882+
"active_sms_rate": 0
883+
}
884+
```
871885
"""
872886
path = self._compose_path("throttle")
873887
return self._get(path)
874888

889+
def get_remaining_throttle_limit(self):
890+
"""
891+
Get the remaining data points allowed before hitting the throttle limit.
892+
This retrieves the user rate limit and deducts usage for the current user.
893+
894+
See https://io.adafruit.com/api/docs/#get-detailed-user-info
895+
"""
896+
path = self._compose_path("throttle")
897+
user_rates = self._get(path)
898+
if user_rates is None:
899+
raise Exception("Could not get user info, get_user_rate_info returned None.")
900+
return user_rates["data_rate_limit"] - user_rates["active_data_rate"]
901+
902+
def get_throttle_limit(self):
903+
"""
904+
Get user throttle limit a.k.a "data_rate_limit" for the current user.
905+
906+
See https://io.adafruit.com/api/docs/#get-detailed-user-info
907+
"""
908+
path = self._compose_path("throttle")
909+
user_rates = self._get(path)
910+
if user_rates is None:
911+
raise Exception("Could not get user info, get_user_rate_info returned None.")
912+
return user_rates["data_rate_limit"]
913+
914+
def get_current_usage(self):
915+
"""
916+
Get current rate usage a.k.a "active_data_rate" for the current user.
917+
918+
See https://io.adafruit.com/api/docs/#get-detailed-user-info
919+
"""
920+
path = self._compose_path("throttle")
921+
user_rates = self._get(path)
922+
if user_rates is None:
923+
raise Exception("Could not get user info, get_user_rate_info returned None.")
924+
return user_rates["active_data_rate"]
925+
875926
def receive_time(self, timezone: str = None):
876927
"""
877928
Returns a struct_time from the Adafruit IO Server based on the device's IP address.

0 commit comments

Comments
 (0)