SIMNet/simnet/client/components/chronicle/base.py

131 lines
4.8 KiB
Python
Raw Permalink Normal View History

2023-05-06 12:53:19 +00:00
from typing import Optional, Any, List
2023-05-28 09:32:21 +00:00
2023-05-01 12:50:48 +00:00
from simnet.client.base import BaseClient
from simnet.client.routes import RECORD_URL
2023-05-06 12:53:19 +00:00
from simnet.errors import DataNotPublic
from simnet.models.lab.record import RecordCard
2023-12-27 07:09:24 +00:00
from simnet.utils.enums import Region, Game
2023-05-01 12:50:48 +00:00
from simnet.utils.types import QueryParamTypes
__all__ = ("BaseChronicleClient",)
2023-05-01 12:50:48 +00:00
class BaseChronicleClient(BaseClient):
"""The base class for the Chronicle API client.
This class provides the basic functionality for making requests to the
Chronicle API endpoints. It is meant to be subclassed by other clients
that provide a more specific interface to the Chronicle API.
Attributes:
region (Region): The region associated with the API client.
"""
async def request_game_record(
self,
endpoint: str,
2023-07-23 09:41:26 +00:00
endpoint_type: str = "api",
2023-05-01 12:50:48 +00:00
data: Optional[Any] = None,
params: Optional[QueryParamTypes] = None,
lang: Optional[str] = None,
region: Optional[Region] = None,
game: Optional[Game] = None,
):
"""Make a request towards the game record endpoint.
Args:
endpoint (str): The endpoint to send the request to.
2023-07-23 09:41:26 +00:00
endpoint_type (str, optional): The type of endpoint to send the request to.
2023-05-01 12:50:48 +00:00
data (Optional[Any], optional): The request payload.
params (Optional[QueryParamTypes], optional): The query parameters for the request.
lang (Optional[str], optional): The language for the response.
region (Optional[Region], optional): The region associated with the request.
game (Optional[Game], optional): The game associated with the request.
Returns:
The response from the server.
Raises:
NetworkError: If an HTTP error occurs while making the request.
TimedOut: If the request times out.
BadRequest: If the response contains an error.
"""
2024-07-05 13:58:00 +00:00
base_url = RECORD_URL.get_url(region or self.region, game or Game.GENSHIN)
2023-05-01 12:50:48 +00:00
if game:
2024-07-05 13:58:00 +00:00
if game == Game.ZZZ:
base_url = base_url / endpoint_type / "zzz"
else:
base_url = base_url / game.value / endpoint_type
2023-05-01 12:50:48 +00:00
url = base_url / endpoint
new_ds = self.region == Region.CHINESE
2023-05-12 03:24:06 +00:00
return await self.request_lab(url, data=data, params=params, lang=lang, new_ds=new_ds)
2023-05-05 04:06:43 +00:00
async def update_settings(
self,
switch_id: int,
on: bool,
*,
game: Optional[Game] = None,
) -> None:
"""Update user settings for the Battle Chronicle.
This method allows the user to update their settings for the Battle Chronicle.
The settings can control whether to show the user's Battle Chronicle on their
profile, show the user's character details in the Battle Chronicle, and enable
real-time notes (only for Genshin Impact).
Args:
switch_id (int): The ID of the setting to update. Valid values are 1, 2, and 3.
The IDs correspond to the following settings:
1. Show your Battle Chronicle on your profile.
2. Show your Character Details in the Battle Chronicle.
3. Enable your Real-Time Notes. (only for Genshin Impact)
on (bool): The new value for the setting. Set to True to turn on the setting,
and False to turn it off.
game (Optional[Game], optional): The game associated with the setting. Only required
if the setting ID is 3 (for enabling real-time notes). Valid values are Game.HONKAI,
Game.GENSHIN, and Game.STARRAIL.
"""
if game is None and switch_id == 3:
game = Game.GENSHIN
2023-06-09 14:55:51 +00:00
game_id = {Game.HONKAI: 1, Game.GENSHIN: 2, Game.STARRAIL: 6}[game]
2023-05-05 04:06:43 +00:00
await self.request_game_record(
"card/wapi/changeDataSwitch",
data=dict(switch_id=switch_id, is_public=on, game_id=game_id),
)
2023-05-06 12:53:19 +00:00
async def get_record_cards(
self,
account_id: Optional[int] = None,
*,
lang: Optional[str] = None,
) -> List[RecordCard]:
2023-05-28 09:32:21 +00:00
"""Get a player record cards.
2023-05-06 12:53:19 +00:00
Args:
2023-05-28 09:32:21 +00:00
account_id: (int, optional), the user's account ID, defaults to None
lang: (str, optional), the language version of the request, defaults to None
2023-05-06 12:53:19 +00:00
Returns:
A list of RecordCard objects.
Raises:
DataNotPublic: If data is empty.
"""
account_id = account_id or self.account_id
data = await self.request_game_record(
"card/wapi/getGameRecordCard",
lang=lang,
params=dict(uid=account_id),
)
if not data["list"]:
raise DataNotPublic({"retcode": 10102})
return [RecordCard.creat(**card) for card in data["list"]]