Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
migrate from deprecated parse_obj_as to v2 TypeAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoubelet committed Jul 29, 2024
1 parent 9883fcb commit b32ba80
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions aoe2netwrapper/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import requests

from loguru import logger
from pydantic import parse_obj_as
from pydantic import TypeAdapter

from aoe2netwrapper.exceptions import Aoe2NetError
from aoe2netwrapper.models import (
Expand All @@ -30,6 +30,9 @@
_MAX_MATCHES_COUNT: int = 1_000
_OK_STATUS_CODE: int = 200

_LIST_MATCHLOBBY_ADAPTER = TypeAdapter(list[MatchLobby])
_LIST_RATINGTIMEPOINT_ADAPTER = TypeAdapter(list[RatingTimePoint])


class AoE2NetAPI:
"""
Expand Down Expand Up @@ -167,7 +170,7 @@ def lobbies(self, game: str = "aoe2de") -> list[MatchLobby]:
timeout=self.timeout,
)
logger.trace(f"Validating response from '{self._LOBBIES_ENDPOINT}'")
return parse_obj_as(list[MatchLobby], processed_response)
return _LIST_MATCHLOBBY_ADAPTER.validate_python(processed_response)

def last_match(
self, game: str = "aoe2de", steam_id: int | None = None, profile_id: int | None = None
Expand Down Expand Up @@ -262,7 +265,7 @@ def match_history(
timeout=self.timeout,
)
logger.trace(f"Validating response from '{self._MATCH_HISTORY_ENDPOINT}'")
return parse_obj_as(list[MatchLobby], processed_response)
return _LIST_MATCHLOBBY_ADAPTER.validate_python(processed_response)

def rating_history(
self,
Expand Down Expand Up @@ -325,7 +328,7 @@ def rating_history(
timeout=self.timeout,
)
logger.trace(f"Validating response from '{self._RATING_HISTORY_ENDPOINT}'")
return parse_obj_as(list[RatingTimePoint], processed_response)
return _LIST_RATINGTIMEPOINT_ADAPTER.validate_python(processed_response)

def matches(self, game: str = "aoe2de", count: int = 10, since: int | None = None) -> list[MatchLobby]:
"""
Expand Down Expand Up @@ -368,7 +371,7 @@ def matches(self, game: str = "aoe2de", count: int = 10, since: int | None = Non
timeout=self.timeout,
)
logger.trace(f"Validating response from '{self._MATCHES_ENDPOINT}'")
return parse_obj_as(list[MatchLobby], processed_response)
return _LIST_MATCHLOBBY_ADAPTER.validate_python(processed_response)

def match(self, game: str = "aoe2de", uuid: str | None = None, match_id: int | None = None) -> MatchLobby:
"""
Expand Down

0 comments on commit b32ba80

Please sign in to comment.