-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestApiCalls.py
More file actions
84 lines (64 loc) · 2 KB
/
testApiCalls.py
File metadata and controls
84 lines (64 loc) · 2 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'''
Note:
When trying to run this project, the regular jwt-library must be installed.
Furthermore the pyJWT library shall be installed.
-> pip install jwt
-> pip install pyJWT
'''
import jwt
import time
import sys
import requests
import base64
import os
github_base_url = "https://api.github.com/"
pem = "privateKey.pem"
# Open PEM
with open(pem, 'rb') as pem_file:
signing_key = jwt.jwk_from_pem(pem_file.read())
payload = {
# Issued at time
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
# GitHub App's identifier
'iss': os.getenv("GH_APP_ID")
}
# Create JWT
jwt_instance = jwt.JWT()
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')
print(f"JWT: {encoded_jwt}")
# Get information
test_req = requests.get(github_base_url + "app/installations", headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + encoded_jwt,
"X-GitHub-Api-Version": "2022-11-28"
})
print(test_req.json())
# List repos
repo_req = requests.get(github_base_url + "orgs/" + os.getenv("ORGANIZATION") + "/repos", headers={
"Accept": "application/vnd.github+json",
"Authorization": encoded_jwt,
"X-GitHub-Api-Version": "2022-11-28"
})
print(repo_req.json())
# Create a blob
blob_req = requests.post(github_base_url + "repos/" + os.getenv("ORGANIZATION") + "/" + os.getenv("REPO") + "/git/blobs", headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + encoded_jwt,
"X-GitHub-Api-Version": "2022-11-28"
}, json={
"owner": os.getenv("ORGANIZATION"),
"repo": os.getenv("REPO"),
"content": "test",
"encoding": 'utf-8',
"Content-Type": "multipart/form-data"
})
print(blob_req.json())
# List installations
installations_req = requests.get(github_base_url + "app/installations", headers={
"Accept": "application/vnd.github+json",
"Authorization": "token " + os.getenv('PERSONAL_PRIVATE_KEY'),
"X-GitHub-Api-Version": "2022-11-28"
})
print(installations_req.json())