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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondThundeR committed May 21, 2022
2 parents fc38fea + 7aba610 commit 14832f0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion shikithon/__init__.py
Original file line number Diff line number Diff line change
@@ -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']
42 changes: 34 additions & 8 deletions shikithon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 14832f0

Please sign in to comment.