8000 Refactor and introduce GitHubAppInstallation class · swinton/github-app-demo.py@becc715 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 4, 2019. It is now read-only.

Commit becc715

Browse files
committed
Refactor and introduce GitHubAppInstallation class
1 parent 6bdf0c5 commit becc715

File tree

1 file changed

+65
-16
lines changed

1 file changed

+65
-16
lines changed

auth.py

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import requests
1010

1111

12+
class NoInstallationException(Exception):
13+
pass
14+
15+
1216
class JWTAuth(requests.auth.AuthBase):
1317
def __init__(self, iss, key, expiration=10 * 60):
1418
self.iss = iss
@@ -35,25 +39,15 @@ def __call__(self, r):
3539
return r
3640

3741

38-
class GitHubApp():
42+
class GitHubRequest():
3943
session = None
40-
private_key = None
4144

4245
def __init__(self):
4346
self.domain = os.environ.get('GITHUB_API_DOMAIN', 'api.github.com')
4447
self.session = requests.Session()
45-
self.session.auth = JWTAuth(
46-
iss=os.environ['APP_ID'],
47-
key=self.read_private_key())
4848
self.session.headers.update(dict(
4949
accept='application/vnd.github.machine-man-preview+json'))
5050

51-
def read_private_key(self):
52-
if self.private_key is None:
53-
with open(os.environ['PRIVATE_KEY_FILE']) as fp:
54-
self.private_key = fp.read()
55-
return self.private_key
56-
5751
def _request(self, method, path):
5852
self.response = self.session.request(method, 'https://{}/{}'.format(self.domain, path))
5953
return self.response.json()
@@ -64,22 +58,77 @@ def _get(self, path):
6458
def _post(self, path):
6559
return self._request('POST', path)
6660

61+
62+
class GitHubApp(GitHubRequest):
63+
private_key = None
64+
65+
def __init__(self):
66+
super().__init__()
67+
self.session.auth = JWTAuth(
68+
iss=os.environ['APP_ID'],
69+
key=self.read_private_key())
70+
71+
def read_private_key(self):
72+
if self.private_key is None:
73+
with open(os.environ['PRIVATE_KEY_FILE']) as fp:
74+
self.private_key = fp.read()
75+
return self.private_key
76+
6777
def get_app(self):
6878
return self._get('app')
6979

7080
def get_installations(self):
7181
return self._get('app/installations')
7282

83+
def get_installation(self, login):
84+
installation = [installation for
85+
installation in self.get_installations()
86+
if installation['account']['login'] == login]
87+
88+
if len(installation) > 0:
89+
return GitHubAppInstallation(self,
90+
installation[0],
91+
self.get_installation_access_token(installation[0]['id']))
92+
93+
else:
94+
raise NoInstallationException(
95+
'No installation found for "{}".'.format(login))
96+
7397
def get_installation_access_token(self, installation_id):
7498
return self._post('installations/{}/access_tokens'
7599
.format(installation_id))
76100

101+
102+
class TokenAuth(requests.auth.AuthBase):
103+
def __init__(self, app, access_token):
104+
self._app = app
105+
self._access_token = access_token
106+
107+
def __call__(self, r):
108+
r.headers['Authorization'] = 'token {}'.format(self._access_token['token'])
109+
return r
110+
111+
112+
class GitHubAppInstallation(GitHubRequest):
113+
def __init__(self, app, installation_dict, access_token):
114+
super().__init__()
115+
116+
self._app = app
117+
self._dict = installation_dict
118+
self._access_token = access_token
119+
120+
for k in installation_dict:
121+
setattr(self, k, installation_dict[k])
122+
123+
self.session.auth = TokenAuth(app, access_token)
124+
125+
def get_repositories(self):
126+
return self._get('installation/repositories')
127+
128+
77129
if __name__ == '__main__':
78130
import pprint
79131

80132
app = GitHubApp()
81-
pprint.pprint(app.get_app())
82-
83-
installations = app.get_installations()
84-
pprint.pprint(installations)
85-
pprint.pprint([(installation['id'], app.get_installation_access_token(installation['id'])) for installation in installations])
133+
installation = app.get_installation('swinton')
134+
pprint.pprint(installation.get_repositories())

0 commit comments

Comments
 (0)
0