Skip to content

Commit

Permalink
update for aiorequestful v0.3.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
geo-martino committed Jul 10, 2024
1 parent e5a7cb6 commit 307f6e5
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
15 changes: 9 additions & 6 deletions musify/libraries/remote/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

from aiorequestful.auth import Authoriser
from aiorequestful.cache.backend.base import ResponseCache
from aiorequestful.exception import CacheError
from aiorequestful.request import RequestHandler
from aiorequestful.types import ImmutableJSON
from aiorequestful.cache.exception import CacheError
from aiorequestful.request.handler import RequestHandler
from aiorequestful.response.payload import JSONPayloadHandler
from aiorequestful.types import ImmutableJSON, JSON
from yarl import URL

from musify.libraries.remote.core import RemoteResponse
Expand All @@ -23,7 +24,7 @@
from musify.utils import align_string, to_collection


class RemoteAPI(metaclass=ABCMeta):
class RemoteAPI[A: Authoriser](metaclass=ABCMeta):
"""
Collection of endpoints for a remote API.
See :py:class:`RequestHandler` and :py:class:`Authoriser`
Expand Down Expand Up @@ -76,7 +77,7 @@ def source(self) -> str:
"""The name of the API service"""
return self.wrangler.source

def __init__(self, authoriser: Authoriser, wrangler: RemoteDataWrangler, cache: ResponseCache | None = None):
def __init__(self, authoriser: A, wrangler: RemoteDataWrangler, cache: ResponseCache | None = None):
# noinspection PyTypeChecker
#: The :py:class:`MusifyLogger` for this object
self.logger: MusifyLogger = logging.getLogger(__name__)
Expand All @@ -85,7 +86,9 @@ def __init__(self, authoriser: Authoriser, wrangler: RemoteDataWrangler, cache:
self.wrangler = wrangler

#: The :py:class:`RequestHandler` for handling authorised requests to the API
self.handler = RequestHandler.create(authoriser=authoriser, cache=cache)
self.handler: RequestHandler[A, JSON] = RequestHandler.create(
authoriser=authoriser, cache=cache, payload_handler=JSONPayloadHandler(),
)

#: Stores the loaded user data for the currently authorised user
self.user_data: dict[str, Any] = {}
Expand Down
4 changes: 2 additions & 2 deletions musify/libraries/remote/core/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
All type hints to use throughout the module.
"""
from collections.abc import MutableMapping
from typing import Any, Mapping
from collections.abc import Mapping, MutableMapping
from typing import Any

from yarl import URL

Expand Down
6 changes: 3 additions & 3 deletions musify/libraries/remote/spotify/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Also includes the default arguments to be used when requesting authorisation from the Spotify API.
"""
from http import HTTPMethod
from pathlib import Path

from aiohttp import ClientResponse
from aiorequestful.auth import AuthRequest
from aiorequestful.auth.oauth2 import AuthorisationCodeFlow
from aiorequestful.cache.backend.base import ResponseCache, ResponseRepository
from aiorequestful.cache.session import CachedSession
from aiorequestful.types import Method
from yarl import URL

from musify.libraries.remote.core.exception import APIError
Expand All @@ -30,7 +30,7 @@ class SpotifyAPI(SpotifyAPIMisc, SpotifyAPIItems, SpotifyAPIPlaylists):
:param client_secret: The client secret to use when authorising requests.
:param scope: The scopes to request access to.
:param cache: When given, attempt to use this cache for certain request types before calling the API.
:param auth_kwargs: Optionally, provide kwargs to use when instantiating the :py:class:`Authoriser`.
:param token_file_path: Optionally, provide a path to save/load a response token.
"""

__slots__ = ()
Expand Down Expand Up @@ -91,7 +91,7 @@ def __init__(
}

authoriser.response_tester.request = AuthRequest(
method=Method.GET, url=wrangler.url_api.joinpath("me")
method=HTTPMethod.GET, url=wrangler.url_api.joinpath("me")
)
authoriser.response_tester.response_test = self._response_test
authoriser.response_tester.max_expiry = 600
Expand Down
5 changes: 3 additions & 2 deletions musify/libraries/remote/spotify/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
from collections.abc import Collection, MutableMapping, Iterable
from typing import Any

from aiorequestful.auth.oauth2 import OAuth2Authoriser
from aiorequestful.cache.backend.base import ResponseRepository
from aiorequestful.cache.exception import CacheError
from aiorequestful.cache.session import CachedSession
from aiorequestful.exception import CacheError
from yarl import URL

from musify.libraries.remote.core.api import RemoteAPI
from musify.libraries.remote.core.types import RemoteObjectType


class SpotifyAPIBase(RemoteAPI, metaclass=ABCMeta):
class SpotifyAPIBase(RemoteAPI[OAuth2Authoriser], metaclass=ABCMeta):
"""Base functionality required for all endpoint functions for the Spotify API"""

__slots__ = ()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ musicbee = [
"lxml~=5.2",
]
sqlite = [
"aiorequestful[sqlite]~=0.2",
"aiorequestful[sqlite]~=0.3",
]

# dev dependencies
Expand Down
9 changes: 3 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,9 @@ async def spotify_api(spotify_mock: SpotifyMock) -> SpotifyAPI:
api.handler.authoriser.response_tester.response_test = None
api.handler.authoriser.response_tester.max_expiry = 0

# force almost no backoff/wait settings
api.handler.backoff_start = 0.001
api.handler.backoff_factor = 1
api.handler.backoff_count = 10
api.handler.wait_time = 0
api.handler.wait_increment = 0
# force no backoff/wait settings
api.handler.wait_timer = None
api.handler.retry_timer = None

async with api as a:
spotify_mock.reset()
Expand Down
2 changes: 1 addition & 1 deletion tests/libraries/remote/spotify/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import pytest
from aiorequestful.cache.backend.base import ResponseCache, ResponseRepository
from aiorequestful.cache.exception import CacheError
from aiorequestful.cache.session import CachedSession
from aiorequestful.exception import CacheError
from yarl import URL

from musify.libraries.remote.core.exception import APIError
Expand Down

0 comments on commit 307f6e5

Please sign in to comment.