small fixes, updated type hints in api

This commit is contained in:
2022-04-23 15:46:18 +02:00
parent 5b0c70024a
commit d8b5b01bcc
2 changed files with 11 additions and 9 deletions

View File

@@ -30,7 +30,7 @@ from avt_fresh.payments import (
get_default_payment_options, get_default_payment_options,
add_payment_option_to_invoice, add_payment_option_to_invoice,
) )
from avt_fresh.token import TokenStoreOnDisk, NoToken, Token from avt_fresh.token import TokenStoreOnDisk, NoToken, TokenStore, TokenTup
BASE_URL = "https://api.freshbooks.com" BASE_URL = "https://api.freshbooks.com"
@@ -48,8 +48,13 @@ class AvtFreshException(Exception):
class ApiClient: class ApiClient:
def __init__( def __init__(
self, client_secret: str, client_id: str, redirect_uri: str, account_id: str, self,
token_store: Token = TokenStoreOnDisk, connection_string: str | None = None, client_secret: str,
client_id: str,
redirect_uri: str,
account_id: str,
token_store: TokenStore = TokenStoreOnDisk,
connection_string: str | None = None,
): ):
self.client_secret = client_secret self.client_secret = client_secret
self.client_id = client_id self.client_id = client_id
@@ -319,7 +324,7 @@ def _get_code_from_user() -> str:
) )
def _is_expired(token: Token) -> bool: def _is_expired(token: TokenTup) -> bool:
return dt.datetime.now().timestamp() > token.created_at + token.expires_in return dt.datetime.now().timestamp() > token.created_at + token.expires_in

View File

@@ -29,18 +29,16 @@ class TokenTup(typing.NamedTuple):
@dataclass @dataclass
class TokenStore(metaclass=abc.ABCMeta): class TokenStore(metaclass=abc.ABCMeta):
@abc.abstractmethod @abc.abstractmethod
def get(cls) -> TokenTup: def get(self) -> TokenTup:
... ...
@abc.abstractmethod @abc.abstractmethod
def set(cls, token_dict: dict) -> None: def set(self, token_dict: dict) -> None:
... ...
class TokenStoreOnDisk(TokenStore): class TokenStoreOnDisk(TokenStore):
@classmethod @classmethod
def get(cls) -> TokenTup: def get(cls) -> TokenTup:
if not TOKEN_PATH.exists(): if not TOKEN_PATH.exists():
@@ -58,7 +56,6 @@ class TokenStoreOnDisk(TokenStore):
class TokenStoreOnRedis(TokenStore): class TokenStoreOnRedis(TokenStore):
def __init__(self, redis_url): def __init__(self, redis_url):
self.redis_client = redis.from_url(redis_url) self.redis_client = redis.from_url(redis_url)