1616)
1717
1818
19- class GitHubAPI :
20- """A simple wrapper that authenticates with and communicates with the GitHub API ."""
19+ class GitHub :
20+ """Work with authentication to GitHub repositories ."""
2121
22- GITHUB_BASE_URL = "https://api.github.com"
22+ # NOTE: This RE takes care of removing the '.git' suffix, to provide normalised URLs.
23+ GITHUB_URL_RE = re .compile (
24+ f"https://{ URL_USERINFO_RE .pattern } ?github.com/(?P<owner>[-A-Za-z0-9]+)/(?P<repo>[^/]+)(.git)?"
25+ )
2326
2427 repo_url : str
2528 repo_owner : str
2629 repo_name : str
2730 userinfo : str
28- session : requests .Session
29-
30- GITHUB_BASE_URL = "https://api.github.com"
31- # NOTE: This RE takes care of removing the '.git' suffix, to provide normalised URLs.
32- GITHUB_URL_RE = re .compile (
33- f"https://{ URL_USERINFO_RE .pattern } ?github.com/(?P<owner>[-A-Za-z0-9]+)/(?P<repo>[^/]+)(.git)?"
34- )
3531
3632 def __init__ (self , repo_url : str ):
3733 self .repo_url = repo_url
3834
39- parsed_url = self .parse_github_url (repo_url )
35+ parsed_url = self .parse_url (repo_url )
4036
4137 if parsed_url is None :
4238 raise ValueError (f"Cannot parse URL as GitHub repo: { repo_url } " )
@@ -45,22 +41,12 @@ def __init__(self, repo_url: str):
4541 self .repo_name = parsed_url ["repo" ]
4642 self .userinfo = parsed_url ["userinfo" ]
4743
48- self .session = requests .Session ()
49- self .session .headers .update (
50- {
51- "Authorization" : f"Bearer { self ._fetch_token ()} " ,
52- "User-Agent" : settings .HTTP_USER_AGENT ,
53- "Accept" : "application/vnd.github+json" ,
54- "X-GitHub-Api-Version" : "2022-11-28" ,
55- }
56- )
57-
5844 @classmethod
59- def is_github_url (cls , url : str ) -> bool :
60- return cls .parse_github_url (url ) is not None
45+ def is_supported_url (cls , url : str ) -> bool :
46+ return cls .parse_url (url ) is not None
6147
6248 @classmethod
63- def parse_github_url (cls , url : str ) -> re .Match [str ] | None :
49+ def parse_url (cls , url : str ) -> re .Match [str ] | None :
6450 """Parse GitHub data from URL, or return None if not Github."""
6551 return re .match (cls .GITHUB_URL_RE , url )
6652
@@ -112,6 +98,27 @@ def _fetch_token(self) -> str | None:
11298 )
11399 return asyncio .run (session .get_token ())
114100
101+
102+ class GitHubAPI (GitHub ):
103+ """A simple wrapper that authenticates with and communicates with the GitHub API."""
104+
105+ session : requests .Session
106+
107+ GITHUB_BASE_URL = "https://api.github.com"
108+
109+ def __init__ (self , repo_url : str ):
110+ super ().__init__ (repo_url )
111+
112+ self .session = requests .Session ()
113+ self .session .headers .update (
114+ {
115+ "Authorization" : f"Bearer { self ._fetch_token ()} " ,
116+ "User-Agent" : settings .HTTP_USER_AGENT ,
117+ "Accept" : "application/vnd.github+json" ,
118+ "X-GitHub-Api-Version" : "2022-11-28" ,
119+ }
120+ )
121+
115122 def get (self , path : str , * args , ** kwargs ) -> dict :
116123 """Send a GET request to the GitHub API with given args and kwargs."""
117124 url = f"{ self .GITHUB_BASE_URL } /{ path } "
0 commit comments