-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithub_api.py
More file actions
36 lines (28 loc) · 1.25 KB
/
Copy pathgithub_api.py
File metadata and controls
36 lines (28 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from utils.jwt_generator import GenerateJWT
import aiohttp
class GithubAPI:
def __init__(self):
self.headers = {
'Accept': 'application/vnd.github+json',
# 'Authorization': f'Bearer {os.getenv("GithubPAT")}'
}
return
async def authenticate_app_as_installation(self, repo_owner):
installation_id = 0
jwt = GenerateJWT().__call__()
url = "https://api.github.com/app/installations"
headers = {
"Authorization": f"Bearer {jwt}"
}
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
installations = await response.json()
for installation in installations:
if installation["account"]["login"] == repo_owner:
installation_id = installation["id"]
url = f"https://api.github.com/app/installations/{installation_id}/access_tokens"
if not installation_id:
return None
async with session.post(url, headers=headers) as token_response:
token_req = await token_response.json()
return token_req["token"]