Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logic to Handle if Key Encounters GitHub API Rate Limit #268

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
patch BaseMetric to handle ratelimits
Signed-off-by: Isaac Milarsky <isaac.milarsky@hhs.cms.gov>
IsaacMilarky committed Oct 16, 2024
commit 4ea02e0274db3f92164a8990f8432a03f4309415
61 changes: 43 additions & 18 deletions scripts/metricsLib/metrics_data_structures.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import json
from json.decoder import JSONDecodeError
import datetime
from time import sleep, mktime, gmtime, time, localtime
from functools import reduce
import operator
import requests
@@ -75,25 +76,49 @@ def hit_metric(self, params=None):
endpoint_to_hit = self.url.format(**params)
request_params = None

if self.headers:
_args_ = (self.method, endpoint_to_hit)
_kwargs_ = {
"params": request_params,
"headers": self.headers,
"timeout": TIMEOUT_IN_SECONDS
}
response = requests.request(*_args_, **_kwargs_)
else:
response = requests.request(
self.method, endpoint_to_hit, params=request_params, timeout=TIMEOUT_IN_SECONDS)

try:
if response.status_code == 200:
response_json = json.loads(response.text)
attempts = 0

while attempts < 10:
if self.headers:
_args_ = (self.method, endpoint_to_hit)
_kwargs_ = {
"params": request_params,
"headers": self.headers,
"timeout": TIMEOUT_IN_SECONDS
}
response = requests.request(*_args_, **_kwargs_)
else:
raise ConnectionError(f"Non valid status code {response.status_code}!")
except JSONDecodeError:
response_json = {}
response = requests.request(
self.method, endpoint_to_hit, params=request_params, timeout=TIMEOUT_IN_SECONDS)

try:
if response.status_code == 200:
IsaacMilarky marked this conversation as resolved.
Show resolved Hide resolved
response_json = json.loads(response.text)
break

#check for rate limit response
if response.status_code in (403,429):
#rate limit was triggered.
wait_until = int(response.headers.get("x-ratelimit-reset"))
wait_in_seconds = int(
mktime(gmtime(wait_until)) -
mktime(gmtime(time()))
)
wait_until_time = localtime(wait_until)

print(f"Ran into rate limit sleeping for {self.name}!")
print(
f"sleeping until {wait_until_time.tm_hour}:{wait_until_time.tm_min} ({wait_in_seconds} seconds)"
IsaacMilarky marked this conversation as resolved.
Show resolved Hide resolved
)
sleep(wait_in_seconds)

response_json = {}
attempts += 1
else:
raise ConnectionError(f"Non valid status code {response.status_code}!")
except JSONDecodeError:
response_json = {}
attempts += 1

return response_json