Skip to content

Commit ff6659e

Browse files
authored
✨ add configs for githubkit
1 parent 3ba1672 commit ff6659e

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

nonebot/adapters/github/adapter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from nonebot.typing import overrides
88
from githubkit.webhooks import verify
9-
from githubkit.exception import RequestFailed
9+
from githubkit.exception import RequestFailed, RequestTimeout
1010
from nonebot.drivers import (
1111
URL,
1212
Driver,
@@ -23,7 +23,7 @@
2323
from .bot import Bot, OAuthBot, GitHubBot
2424
from .message import Message, MessageSegment
2525
from .config import Config, OAuthApp, GitHubApp
26-
from .exception import ActionFailed, NetworkError
26+
from .exception import ActionFailed, NetworkError, ActionTimeout
2727

2828

2929
class Adapter(BaseAdapter):
@@ -109,8 +109,10 @@ async def _call_api(self, bot: Bot, api: str, **data: Any) -> Any:
109109
return await func(**data)
110110
except RequestFailed as e:
111111
raise ActionFailed(e.response) from None
112+
except RequestTimeout as e:
113+
raise ActionTimeout(e.request) from None
112114
except Exception as e:
113-
raise NetworkError from e
115+
raise NetworkError(f"API request failed: {e!r}") from e
114116

115117
@classmethod
116118
def payload_to_event(

nonebot/adapters/github/bot.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ async def send(
106106

107107

108108
class Bot(BaseBot):
109+
adapter: "Adapter"
109110

110111
send_handler: Callable[
111112
["Bot", Event, Union[str, Message, MessageSegment]], Any
@@ -148,7 +149,11 @@ class OAuthBot(Bot):
148149
def __init__(self, adapter: "Adapter", app: OAuthApp):
149150
super().__init__(adapter, app)
150151
self._github: GitHub[OAuthAppAuthStrategy] = GitHub(
151-
OAuthAppAuthStrategy(app.client_id, app.client_secret)
152+
OAuthAppAuthStrategy(app.client_id, app.client_secret),
153+
base_url=self.adapter.github_config.github_base_url,
154+
accept_format=self.adapter.github_config.github_accept_format,
155+
previews=self.adapter.github_config.github_previews,
156+
timeout=self.config.api_timeout,
152157
)
153158

154159
@contextmanager
@@ -188,7 +193,11 @@ def __init__(self, adapter: "Adapter", app: GitHubApp):
188193
self._github: GitHub[AppAuthStrategy] = GitHub(
189194
AppAuthStrategy(
190195
app.app_id, app.private_key, app.client_id, app.client_secret
191-
)
196+
),
197+
base_url=self.adapter.github_config.github_base_url,
198+
accept_format=self.adapter.github_config.github_accept_format,
199+
previews=self.adapter.github_config.github_previews,
200+
timeout=self.config.api_timeout,
192201
)
193202
self._app_slug: Optional[str] = None
194203

nonebot/adapters/github/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ class Config(BaseModel, extra=Extra.ignore):
3030

3131
github_apps: List[Union[GitHubApp, OAuthApp]] = Field(default_factory=list)
3232
"""Allowed GitHub App List"""
33+
github_base_url: Optional[str] = None
34+
github_accept_format: Optional[str] = None
35+
github_previews: Optional[List[str]] = None

nonebot/adapters/github/exception.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
description: onebot.v11.exception 模块
66
"""
77

8-
from githubkit.exception import RequestFailed
8+
from typing import Optional
9+
910
from nonebot.exception import AdapterException
11+
from githubkit.exception import RequestFailed, RequestTimeout
1012
from nonebot.exception import ActionFailed as BaseActionFailed
1113
from nonebot.exception import NetworkError as BaseNetworkError
12-
from nonebot.exception import NoLogException as BaseNoLogException
1314
from nonebot.exception import ApiNotAvailable as BaseApiNotAvailable
1415

1516

@@ -19,11 +20,22 @@ def __init__(self):
1920

2021

2122
class NetworkError(BaseNetworkError, GitHubAdapterException):
22-
"""网络错误。"""
23+
def __init__(self, msg: Optional[str] = None):
24+
super().__init__()
25+
self.msg: Optional[str] = msg
26+
"""错误原因"""
27+
28+
def __repr__(self):
29+
return f"<NetWorkError message={self.msg}>"
30+
31+
32+
class ActionTimeout(RequestTimeout, NetworkError):
33+
def __repr__(self) -> str:
34+
return f"<NetworkError: {self.request.method} {self.request.url}>"
2335

2436

2537
class ApiNotAvailable(BaseApiNotAvailable, GitHubAdapterException):
26-
pass
38+
...
2739

2840

2941
class ActionFailed(RequestFailed, BaseActionFailed, GitHubAdapterException):

0 commit comments

Comments
 (0)