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

Commit b48ccd2

Browse files
committed
Refactor into GitHubApp class
1 parent ee9c264 commit b48ccd2

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

auth.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33

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-
94
import os
105
import time
116

@@ -40,24 +35,33 @@ def __call__(self, r):
4035
return r
4136

4237

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
4741

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'))
4849

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
5355

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')
5758

58-
return response
59+
def get_installations(self):
60+
return self.session.get('https://api.github.com/app/installations')
5961

6062

6163
if __name__ == '__main__':
6264
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

Comments
 (0)