Skip to content

Commit

Permalink
♿ 兼容 Pydantic v1
Browse files Browse the repository at this point in the history
  • Loading branch information
KomoriDev committed Sep 14, 2024
1 parent 1fbd63a commit 7ee9a7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 5 additions & 5 deletions nonebot_plugin_lxns_maimai/apis/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def get_player_info(cls, friend_code: int) -> Player:
)
if response.status_code != 200:
raise FetchUserException(response.json()["message"])
return Player.model_validate(response.json()["data"])
return Player(**response.json()["data"])

@classmethod
async def get_rating_trend(cls, friend_code: int) -> Trend:
Expand All @@ -39,7 +39,7 @@ async def get_rating_trend(cls, friend_code: int) -> Trend:
)
if response.status_code != 200:
raise FetchUserException(response.json()["message"])
return Trend.model_validate(response.json()["data"])
return Trend(**response.json()["data"])

@classmethod
async def get_bests(
Expand All @@ -63,15 +63,15 @@ async def get_bests(
if response.status_code != 200:
raise FetchUserException(response.json()["message"])
data = response.json()["data"]
standard_scores = [Score.model_validate(score) for score in data["standard"]]
dx_scores = [Score.model_validate(score) for score in data["dx"]]
standard_scores = [Score(**score) for score in data["standard"]]
dx_scores = [Score(**score) for score in data["dx"]]
return data["standard_total"], data["dx_total"], standard_scores, dx_scores

@classmethod
async def get_song_info(cls, song_id: int) -> Song:
async with httpx.AsyncClient() as client:
response = await client.get(f"{url}/song/{song_id}")
return Song.model_validate(response.json())
return Song(**response.json())

@classmethod
async def download_player_icon(cls, player: Player) -> BytesIO | bytes:
Expand Down
8 changes: 7 additions & 1 deletion nonebot_plugin_lxns_maimai/schema/score.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from nonebot.compat import PYDANTIC_V2
from pydantic import BaseModel, ConfigDict

from .enum import FCType, FSType, RateType, SongType, LevelIndex
Expand Down Expand Up @@ -33,4 +34,9 @@ class Score(BaseModel):
upload_time: str
"""仅获取 `Score` 时返回,成绩被同步时的 UTC 时间"""

model_config = ConfigDict(extra="allow")
if PYDANTIC_V2:
model_config = ConfigDict(extra="allow") # type: ignore
else:

class Config:
extra = "allow"

0 comments on commit 7ee9a7a

Please sign in to comment.