🐛 fix unclosed requester warning

This commit is contained in:
yanyongyu 2021-06-15 22:26:08 +08:00
parent 0f4abe7b79
commit eaf4356f6b
5 changed files with 32 additions and 25 deletions

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-03-11 01:34:31
@LastEditors : yanyongyu
@LastEditTime : 2021-06-08 19:59:46
@LastEditTime : 2021-06-15 22:11:43
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -34,6 +34,12 @@ class BaseModel(_BaseModel):
if hd:
_requester.reset(hd)
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()
class Config:
extra = "allow"
arbitrary_types_allowed = True

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-03-09 17:34:53
@LastEditors : yanyongyu
@LastEditTime : 2021-03-12 13:36:50
@LastEditTime : 2021-06-15 22:14:45
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -39,6 +39,12 @@ class Requester:
self._client: Optional[httpx.AsyncClient] = None
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()
@property
def client(self) -> httpx.AsyncClient:
if not self._client:

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-03-09 16:45:25
@LastEditors : yanyongyu
@LastEditTime : 2021-05-29 14:13:41
@LastEditTime : 2021-06-15 22:20:20
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -36,12 +36,9 @@ async def get_issue(owner: str,
else:
g = Github()
try:
async with g:
repo = await g.get_repo(f"{owner}/{repo_name}", True)
issue = await repo.get_issue(number)
finally:
await g.close()
return issue
return await repo.get_issue(number)
OPTIONS: Dict[str, str] = {"encoding": "utf-8"}

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-05-14 17:09:12
@LastEditors : yanyongyu
@LastEditTime : 2021-06-08 19:33:37
@LastEditTime : 2021-06-15 22:12:28
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -78,19 +78,19 @@ env.filters["find_dismissed_review"] = find_dismissed_review
async def issue_to_html(owner: str, repo_name: str, issue: Issue) -> str:
template = env.get_template("issue.html")
timeline = await issue.get_timeline()
await issue.close()
return await template.render_async(owner=owner,
repo_name=repo_name,
issue=issue,
timeline=timeline)
async with issue:
timeline = await issue.get_timeline()
return await template.render_async(owner=owner,
repo_name=repo_name,
issue=issue,
timeline=timeline)
async def pr_diff_to_html(owner: str, repo_name: str, issue: Issue) -> str:
template = env.get_template("diff.html")
diff = await issue.get_diff()
await issue.close()
return await template.render_async(owner=owner,
repo_name=repo_name,
issue=issue,
diff=PatchSet(diff))
async with issue:
diff = await issue.get_diff()
return await template.render_async(owner=owner,
repo_name=repo_name,
issue=issue,
diff=PatchSet(diff))

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-03-12 15:36:14
@LastEditors : yanyongyu
@LastEditTime : 2021-03-16 00:59:46
@LastEditTime : 2021-06-15 22:16:20
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -27,7 +27,5 @@ async def get_repo(owner: str,
else:
g = Github()
try:
async with g:
return await g.get_repo(f"{owner}/{repo_name}", False)
finally:
await g.close()