diff --git a/simnet/client/components/lab.py b/simnet/client/components/lab.py index 29d4f65..520215c 100644 --- a/simnet/client/components/lab.py +++ b/simnet/client/components/lab.py @@ -3,12 +3,12 @@ from typing import Optional, List, Dict, Any from simnet.client.base import BaseClient from simnet.client.headers import Headers -from simnet.client.routes import TAKUMI_URL, HK4E_URL, CODE_URL +from simnet.client.routes import TAKUMI_URL, HK4E_URL, CODE_URL, CODE_HOYOLAB_URL from simnet.models.lab.announcement import Announcement from simnet.models.lab.record import PartialUser, FullUser, Account from simnet.utils.enum_ import Region, Game from simnet.utils.lang import create_short_lang_code -from simnet.utils.player import recognize_genshin_server +from simnet.utils.player import recognize_genshin_server, recognize_server, recognize_game_biz from simnet.utils.types import HeaderTypes __all__ = ("LabClient",) @@ -207,6 +207,31 @@ class LabClient(BaseClient): ), ) + async def redeem_code_by_hoyolab( + self, + code: str, + player_id: Optional[int] = None, + *, + lang: Optional[str] = None, + ) -> None: + """Redeems a gift code for the current or specified user. + + Args: + code (str): The gift code to redeem. + player_id (int, optional): The player ID to redeem the code for. Defaults to None. + lang (str, optional): The language code used for the request. Defaults to None. + """ + player_id = self.player_id or player_id + url = CODE_HOYOLAB_URL.get_url(self.region, self.game) + params = dict( + uid=player_id, + region=recognize_server(player_id, self.game), + cdkey=code, + game_biz=recognize_game_biz(player_id, self.game), + lang=create_short_lang_code(lang or self.lang), + ) + await self.request_bbs(url, params=params) + async def get_game_accounts(self, *, lang: Optional[str] = None) -> List[Account]: """Get the game accounts of the currently logged-in user. diff --git a/simnet/client/routes.py b/simnet/client/routes.py index 0bc2ab9..1f46e2e 100644 --- a/simnet/client/routes.py +++ b/simnet/client/routes.py @@ -28,6 +28,7 @@ __all__ = ( "INFO_LEDGER_URL", "HK4E_URL", "CODE_URL", + "CODE_HOYOLAB_URL", "YSULOG_URL", "QRCODE_URL", ) @@ -307,5 +308,12 @@ YSULOG_URL = InternationalRoute( HK4E_URL = Route("https://sg-hk4e-api.hoyoverse.com/common/hk4e_global/") CODE_URL = Route("https://sg-hk4e-api.hoyoverse.com/common/apicdkey/api/webExchangeCdkey") +CODE_HOYOLAB_URL = GameRoute( + overseas=dict( + genshin="https://sg-hk4e-api.hoyolab.com/common/apicdkey/api/webExchangeCdkeyHyl", + hkrpg="https://sg-hkrpg-api.hoyolab.com/common/apicdkey/api/webExchangeCdkeyHyl", + ), + chinese=dict(), +) QRCODE_URL = Route("https://hk4e-sdk.mihoyo.com/hk4e_cn/combo/panda/qrcode")