|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf8 -*- |
3 | 3 |
|
4 | | -""" |
5 | | -Authenticate as a GitHub App: |
6 | | -https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/#authenticating-as-a-github-app |
7 | | -""" |
8 | | - |
9 | 4 | import os |
10 | 5 | import time |
11 | 6 |
|
@@ -40,24 +35,33 @@ def __call__(self, r): |
40 | 35 | return r |
41 | 36 |
|
42 | 37 |
|
43 | | -def read_private_key(): |
44 | | - with open(os.environ['PRIVATE_KEY_FILE']) as fp: |
45 | | - private_key = fp.read() |
46 | | - return private_key |
| 38 | +class GitHubApp(): |
| 39 | + session = None |
| 40 | + private_key = None |
47 | 41 |
|
| 42 | + def __init__(self): |
| 43 | + self.session = requests.Session() |
| 44 | + self.session.auth = JWTAuth( |
| 45 | + iss=os.environ['APP_ID'], |
| 46 | + key=self.read_private_key()) |
| 47 | + self.session.headers.update(dict( |
| 48 | + accept='application/vnd.github.machine-man-preview+json')) |
48 | 49 |
|
49 | | -def authenticate(): |
50 | | - authorization = JWTAuth( |
51 | | - iss=os.environ['APP_ID'], |
52 | | - key=read_private_key()) |
| 50 | + def read_private_key(self): |
| 51 | + if self.private_key is None: |
| 52 | + with open(os.environ['PRIVATE_KEY_FILE']) as fp: |
| 53 | + self.private_key = fp.read() |
| 54 | + return self.private_key |
53 | 55 |
|
54 | | - response = requests.get('https://api.github.com/app', |
55 | | - auth=authorization, |
56 | | - headers=dict(accept='application/vnd.github.machine-man-preview+json')) |
| 56 | + def get_app(self): |
| 57 | + return self.session.get('https://api.github.com/app') |
57 | 58 |
|
58 | | - return response |
| 59 | + def get_installations(self): |
| 60 | + return self.session.get('https://api.github.com/app/installations') |
59 | 61 |
|
60 | 62 |
|
61 | 63 | if __name__ == '__main__': |
62 | 64 | import pprint |
63 | | - pprint.pprint(authenticate().json()) |
| 65 | + app = GitHubApp() |
| 66 | + pprint.pprint(app.get_app().json()) |
| 67 | + pprint.pprint(app.get_installations().json()) |
0 commit comments