Skip to content

Commit

Permalink
API调用失败应抛出ActionFailed (#17)
Browse files Browse the repository at this point in the history
* 更新依赖版本

* API调用失败应抛出ActionFailed
  • Loading branch information
ssttkkl authored Oct 26, 2022
1 parent 343e0c8 commit 64df6a7
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 392 deletions.
35 changes: 13 additions & 22 deletions nonebot/adapters/kaiheila/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .config import Config as KaiheilaConfig
from .event import *
from .event import OriginEvent
from .exception import NetworkError, ApiNotAvailable, ReconnectError, TokenError
from .exception import ApiNotAvailable, ReconnectError, TokenError, ActionFailed
from .message import Message, MessageSegment
from .utils import ResultStore, log, _handle_api_result

Expand Down Expand Up @@ -114,30 +114,21 @@ async def _call_api(self, bot: Bot, api: str, **data) -> Any:
timeout=self.config.api_timeout,
)

try:
response = await self.driver.request(request)
if 200 <= response.status_code < 300:
if not response.content:
raise ValueError("Empty response")
result = json.loads(response.content)
return _handle_api_result(result)
response = await self.driver.request(request)
if 200 <= response.status_code < 300:
if not response.content:
raise ValueError("Empty response")
result = json.loads(response.content)
return _handle_api_result(result)
else:
# API调用失败也可能返回非200的状态码(如403)
# 尝试输出更为详细的信息
try:
# 尝试输出更为详细的信息
result = json.loads(response.content)
raise NetworkError(
f"HTTP request received unexpected "
f"status code: {response.status_code} "
"({})".format(result.get("data"))
)
return _handle_api_result(result)
except json.decoder.JSONDecodeError:
raise NetworkError(
f"HTTP request received unexpected "
f"status code: {response.status_code} "
)
except NetworkError:
raise
except Exception as e:
raise NetworkError("HTTP request failed") from e
raise ActionFailed(code=response.status_code)

else:
raise ApiNotAvailable

Expand Down
8 changes: 2 additions & 6 deletions nonebot/adapters/kaiheila/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ async def send_msg(

return await self.call_api(api, **params)

async def upload_file(self, file: Union[str, PathLike[str], BinaryIO, bytes,
Tuple[str, Union[str, PathLike[str], BinaryIO, bytes], str]],
async def upload_file(self, file: Union[str, PathLike[str], BinaryIO, bytes],
filename: Optional[str] = None) -> str:
"""
上传文件。
Expand All @@ -340,9 +339,6 @@ async def upload_file(self, file: Union[str, PathLike[str], BinaryIO, bytes,
返回值:
文件的 URL
"""
# 旧版本API是直接把三元组传参到file,这里处理兼容性
# 经过测试,服务器会用从文件读取到的mime覆盖掉我们传过去的mime
if not isinstance(file, Sequence) or len(file) != 3:
file = (filename or "upload-file", file, "application/octet-stream")
file = (filename or "upload-file", file, "application/octet-stream")
result = await self.call_api("asset/create", file=file)
return result.get("url")
Loading

0 comments on commit 64df6a7

Please sign in to comment.