Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] aiosu v2.0.0 #151

Merged
merged 10 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ API v2 Example


async def main():
token = aiosu.models.OAuthToken.parse_obj(json_token_from_api)
token = aiosu.models.OAuthToken.model_validate(json_token_from_api)

# or

Expand Down
12 changes: 6 additions & 6 deletions aiosu/models/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Album(BaseModel):
class ArtistTrack(BaseModel):
id: int
artist_id: int
bpm: int
bpm: float
cover_url: str
exclusive: bool
genre: str
Expand All @@ -50,14 +50,14 @@ class ArtistTrack(BaseModel):
artist: Artist
osz_url: str = Field(alias="osz")
preview_url: str = Field(alias="preview")
album: Optional[Album]
album_id: Optional[int]
updated_at: Optional[datetime]
version: Optional[str]
album: Optional[Album] = None
album_id: Optional[int] = None
updated_at: Optional[datetime] = None
version: Optional[str] = None


class ArtistResponse(CursorModel):
"""Artist response model."""

artist_tracks: list[ArtistTrack]
search: Optional[ArtistSearch]
search: Optional[ArtistSearch] = None
43 changes: 20 additions & 23 deletions aiosu/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,38 @@
"""
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TypeVar

import orjson
import pydantic

from .mods import Mods


if TYPE_CHECKING:
from typing import Any
from pydantic import ConfigDict

__all__ = (
"BaseModel",
"FrozenModel",
)


def orjson_dumps(v: object, *, default: Any) -> str:
# orjson.dumps returns bytes, to match standard json.dumps we need to decode
return orjson.dumps(v, default=default).decode()
T = TypeVar("T")


class BaseModel(pydantic.BaseModel):
class Config:
arbitrary_types_allowed = True
allow_population_by_field_name = True
json_loads = orjson.loads
json_dumps = orjson_dumps
model_config = ConfigDict(arbitrary_types_allowed=True, populate_by_name=True)

def model_validate_file(self, path: str) -> BaseModel:
"""Validates a model from a file.

json_encoders = {
Mods: str,
}
:param path: The path to the file
:type path: str
:raises TypeError: If the file is not a JSON file
:return: The validated model
:rtype: aiosu.models.base.BaseModel
"""
with open(path) as f:
return self.model_validate_json(f.read())


class FrozenModel(BaseModel):
def __init__(self, **data: Any) -> None:
super().__init__(**data)
self.__config__.frozen = True
model_config = ConfigDict(
arbitrary_types_allowed=True,
populate_by_name=True,
frozen=True,
)
Loading