Skip to content

Commit

Permalink
refactor: Remove all occurrences of genshin
Browse files Browse the repository at this point in the history
Because the wrapper will only support Genshin Impact from now on
  • Loading branch information
seriaati committed Feb 1, 2024
1 parent ff7527c commit 1905cb7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Introduction
enka.py is an async API wrapper for [enka.network](https://enka.network/) written in Python.
> [!NOTE]
> The wrapper currently only supports fetching Genshin Impact showcase, for Honkai: Star Rail, use [mihomo](https://github.com/KT-Yeh/mihomo) instead.
> The wrapper only supports fetching Genshin Impact showcase, for Honkai: Star Rail, use [mihomo](https://github.com/KT-Yeh/mihomo) instead.
## Features
- Fully typed.
Expand All @@ -46,7 +46,7 @@ import asyncio

async def main() -> None:
async with enka.EnkaAPI() as api:
response = await api.fetch_genshin_showcase(901211014)
response = await api.fetch_showcase(901211014)
print(response.player.nickname)
print(response.characters[0].name)

Expand All @@ -64,7 +64,7 @@ import asyncio
async def main() -> None:
api = enka.EnkaAPI()
await api.start()
response = await api.fetch_genshin_showcase(901211014)
response = await api.fetch_showcase(901211014)
await api.close()

asyncio.run(main())
Expand All @@ -76,7 +76,7 @@ import asyncio

async def main() -> None:
async with enka.EnkaAPI() as api:
await api.fetch_genshin_showcase(901211014)
await api.fetch_showcase(901211014)

asyncio.run(main())
```
Expand Down
30 changes: 14 additions & 16 deletions enka/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from .assets.updater import AssetUpdater
from .enums import Element, Language
from .exceptions import raise_for_retcode
from .models.response import GenshinShowcaseResponse
from .models.response import ShowcaseResponse

if TYPE_CHECKING:
from .models.character import Character
from .models.player import GenshinPlayer, ShowcaseCharacter
from .models.player import Player, ShowcaseCharacter

__all__ = ("EnkaAPI",)

Expand Down Expand Up @@ -83,7 +83,7 @@ async def _request(self, url: str) -> dict[str, Any]:
self._cache[url] = data
return data

def _post_process_genshin_showcase_player(self, player: "GenshinPlayer") -> "GenshinPlayer":
def _post_process_showcase_player(self, player: "Player") -> "Player":
# namecard
namecard_icon = self._namecard_data[str(player.namecard_id)]["icon"]
player.namecard_icon = f"https://enka.network/ui/{namecard_icon}.png"
Expand All @@ -96,7 +96,7 @@ def _post_process_genshin_showcase_player(self, player: "GenshinPlayer") -> "Gen

return player

def _post_process_genshin_showcase_character(
def _post_process_showcase_character(
self, showcase_character: "ShowcaseCharacter"
) -> "ShowcaseCharacter":
if showcase_character.costume_id is None:
Expand All @@ -109,7 +109,7 @@ def _post_process_genshin_showcase_character(

return showcase_character

def _post_process_genshin_character(self, character: "Character") -> "Character":
def _post_process_character(self, character: "Character") -> "Character":
character_data = self._character_data[str(character.id)]
# name
character_name_text_map_hash = character_data["NameTextMapHash"]
Expand Down Expand Up @@ -164,21 +164,19 @@ def _post_process_genshin_character(self, character: "Character") -> "Character"

return character

def _post_process_genshin_showcase(
self, showcase: GenshinShowcaseResponse
) -> GenshinShowcaseResponse:
showcase.player = self._post_process_genshin_showcase_player(showcase.player)
def _post_process_showcase(self, showcase: ShowcaseResponse) -> ShowcaseResponse:
showcase.player = self._post_process_showcase_player(showcase.player)

# costume
showcase_characters: list[ShowcaseCharacter] = []
for character in showcase.player.showcase_characters:
showcase_characters.append(self._post_process_genshin_showcase_character(character))
showcase_characters.append(self._post_process_showcase_character(character))
showcase.player.showcase_characters = showcase_characters

# characters
characters: list[Character] = []
for character in showcase.characters:
characters.append(self._post_process_genshin_character(character))
characters.append(self._post_process_character(character))

return showcase

Expand Down Expand Up @@ -218,11 +216,11 @@ async def update_assets(self) -> None:

LOGGER_.info("Assets updated")

async def fetch_genshin_showcase( # noqa: C901, PLR0912
async def fetch_showcase( # noqa: C901, PLR0912
self, uid: str | int, *, info_only: bool = False
) -> GenshinShowcaseResponse:
) -> ShowcaseResponse:
"""
Fetches the Genshin Impact character showcase of the given UID.
Fetches the Impact character showcase of the given UID.
Parameters
----------
Expand All @@ -237,5 +235,5 @@ async def fetch_genshin_showcase( # noqa: C901, PLR0912
url += "?info"

data = await self._request(url)
showcase = GenshinShowcaseResponse(**data)
return self._post_process_genshin_showcase(showcase)
showcase = ShowcaseResponse(**data)
return self._post_process_showcase(showcase)
4 changes: 2 additions & 2 deletions enka/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel, Field, field_validator

__all__ = ("GenshinPlayer", "ShowcaseCharacter")
__all__ = ("Player", "ShowcaseCharacter")


class ShowcaseCharacter(BaseModel):
Expand Down Expand Up @@ -46,7 +46,7 @@ def costume_art(self) -> Optional[str]:
return self.costume_side_icon.replace("AvatarIcon_Side", "Costume")


class GenshinPlayer(BaseModel):
class Player(BaseModel):
"""
Represents a Genshin Impact player.
Expand Down
10 changes: 5 additions & 5 deletions enka/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
from pydantic import BaseModel, Field, model_validator

from .character import Character
from .player import GenshinPlayer
from .player import Player

__all__ = ("GenshinShowcaseResponse",)
__all__ = ("ShowcaseResponse",)


class GenshinShowcaseResponse(BaseModel):
class ShowcaseResponse(BaseModel):
"""
Represents a Genshin Impact showcase response.
Attributes
----------
characters: List[:class:`Character`]
The characters in the showcase.
player: :class:`GenshinPlayer`
player: :class:`Player`
The player of the showcase.
ttl: :class:`int`
The time to live of the response.
Expand All @@ -25,7 +25,7 @@ class GenshinShowcaseResponse(BaseModel):
"""

characters: List[Character] = Field(alias="avatarInfoList")
player: GenshinPlayer = Field(alias="playerInfo")
player: Player = Field(alias="playerInfo")
ttl: int
uid: str

Expand Down
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

async def main() -> None:
async with enka.EnkaAPI() as api:
response = await api.fetch_genshin_showcase(901211014)
response = await api.fetch_showcase(901211014)

print("Name:", response.player.nickname)
print("Level:", response.player.level)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_enka.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
@pytest.mark.asyncio
async def test_fetch_showcase() -> None:
async with EnkaAPI() as api:
showcase = await api.fetch_genshin_showcase("901211014")
showcase = await api.fetch_showcase("901211014")
assert showcase.uid == "901211014"


@pytest.mark.asyncio
async def test_empty_showcase() -> None:
async with EnkaAPI() as api:
showcase = await api.fetch_genshin_showcase("123456789")
showcase = await api.fetch_showcase("123456789")
assert showcase.uid == "123456789"
assert len(showcase.characters) == 0


@pytest.mark.asyncio
async def test_traveler_showcase() -> None:
async with EnkaAPI() as api:
showcase = await api.fetch_genshin_showcase("600001919")
showcase = await api.fetch_showcase("600001919")
assert showcase.uid == "600001919"


Expand Down

0 comments on commit 1905cb7

Please sign in to comment.