Skip to content

Commit

Permalink
[feat] aiosu v2.0.0 (#151)
Browse files Browse the repository at this point in the history
* feat: update models for pydantic v2

* chore: update test serialization methods

* docs: document breaking changes

* fix: change typing on `model_validate_file`

* chore: update examples

* chore: bump pydantic version

* docs: update readme example

* feat: add `message_length_limit` to `ChatChannel`

* feat: add data to models

* style: remove unused imports
  • Loading branch information
NiceAesth committed Jul 18, 2023
1 parent 14b3eb6 commit 362bb9c
Show file tree
Hide file tree
Showing 55 changed files with 655 additions and 543 deletions.
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

0 comments on commit 362bb9c

Please sign in to comment.