From fc2faa805f5802afe862dad61ab59f1c4a6ceb87 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Sun, 11 Jun 2023 22:19:20 +0800 Subject: [PATCH] :bug: Fix `RegionNotSupported` Error --- simnet/client/base.py | 4 +++- simnet/client/components/diary/base.py | 2 +- simnet/client/routes.py | 21 ++++++++++++++++++--- simnet/errors.py | 13 +++++++------ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/simnet/client/base.py b/simnet/client/base.py index 8f69787..e45d0e0 100644 --- a/simnet/client/base.py +++ b/simnet/client/base.py @@ -7,7 +7,7 @@ from httpx import AsyncClient, TimeoutException, Response, HTTPError, Timeout from simnet.client.cookies import Cookies from simnet.client.headers import Headers -from simnet.errors import TimedOut, NetworkError, BadRequest, raise_for_ret_code +from simnet.errors import TimedOut, NetworkError, BadRequest, raise_for_ret_code, NotSupported from simnet.utils.cookies import parse_cookie from simnet.utils.ds import generate_dynamic_secret, DSType, hex_digest from simnet.utils.enum_ import Region, Game @@ -308,6 +308,8 @@ class BaseClient(AsyncContextManager["BaseClient"]): if response.is_error or ret_code != 0: raise_for_ret_code(data) return data["data"] + if response.status_code == 404: + raise NotSupported("API not supported or has been removed.") raise BadRequest(status_code=response.status_code, message=response.text) async def request_lab( diff --git a/simnet/client/components/diary/base.py b/simnet/client/components/diary/base.py index 313e992..192203c 100644 --- a/simnet/client/components/diary/base.py +++ b/simnet/client/components/diary/base.py @@ -44,7 +44,7 @@ class BaseDiaryClient(BaseClient): params = params or {} url = ( - DETAIL_LEDGER_URL.get_url(self.region) + DETAIL_LEDGER_URL.get_url(self.region, game) if detail else INFO_LEDGER_URL.get_url( self.region, diff --git a/simnet/client/routes.py b/simnet/client/routes.py index b2ca88c..18ac50f 100644 --- a/simnet/client/routes.py +++ b/simnet/client/routes.py @@ -61,6 +61,15 @@ class URL(_URL): """ return URL(urljoin(str(self) + "/", str(URL(url)))) + def __bool__(self): + """Return True if the URL is not empty. + + Returns: + bool: True if the URL is not empty. + + """ + return str(self) != "" + class BaseRoute: """A base class for defining routes with useful metadata.""" @@ -241,9 +250,15 @@ CALCULATOR_URL = InternationalRoute( chinese="https://api-takumi.mihoyo.com/event/e20200928calculate/v1/", ) -DETAIL_LEDGER_URL = InternationalRoute( - overseas="https://sg-hk4e-api.hoyolab.com/event/ysledgeros/month_detail", - chinese="https://hk4e-api.mihoyo.com/event/ys_ledger/monthDetail", +DETAIL_LEDGER_URL = GameRoute( + overseas=dict( + genshin="https://sg-hk4e-api.hoyolab.com/event/ysledgeros/month_detail", + hkrpg="", + ), + chinese=dict( + genshin="https://hk4e-api.mihoyo.com/event/ys_ledger/monthDetail", + hkrpg="https://api-takumi.mihoyo.com/event/srledger/month_detail", + ), ) INFO_LEDGER_URL = GameRoute( diff --git a/simnet/errors.py b/simnet/errors.py index 7667c7c..662c49f 100644 --- a/simnet/errors.py +++ b/simnet/errors.py @@ -36,12 +36,13 @@ class BadRequest(ApiHelperException): ) -> None: if status_code is not None: self.status_code = status_code - ret_code = response.get("ret_code") - if ret_code is not None: - self.ret_code = ret_code - response_message = response.get("message") - if response_message is not None: - self.original = response_message + if response is not None: + ret_code = response.get("ret_code") + if ret_code is not None: + self.ret_code = ret_code + response_message = response.get("message") + if response_message is not None: + self.original = response_message if message is not None or self.original is not None: self.message = message or self.original