1515from ._app_base import GithubBaseApp
1616
1717
18+ @contextlib .asynccontextmanager
1819async def call_rest (method , url , * , body = None , preview = None , bearer = None ):
1920 """
2021 Performs an API request, in the form of GitHub v3 API.
@@ -40,7 +41,8 @@ async def call_rest(method, url, *, body=None, preview=None, bearer=None):
4041 if bearer :
4142 headers ['Authorization' ] = f"Bearer { bearer } "
4243
43- return await aiohttp .request (method , url , headers = headers , data = data , raise_for_status = True )
44+ async with aiohttp .request (method , url , headers = headers , data = data , raise_for_status = True ) as resp :
45+ yield resp
4446
4547
4648async def iter_pages (url , * , preview = None , bearer = None ):
@@ -53,19 +55,19 @@ async def iter_pages(url, *, preview=None, bearer=None):
5355 """
5456 nextpage = url
5557 while nextpage :
56- resp = await call_rest ('GET' , url , preview = preview , bearer = bearer )
57- yield resp
58+ async with call_rest ('GET' , url , preview = preview , bearer = bearer ) as resp :
59+ yield resp
5860
59- if 'next' not in resp .links :
60- break
61+ if 'next' not in resp .links :
62+ break
6163
62- nexts = list (resp .links ['next' ].keys ())
63- assert 0 <= len (nexts ) <= 1
64+ nexts = list (resp .links ['next' ].keys ())
65+ assert 0 <= len (nexts ) <= 1
6466
65- if nexts :
66- nextpage = nexts [0 ]
67- else :
68- break
67+ if nexts :
68+ nextpage = nexts [0 ]
69+ else :
70+ break
6971
7072
7173class GithubApp (GithubBaseApp ):
@@ -80,21 +82,21 @@ async def get_app(self, slug):
8082
8183 https://developer.github.com/v3/apps/#get-a-single-github-app
8284 """
83- resp = await call_rest (
85+ async with call_rest (
8486 'GET' , f'/apps/{ slug } ' , bearer = self .token , preview = 'machine-man-preview'
85- )
86- return await resp .json (content_type = False )
87+ ) as resp :
88+ return await resp .json (content_type = False )
8789
8890 async def get_this_app (self ):
8991 """
9092 Get the app information for the current app
9193
9294 https://developer.github.com/v3/apps/#get-the-authenticated-github-app
9395 """
94- resp = await call_rest (
96+ async with call_rest (
9597 'GET' , '/app' , bearer = self .token , preview = 'machine-man-preview' ,
96- )
97- return await resp .json (content_type = False )
98+ ) as resp :
99+ return await resp .json (content_type = False )
98100
99101 async def iter_installations (self ):
100102 """
@@ -116,23 +118,23 @@ async def get_installation(self, installation_id):
116118
117119 https://developer.github.com/v3/apps/#get-an-installation
118120 """
119- resp = await call_rest (
121+ async with await call_rest (
120122 'GET' , f'/app/installations/{ installation_id } ' ,
121123 bearer = self .token , preview = 'machine-man-preview' ,
122- )
123- return await resp .json (content_type = False )
124+ ) as resp :
125+ return await resp .json (content_type = False )
124126
125127 async def delete_installation (self , installation_id ):
126128 """
127129 Delete an installation, by ID.
128130
129131 https://developer.github.com/v3/apps/#delete-an-installation
130132 """
131- resp = await call_rest (
133+ async with await call_rest (
132134 'DELETE' , f'/app/installations/{ installation_id } ' ,
133135 bearer = self .token , preview = ['machine-man-preview' , 'gambit-preview' ],
134- )
135- assert resp .status == 204
136+ ) as resp :
137+ assert resp .status == 204
136138
137139 async def make_installation_token (self , installation_id , * , repository_ids = None , permissions = None ):
138140 """
@@ -148,51 +150,51 @@ async def make_installation_token(self, installation_id, *, repository_ids=None,
148150 params ['repository_ids' ] = repository_ids
149151 if permissions :
150152 params ['permissions' ] = permissions
151- resp = await call_rest (
153+ async with call_rest (
152154 'POST' , f'/app/installations/{ installation_id } /access_tokens' ,
153155 body = params , bearer = self .token , preview = 'machine-man-preview' ,
154- )
155- assert resp .status == 201
156- return await resp .json (content_type = False )
156+ ) as resp :
157+ assert resp .status == 201
158+ return await resp .json (content_type = False )
157159
158160 async def get_org_installation (self , org ):
159161 """
160162 Get an installation, by organization.
161163
162164 https://developer.github.com/v3/apps/#get-an-organization-installation
163165 """
164- resp = await call_rest (
166+ async with call_rest (
165167 'GET' , f'/orgs/{ org } /installation' ,
166168 bearer = self .token , preview = 'machine-man-preview' ,
167- )
168- assert resp .status == 200
169- return await resp .json (content_type = False )
169+ ) as resp :
170+ assert resp .status == 200
171+ return await resp .json (content_type = False )
170172
171173 async def get_repo_installation (self , owner , repo ):
172174 """
173175 Get an installation, by owner and repository.
174176
175177 https://developer.github.com/v3/apps/#get-a-repository-installation
176178 """
177- resp = await call_rest (
179+ async with call_rest (
178180 'GET' , f'/repos/{ owner } /{ repo } /installation' ,
179181 bearer = self .token , preview = 'machine-man-preview' ,
180- )
181- assert resp .status == 200
182- return await resp .json (content_type = False )
182+ ) as resp :
183+ assert resp .status == 200
184+ return await resp .json (content_type = False )
183185
184186 async def get_user_installation (self , username ):
185187 """
186188 Get an installation, by user
187189
188190 https://developer.github.com/v3/apps/#get-a-user-installation
189191 """
190- resp = await call_rest (
192+ async with call_rest (
191193 'GET' , f'/users/{ username } /installation' ,
192194 bearer = self .token , preview = 'machine-man-preview' ,
193- )
194- assert resp .status == 200
195- return await resp .json (content_type = False )
195+ ) as resp :
196+ assert resp .status == 200
197+ return await resp .json (content_type = False )
196198
197199 @staticmethod
198200 async def create_app_from_manifest (code ):
@@ -201,12 +203,12 @@ async def create_app_from_manifest(code):
201203
202204 https://developer.github.com/v3/apps/#create-a-github-app-from-a-manifest
203205 """
204- resp = await call_rest (
206+ async with call_rest (
205207 'POST' , f'/app-manifests/{ code } /conversions' ,
206208 preview = 'fury-preview' ,
207- )
208- assert resp .status == 200
209- return await resp .json (content_type = False )
209+ ) as resp :
210+ assert resp .status == 200
211+ return await resp .json (content_type = False )
210212
211213 async def token_for_repo (self , owner_or_repo , repo = None , * , repo_id = None , permissions = None ):
212214 """
0 commit comments