🐛 Fix RegionNotSupported Error

This commit is contained in:
omg-xtao 2023-06-11 22:19:20 +08:00 committed by GitHub
parent db76c21d4a
commit fc2faa805f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 11 deletions

View File

@ -7,7 +7,7 @@ from httpx import AsyncClient, TimeoutException, Response, HTTPError, Timeout
from simnet.client.cookies import Cookies from simnet.client.cookies import Cookies
from simnet.client.headers import Headers 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.cookies import parse_cookie
from simnet.utils.ds import generate_dynamic_secret, DSType, hex_digest from simnet.utils.ds import generate_dynamic_secret, DSType, hex_digest
from simnet.utils.enum_ import Region, Game from simnet.utils.enum_ import Region, Game
@ -308,6 +308,8 @@ class BaseClient(AsyncContextManager["BaseClient"]):
if response.is_error or ret_code != 0: if response.is_error or ret_code != 0:
raise_for_ret_code(data) raise_for_ret_code(data)
return data["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) raise BadRequest(status_code=response.status_code, message=response.text)
async def request_lab( async def request_lab(

View File

@ -44,7 +44,7 @@ class BaseDiaryClient(BaseClient):
params = params or {} params = params or {}
url = ( url = (
DETAIL_LEDGER_URL.get_url(self.region) DETAIL_LEDGER_URL.get_url(self.region, game)
if detail if detail
else INFO_LEDGER_URL.get_url( else INFO_LEDGER_URL.get_url(
self.region, self.region,

View File

@ -61,6 +61,15 @@ class URL(_URL):
""" """
return URL(urljoin(str(self) + "/", str(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: class BaseRoute:
"""A base class for defining routes with useful metadata.""" """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/", chinese="https://api-takumi.mihoyo.com/event/e20200928calculate/v1/",
) )
DETAIL_LEDGER_URL = InternationalRoute( DETAIL_LEDGER_URL = GameRoute(
overseas="https://sg-hk4e-api.hoyolab.com/event/ysledgeros/month_detail", overseas=dict(
chinese="https://hk4e-api.mihoyo.com/event/ys_ledger/monthDetail", 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( INFO_LEDGER_URL = GameRoute(

View File

@ -36,12 +36,13 @@ class BadRequest(ApiHelperException):
) -> None: ) -> None:
if status_code is not None: if status_code is not None:
self.status_code = status_code self.status_code = status_code
ret_code = response.get("ret_code") if response is not None:
if ret_code is not None: ret_code = response.get("ret_code")
self.ret_code = ret_code if ret_code is not None:
response_message = response.get("message") self.ret_code = ret_code
if response_message is not None: response_message = response.get("message")
self.original = response_message if response_message is not None:
self.original = response_message
if message is not None or self.original is not None: if message is not None or self.original is not None:
self.message = message or self.original self.message = message or self.original