9
9
import requests
10
10
11
11
12
+ class NoInstallationException (Exception ):
13
+ pass
14
+
15
+
12
16
class JWTAuth (requests .auth .AuthBase ):
13
17
def __init__ (self , iss , key , expiration = 10 * 60 ):
14
18
self .iss = iss
@@ -35,25 +39,15 @@ def __call__(self, r):
35
39
return r
36
40
37
41
38
- class GitHubApp ():
42
+ class GitHubRequest ():
39
43
session = None
40
- private_key = None
41
44
42
45
def __init__ (self ):
43
46
self .domain = os .environ .get ('GITHUB_API_DOMAIN' , 'api.github.com' )
44
47
self .session = requests .Session ()
45
- self .session .auth = JWTAuth (
46
- iss = os .environ ['APP_ID' ],
47
- key = self .read_private_key ())
48
48
self .session .headers .update (dict (
49
49
accept = 'application/vnd.github.machine-man-preview+json' ))
50
50
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
-
57
51
def _request (self , method , path ):
58
52
self .response = self .session .request (method , 'https://{}/{}' .format (self .domain , path ))
59
53
return self .response .json ()
@@ -64,22 +58,77 @@ def _get(self, path):
64
58
def _post (self , path ):
65
59
return self ._request ('POST' , path )
66
60
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
+
67
77
def get_app (self ):
68
78
return self ._get ('app' )
69
79
70
80
def get_installations (self ):
71
81
return self ._get ('app/installations' )
72
82
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
+
73
97
def get_installation_access_token (self , installation_id ):
74
98
return self ._post ('installations/{}/access_tokens'
75
99
.format (installation_id ))
76
100
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
+
77
129
if __name__ == '__main__' :
78
130
import pprint
79
131
80
132
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