mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-07 08:31:06 +00:00
refactor(tokens-repo): simplify getting tokens
get_token_by_token_string and get_token_by_name are no longer tied to json.
This commit is contained in:
parent
b856a2aad3
commit
ff264ec808
|
@ -25,29 +25,19 @@ DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
|
||||||
class JsonTokensRepository(AbstractTokensRepository):
|
class JsonTokensRepository(AbstractTokensRepository):
|
||||||
def get_token_by_token_string(self, token_string: str) -> Optional[Token]:
|
def get_token_by_token_string(self, token_string: str) -> Optional[Token]:
|
||||||
"""Get the token by token"""
|
"""Get the token by token"""
|
||||||
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
tokens = self.get_tokens()
|
||||||
for userdata_token in tokens_file["tokens"]:
|
for token in tokens:
|
||||||
if userdata_token["token"] == token_string:
|
if token.token == token_string:
|
||||||
|
return token
|
||||||
return Token(
|
|
||||||
token=token_string,
|
|
||||||
device_name=userdata_token["name"],
|
|
||||||
created_at=userdata_token["date"],
|
|
||||||
)
|
|
||||||
|
|
||||||
raise TokenNotFound("Token not found!")
|
raise TokenNotFound("Token not found!")
|
||||||
|
|
||||||
def get_token_by_name(self, token_name: str) -> Optional[Token]:
|
def get_token_by_name(self, token_name: str) -> Optional[Token]:
|
||||||
"""Get the token by name"""
|
"""Get the token by name"""
|
||||||
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
tokens = self.get_tokens()
|
||||||
for userdata_token in tokens_file["tokens"]:
|
for token in tokens:
|
||||||
if userdata_token["name"] == token_name:
|
if token.device_name == token_name:
|
||||||
|
return token
|
||||||
return Token(
|
|
||||||
token=userdata_token["token"],
|
|
||||||
device_name=token_name,
|
|
||||||
created_at=userdata_token["date"],
|
|
||||||
)
|
|
||||||
|
|
||||||
raise TokenNotFound("Token not found!")
|
raise TokenNotFound("Token not found!")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue