Skip to content

Commit 440a41f

Browse files
committed
hit api method
Signed-off-by: Isaac Milarsky <[email protected]>
1 parent 0e25d9a commit 440a41f

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

codejson_index_generator/parsers.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,77 @@
11
import json
2+
from json.decoder import JSONDecodeError
23
import base64
34
import argparse
45
import os
6+
import requests
57

8+
from time import sleep, mktime, gmtime, time, localtime
69
from typing import Dict, Optional
7-
from github import Github, Repository, GithubException, Organization
10+
11+
RETRIES = 5
12+
13+
14+
def hit_endpoint(url,token,method='GET'):
15+
headers = {"Authorization": f"bearer {token}"}
16+
17+
attempts = 0
18+
while attempts < RETRIES:
19+
20+
response = requests.request(method, url, headers=headers,timeout=10)
21+
22+
try:
23+
if response.status_code == 200:
24+
response_json = json.loads(response.text)
25+
break
26+
elif response.status_code in (403,429):
27+
#rate limit was triggered.
28+
wait_until = int(response.headers.get("x-ratelimit-reset"))
29+
wait_in_seconds = int(
30+
mktime(gmtime(wait_until)) -
31+
mktime(gmtime(time()))
32+
)
33+
wait_until_time = localtime(wait_until)
34+
35+
print(f"Ran into rate limit sleeping for {self.name}!")
36+
print(
37+
f"sleeping until {wait_until_time.tm_hour}:{wait_until_time.tm_min} ({wait_in_seconds} seconds)"
38+
)
39+
sleep(wait_in_seconds)
40+
41+
response_json = {}
42+
attempts += 1
43+
44+
if attempts >= REQUEST_RETRIES:
45+
raise ConnectionError(
46+
f"Rate limit was reached and couldn't be rectified after {attempts} tries"
47+
)
48+
else:
49+
raise ConnectionError("Rate limit error!")
50+
except JSONDecodeError:
51+
response_json = {}
52+
attempts += 1
53+
54+
return response_json
55+
56+
57+
58+
859

960

1061
class IndexGenerator:
11-
def __init__(self, agency: str, verison: str, token: Optional[str] = None,):
12-
self.github = Github(token) if token else Github()
62+
def __init__(self, agency: str, version: str, token: Optional[str] = None,):
1363

14-
# user can change agency and version depending on paramters
64+
# user can change agency and version depending on parameters
1565
self.index = {
1666
"agency": agency,
17-
"version": verison,
67+
"version": version,
1868
"measurementType": {
1969
"method": "projects"
2070
},
2171
"releases": []
2272
}
2373

24-
def get_code_json(self, repo: Repository) -> Optional[Dict]:
74+
def get_code_json(self, repo: str) -> Optional[Dict]:
2575
try:
2676
content = repo.get_contents("code.json", ref = repo.default_branch)
2777
except GithubException as e:
@@ -35,7 +85,7 @@ def get_code_json(self, repo: Repository) -> Optional[Dict]:
3585
print(f"JSON Error: {str(e)}")
3686
return None
3787

38-
def save_code_json(self, repo: Repository, output_path: str) -> Optional[str]:
88+
def save_code_json(self, repo: str, output_path: str) -> Optional[str]:
3989

4090
res = self.get_code_json(repo)
4191

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def main():
4040
try:
4141
indexGen = IndexGenerator(
4242
agency = args.agency,
43-
verison = args.version,
43+
version = args.version,
4444
token = github_key
4545
)
4646

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repository = "https://github.com/DSACMS/codejson-index-generator"
99

1010
[tool.poetry.dependencies]
1111
python = "^3.13"
12-
pygithub = ">=1.59,<2.0"
12+
requests = "^2.32.4"
1313

1414

1515
[build-system]

0 commit comments

Comments
 (0)