Skip to content

Commit

Permalink
Support calculator in fastapi extras
Browse files Browse the repository at this point in the history
Added endpoints for calculator methods.
Fixed issues wtih model re-validation.
  • Loading branch information
thesadru committed Dec 18, 2021
1 parent ba4ab35 commit f6a79b9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
60 changes: 52 additions & 8 deletions extra/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,42 @@ async def index():
return RedirectResponse("/docs")


@app.get("/card/{hoyolab_uid}", response_model=genshin.models.RecordCard)
@app.get("/card/{hoyolab_uid}", response_model=genshin.models.RecordCard, tags=["chronicle"])
async def hoyolab_user(hoyolab_uid: int, lang: Lang = None):
return await client.get_record_card(hoyolab_uid, lang=lang)


@app.get("/user/{uid}", response_model=genshin.models.UserStats)
@app.get("/user/{uid}", response_model=genshin.models.UserStats, tags=["chronicle"])
async def user(uid: int, lang: Lang = None):
return await client.get_user(uid, lang=lang)


@app.get("/user/{uid}/abyss", response_model=genshin.models.SpiralAbyss)
@app.get("/user/{uid}/abyss", response_model=genshin.models.SpiralAbyss, tags=["chronicle"])
async def abyss(uid: int, previous: bool = False):
return await client.get_spiral_abyss(uid, previous=previous)


@app.get("/user/{uid}/notes", response_model=genshin.models.Notes)
@app.get("/user/{uid}/notes", response_model=genshin.models.Notes, tags=["chronicle"])
async def notes(uid: int, lang: Lang = None):
return await client.get_notes(uid, lang=lang)


@app.get("/user/{uid}/activities", response_model=genshin.models.Activities)
@app.get("/user/{uid}/activities", response_model=genshin.models.Activities, tags=["chronicle"])
async def activities(uid: int, lang: Lang = None):
return await client.get_activities(uid, lang=lang)


@app.get("/user/{uid}/partial", response_model=genshin.models.PartialUserStats)
@app.get("/user/{uid}/partial", response_model=genshin.models.PartialUserStats, tags=["chronicle"])
async def partial_user(uid: int, lang: Lang = None):
return await client.get_partial_user(uid, lang=lang)


@app.get("/user/{uid}/full", response_model=genshin.models.FullUserStats)
@app.get("/user/{uid}/full", response_model=genshin.models.FullUserStats, tags=["chronicle"])
async def full_user(uid: int, lang: Lang = None):
return await client.get_full_user(uid, lang=lang)


@app.get("/wish", response_model=List[genshin.models.Wish])
@app.get("/wish", response_model=List[genshin.models.Wish], tags=["remote"])
async def wish(
authkey: str,
banner: Literal["100", "200", "301", "302"] = None,
Expand All @@ -82,6 +82,7 @@ async def wish(
@app.get(
"/transaction",
response_model=List[Union[genshin.models.Transaction, genshin.models.ItemTransaction]],
tags=["remote"],
)
async def transaction(
authkey: str,
Expand All @@ -94,6 +95,49 @@ async def transaction(
return await paginator.flatten()


@app.get(
"/calculator/characters",
response_model=List[genshin.models.CalculatorCharacter],
tags=["calculator"],
)
async def calculator_characters(query: str = None, lang: Lang = None):
return await client.get_calculator_characters(query=query, lang=lang)


@app.get(
"/calculator/weapons", response_model=List[genshin.models.CalculatorWeapon], tags=["calculator"]
)
async def calculator_weapons(query: str = None, lang: Lang = None):
return await client.get_calculator_weapons(query=query, lang=lang)


@app.get(
"/calculator/artifacts",
response_model=List[genshin.models.CalculatorArtifact],
tags=["calculator"],
)
async def calculator_artifacts(query: str = None, pos: int = 1, lang: Lang = None):
return await client.get_calculator_artifacts(query=query, pos=pos, lang=lang)


@app.get(
"/calculator/characters/{character_id}/talents",
response_model=List[genshin.models.CalculatorTalent],
tags=["calculator"],
)
async def calculator_character_talents(character_id: int, lang: Lang = None):
return await client.get_character_talents(character_id)


@app.get(
"/calculator/artifacts/{artifact_id}/set",
response_model=List[genshin.models.CalculatorArtifact],
tags=["calculator"],
)
async def calculator_artifact_set(artifact_id: int, lang: Lang = None):
return await client.get_complete_artifact_set(artifact_id, lang=lang)


@app.exception_handler(genshin.GenshinException)
async def handle_genshin_exception(request: Request, exc: genshin.GenshinException):
if isinstance(exc, genshin.AccountNotFound):
Expand Down
23 changes: 16 additions & 7 deletions genshin/models/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ class CalculatorCharacter(BaseCharacter):
level: int = Field(0, galias="level_current")
max_level: int

@validator("element")
@validator("element", pre=True)
def __parse_element(cls, v: Any) -> str:
if isinstance(v, str):
return v

return CALCULATOR_ELEMENTS[int(v)]

@validator("weapon_type")
@validator("weapon_type", pre=True)
def __parse_weapon_type(cls, v: Any) -> str:
if isinstance(v, str):
return v

return CALCULATOR_WEAPON_TYPES[int(v)]


Expand All @@ -74,8 +80,11 @@ class CalculatorWeapon(GenshinModel, Unique):
level: int = Field(0, galias="level_current")
max_level: int

@validator("type")
@validator("type", pre=True)
def __parse_weapon_type(cls, v: Any) -> str:
if isinstance(v, str):
return v

return CALCULATOR_WEAPON_TYPES[int(v)]


Expand All @@ -85,8 +94,8 @@ class CalculatorArtifact(GenshinModel, Unique):
id: int
name: str
icon: str
rarity: int = Field(alias="reliquary_level")
pos: int = Field(alias="reliquary_cat_id")
rarity: int = Field(galias="reliquary_level")
pos: int = Field(galias="reliquary_cat_id")
level: int = Field(0, galias="level_current")
max_level: int

Expand Down Expand Up @@ -171,8 +180,8 @@ def __correct_talent_current_level(cls, v: List[CalculatorTalent]) -> List[Calcu
class CalculatorArtifactResult(GenshinModel):
"""A calulation result for a specific artifact"""

artifact_id: int = Field(alias="reliquary_id")
list: List[CalculatorConsumable] = Field(alias="id_consume_list")
artifact_id: int = Field(galias="reliquary_id")
list: List[CalculatorConsumable] = Field(galias="id_consume_list")


class CalculatorResult(GenshinModel):
Expand Down

0 comments on commit f6a79b9

Please sign in to comment.