From eaf4356f6b4288f2ed3199c694fffc25f7eb7bf6 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 15 Jun 2021 22:26:08 +0800 Subject: [PATCH] :bug: fix unclosed requester warning --- src/libs/github/models/__init__.py | 8 ++++++- src/libs/github/request.py | 8 ++++++- src/plugins/github/libs/issue/__init__.py | 9 +++----- src/plugins/github/libs/issue/render.py | 26 +++++++++++------------ src/plugins/github/libs/repo.py | 6 ++---- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/libs/github/models/__init__.py b/src/libs/github/models/__init__.py index a8dfde2..2f58475 100644 --- a/src/libs/github/models/__init__.py +++ b/src/libs/github/models/__init__.py @@ -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 diff --git a/src/libs/github/request.py b/src/libs/github/request.py index 5ae5f16..1833770 100644 --- a/src/libs/github/request.py +++ b/src/libs/github/request.py @@ -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: diff --git a/src/plugins/github/libs/issue/__init__.py b/src/plugins/github/libs/issue/__init__.py index 18694bc..969a53a 100644 --- a/src/plugins/github/libs/issue/__init__.py +++ b/src/plugins/github/libs/issue/__init__.py @@ -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"} diff --git a/src/plugins/github/libs/issue/render.py b/src/plugins/github/libs/issue/render.py index ea657f8..cf33740 100644 --- a/src/plugins/github/libs/issue/render.py +++ b/src/plugins/github/libs/issue/render.py @@ -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)) diff --git a/src/plugins/github/libs/repo.py b/src/plugins/github/libs/repo.py index d99282f..bcb8e7f 100644 --- a/src/plugins/github/libs/repo.py +++ b/src/plugins/github/libs/repo.py @@ -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()