11import json
2+ from json .decoder import JSONDecodeError
23import base64
34import argparse
45import os
6+ import requests
57
8+ from time import sleep , mktime , gmtime , time , localtime
69from 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
1061class 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
0 commit comments