Skip to content

Commit

Permalink
🐞 fix(pydantic): fix #2
Browse files Browse the repository at this point in the history
release 0.1b8
  • Loading branch information
SerinaNya committed Aug 31, 2023
1 parent 744823b commit f38bf49
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "yggdrasil-mc"
version = "0.1b7"
version = "0.1b8"
authors = [
{ name="Xiao_Jin", email="[email protected]" },
]
Expand Down
13 changes: 7 additions & 6 deletions src/yggdrasil_mc/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
from base64 import b64decode
from datetime import date
from datetime import datetime
from typing import Literal
from urllib.parse import urlparse

from pydantic import BaseModel, Field, root_validator
from pydantic import model_validator, BaseModel, Field, root_validator


class YggdrasilPlayerUuidApiModel(BaseModel):
Expand All @@ -30,13 +30,13 @@ class _Skin(BaseModel):
url: str | None = None
hash: str | None = None
metadata: _SkinMetaData | None = _SkinMetaData(model="default")
_get_hash = root_validator(allow_reuse=True)(get_hash)
_get_hash = model_validator(mode="before")(get_hash)


class _Cape(BaseModel):
url: str | None = None
hash: str | None = None
_get_hash = root_validator(allow_reuse=True)(get_hash)
_get_hash = model_validator(mode="before")(get_hash)


class YggdrasilTexturesModel(BaseModel):
Expand All @@ -45,7 +45,7 @@ class YggdrasilTexturesModel(BaseModel):


class YggdrasilPropertiesTexturesModel(BaseModel):
timestamp: date
timestamp: datetime
profileId: str
profileName: str
textures: YggdrasilTexturesModel
Expand All @@ -59,7 +59,8 @@ class Properties(BaseModel):
name: str
properties: Properties # a bit difference between API

@root_validator(pre=True)
@model_validator(mode="before")
@classmethod
def pre_processer(cls, values):
# Doc: https://wiki.vg/Mojang_API#UUID_-.3E_Profile_.2B_Skin.2FCape
# base64 decode and a little change
Expand Down
4 changes: 2 additions & 2 deletions src/yggdrasil_mc/ygg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def get(cls, api_root: str, player_name: str):
resp = httpx.get(f"{api_root}/users/profiles/minecraft/{player_name}")
if resp.status_code in [204, 404]: # No Content & Not Found
return cls(existed=False)
return cls.parse_raw(resp.text)
return cls.model_validate_json(resp.text)

@classmethod
def getYggdrasilServer(cls, api_root: str, player_name: str):
Expand All @@ -24,7 +24,7 @@ class YggdrasilGameProfileApi(model.YggdrasilGameProfileApiModel):
@classmethod
def get(cls, api_root: str, player_uuid: str):
resp = httpx.get(f"{api_root}/session/minecraft/profile/{player_uuid}")
return cls.parse_raw(resp.text)
return cls.model_validate_json(resp.text)

@classmethod
def getYggdrasilServer(cls, api_root: str, player_uuid: str):
Expand Down
4 changes: 2 additions & 2 deletions src/yggdrasil_mc/ygg_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def get(cls, api_root: str, player_name: str):
)
if resp.status_code in [204, 404]: # No Content & Not Found
return cls(existed=False)
return cls.parse_raw(resp.text)
return cls.model_validate_json(resp.text)

@classmethod
async def getYggdrasilServer(cls, api_root: str, player_name: str):
Expand All @@ -30,7 +30,7 @@ async def get(cls, api_root: str, player_uuid: str):
resp = await client.get(
f"{api_root}/session/minecraft/profile/{player_uuid}"
)
return cls.parse_raw(resp.text)
return cls.model_validate_json(resp.text)

@classmethod
async def getYggdrasilServer(cls, api_root: str, player_uuid: str):
Expand Down

0 comments on commit f38bf49

Please sign in to comment.