diff --git a/pyproject.toml b/pyproject.toml index a23a6104..843f18b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "shikithon" -version = "0.4.1" +version = "0.4.2" description = "Yet another Python wrapper for Shikimori API" authors = [ "SecondThundeR " diff --git a/shikithon/__init__.py b/shikithon/__init__.py index 691007fa..20c1dbfb 100644 --- a/shikithon/__init__.py +++ b/shikithon/__init__.py @@ -1,5 +1,5 @@ """Contains package version and some magic for importing API object.""" from shikithon.api import API -__version__ = '0.4.1' +__version__ = '0.4.2' __all__ = ['API'] diff --git a/shikithon/api.py b/shikithon/api.py index 0d45abd5..4736923c 100644 --- a/shikithon/api.py +++ b/shikithon/api.py @@ -556,6 +556,34 @@ def _request( logger.debug('Can\'t extract JSON. Returning status_code/text') return response.status_code if not response.text else response.text + def _semi_protected_method(self, api_name: str) -> Optional[Dict[str, str]]: + """ + This method utilizes protected method decoration logic + for such methods, which uses access tokens in some situations. + + :param api_name: Name of API endpoint for calling as protected + :type api_name: str + + :return: Authorization header with correct tokens or None + :rtype: Optional[Dict[str, str]] + """ + logger.debug(f'Checking the possibility of using "{api_name}" ' + f'as protected method') + + if self.restricted_mode: + logger.debug(f'It is not possible to use "{api_name}" ' + 'as the protected method ' + 'due to the restricted mode') + return None + + if self.token_expired(): + logger.debug('Token has expired. Refreshing...') + self.refresh_tokens() + + logger.debug('All checks for use of the protected ' + 'method have been passed') + return self._authorization_header + def refresh_tokens(self): """ Manages tokens refreshing and caching. @@ -680,11 +708,10 @@ def animes(self, limit=[limit, 50], score=[score, 9]) - headers: Optional[Dict[str, str]] = None + headers: Dict[str, str] = self._user_agent - if not self.restricted_mode: - logger.debug('Using "/api/animes" as protected method') - headers = self._authorization_header + if my_list: + headers = self._semi_protected_method('/api/animes') response: List[Dict[str, Any]] = self._request( self._endpoints.animes, @@ -1767,11 +1794,10 @@ def mangas(self, limit=[limit, 50], score=[score, 9]) - headers: Optional[Dict[str, str]] = None + headers: Dict[str, str] = self._user_agent - if not self.restricted_mode: - logger.debug('Using "/api/mangas" as protected method') - headers = self._authorization_header + if my_list: + headers = self._semi_protected_method('/api/mangas') response: List[Dict[str, Any]] = self._request( self._endpoints.mangas,