mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-11 18:39:30 +00:00
refactor(tokens-repo): move create_token to abstract class
This commit is contained in:
parent
572ec75c39
commit
682cd4ae87
|
@ -31,9 +31,13 @@ class AbstractTokensRepository(ABC):
|
||||||
def get_tokens(self) -> list[Token]:
|
def get_tokens(self) -> list[Token]:
|
||||||
"""Get the tokens"""
|
"""Get the tokens"""
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def create_token(self, device_name: str) -> Token:
|
def create_token(self, device_name: str) -> Token:
|
||||||
"""Create new token"""
|
"""Create new token"""
|
||||||
|
new_token = Token.generate(device_name)
|
||||||
|
|
||||||
|
self._store_token(new_token)
|
||||||
|
|
||||||
|
return new_token
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def delete_token(self, input_token: Token) -> None:
|
def delete_token(self, input_token: Token) -> None:
|
||||||
|
@ -102,3 +106,7 @@ class AbstractTokensRepository(ABC):
|
||||||
self, mnemonic_phrase: str, device_name: str
|
self, mnemonic_phrase: str, device_name: str
|
||||||
) -> Token:
|
) -> Token:
|
||||||
"""Use the mnemonic new device key"""
|
"""Use the mnemonic new device key"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _store_token(self, new_token: Token):
|
||||||
|
"""Store a token directly"""
|
||||||
|
|
|
@ -39,15 +39,8 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
|
|
||||||
return tokens_list
|
return tokens_list
|
||||||
|
|
||||||
def create_token(self, device_name: str) -> Token:
|
def _store_token(self, new_token: Token):
|
||||||
"""Create new token"""
|
"""Store a token directly"""
|
||||||
new_token = Token.generate(device_name)
|
|
||||||
|
|
||||||
self.__store_token(new_token)
|
|
||||||
|
|
||||||
return new_token
|
|
||||||
|
|
||||||
def __store_token(self, new_token: Token):
|
|
||||||
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
||||||
tokens_file["tokens"].append(
|
tokens_file["tokens"].append(
|
||||||
{
|
{
|
||||||
|
@ -73,7 +66,7 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
|
|
||||||
if input_token in self.get_tokens():
|
if input_token in self.get_tokens():
|
||||||
self.delete_token(input_token)
|
self.delete_token(input_token)
|
||||||
self.__store_token(new_token)
|
self._store_token(new_token)
|
||||||
return new_token
|
return new_token
|
||||||
|
|
||||||
raise TokenNotFound("Token not found!")
|
raise TokenNotFound("Token not found!")
|
||||||
|
|
Loading…
Reference in a new issue