55import requests
66from requests import Response
77
8+ from .cache import cache
89from ..lib import __version__
910
1011BASE_URL = "https://api.github.com"
@@ -22,10 +23,20 @@ def __init__(
2223 ),
2324 ):
2425 self .user_agent = user_agent
26+ self .cache = cache
2527
2628 def get (
27- self , url : str , params : t .Optional [dict ] = None , return_response : bool = False
29+ self ,
30+ url : str ,
31+ params : t .Optional [dict ] = None ,
32+ return_response : bool = False ,
33+ use_cache : bool = True ,
2834 ) -> t .Union [dict , list , Response ]:
35+ if use_cache and not return_response :
36+ cached = self .cache .get (url , params )
37+ if cached is not None :
38+ return cached
39+
2940 response = requests .get (
3041 url = url , params = params , headers = {"User-Agent" : self .user_agent }
3142 )
@@ -34,33 +45,51 @@ def get(
3445 return response
3546
3647 if response .status_code == 200 :
37- return self ._sanitise_response (response = response .json ())
48+ sanitised = self .sanitise_response (response = response .json ())
49+
50+ # Cache the successful response
51+ if use_cache :
52+ self .cache .set (url , sanitised , params )
53+
54+ return sanitised
55+
3856 return []
3957
4058 def is_valid_entity (
41- self , entity_type : t .Literal ["user" , "org" , "repo" ], ** kwargs
59+ self , _type : t .Literal ["user" , "org" , "repo" ], ** kwargs
4260 ) -> bool :
4361 """Validate if a GitHub entity exists."""
4462 try :
45- if entity_type == "user" :
46- url = f"https://api.github.com/users/{ kwargs .get ('username' )} "
47- elif entity_type == "org" :
48- url = f"https://api.github.com/orgs/{ kwargs .get ('username' )} "
49- elif entity_type == "repo" :
50- url = f"https://api.github.com/repos/{ kwargs .get ('repo_owner' )} /{ kwargs .get ('repo_name' )} "
51- else :
63+ type_map = {
64+ "user" : f"https://api.github.com/users/{ kwargs .get ('username' )} " ,
65+ "org" : f"https://api.github.com/orgs/{ kwargs .get ('username' )} " ,
66+ "repo" : f"https://api.github.com/repos/{ kwargs .get ('repo_owner' )} /{ kwargs .get ('repo_name' )} " ,
67+ }
68+
69+ url = type_map [_type ]
70+
71+ # Check cache first
72+ cached = self .cache .get (url )
73+ if cached is not None :
74+ return True # If cached, entity exists
75+
76+ response = requests .get (url = url , headers = {"User-Agent" : self .user_agent })
77+
78+ # Only cache if entity exists (status 200)
79+ if response .status_code == 200 :
80+ sanitised = self .sanitise_response (response .json ())
81+ self .cache .set (url , sanitised )
5282 return True
5383
54- response = self .get (url = url , return_response = True )
55- return response .status_code == 200
84+ return False
5685 except requests .RequestException :
5786 return False
5887
59- def _sanitise_response (self , response : t .Union [dict , list ]) -> t .Union [dict , list ]:
88+ def sanitise_response (self , response : t .Union [dict , list ]) -> t .Union [dict , list ]:
6089 pattern = re .compile (r"https://api\.github\.com" )
6190
6291 if isinstance (response , list ):
63- return [self ._sanitise_response (response = item ) for item in response ]
92+ return [self .sanitise_response (response = item ) for item in response ]
6493
6594 if isinstance (response , dict ):
6695 keys_to_remove = [
@@ -74,6 +103,6 @@ def _sanitise_response(self, response: t.Union[dict, list]) -> t.Union[dict, lis
74103 # Recursively clean nested dicts/lists
75104 for key , value in response .items ():
76105 if isinstance (value , (dict , list )):
77- response [key ] = self ._sanitise_response (response = value )
106+ response [key ] = self .sanitise_response (response = value )
78107
79108 return response
0 commit comments