Skip to content

Commit

Permalink
feat(MyPy): TokenManager annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
650elx committed Oct 18, 2023
1 parent e29c69a commit 7cb06ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sinch/core/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Union
from sinch.core.models.http_response import HTTPResponse


class SinchException(Exception):
def __init__(self, message: str, response: HTTPResponse, is_from_server: bool):
def __init__(self, message: str, response: Union[HTTPResponse, None], is_from_server: bool):
self.is_from_server = is_from_server
self.response_status_code = response.status_code if response else None
self.http_response = response
Expand Down
11 changes: 7 additions & 4 deletions sinch/core/token_manager.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from enum import Enum
from abc import ABC, abstractmethod
from typing import Optional
from typing import Optional, TYPE_CHECKING

from sinch.domains.authentication.models.authentication import OAuthToken
from sinch.domains.authentication.endpoints.oauth import OAuthEndpoint
from sinch.core.exceptions import ValidationException
from sinch.core.models.http_response import HTTPResponse

if TYPE_CHECKING:
from sinch.core.clients.sinch_client_base import ClientBase


class TokenState(Enum):
VALID = "VALID"
Expand All @@ -15,7 +18,7 @@ class TokenState(Enum):


class TokenManagerBase(ABC):
def __init__(self, sinch):
def __init__(self, sinch: 'ClientBase') -> None:
self.sinch = sinch
self.token: Optional[OAuthToken] = None
self.token_state = TokenState.INVALID
Expand All @@ -24,11 +27,11 @@ def __init__(self, sinch):
def get_auth_token(self) -> Optional[OAuthToken]:
pass

def invalidate_expired_token(self):
def invalidate_expired_token(self) -> None:
self.token = None
self.token_state = TokenState.EXPIRED

def handle_invalid_token(self, http_response: HTTPResponse):
def handle_invalid_token(self, http_response: HTTPResponse) -> None:
if http_response.headers.get("www-authenticate") and "expired" in http_response.headers["www-authenticate"]:
self.invalidate_expired_token()

Expand Down

0 comments on commit 7cb06ae

Please sign in to comment.