From 102a732f9b0df244c3d5e8112f6b51c9e679bec0 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 1 Jan 2024 19:23:14 +0100 Subject: [PATCH] simplify OAuth as_dict and is_oauth --- ytmusicapi/auth/oauth/token.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/ytmusicapi/auth/oauth/token.py b/ytmusicapi/auth/oauth/token.py index 35804aa..0df8cb8 100644 --- a/ytmusicapi/auth/oauth/token.py +++ b/ytmusicapi/auth/oauth/token.py @@ -23,20 +23,17 @@ class Token: expires_at: int expires_in: int = 0 + @staticmethod + def members(): + return Token.__annotations__.keys() + def __repr__(self) -> str: """Readable version.""" return f"{self.__class__.__name__}: {self.as_dict()}" def as_dict(self) -> RefreshableTokenDict: """Returns dictionary containing underlying token values.""" - return { - "access_token": self.access_token, - "refresh_token": self.refresh_token, - "scope": self.scope, - "expires_at": self.expires_at, - "expires_in": self.expires_in, - "token_type": self.token_type, - } + return {key: self.__dict__[key] for key in Token.members()} # type: ignore def as_json(self) -> str: return json.dumps(self.as_dict()) @@ -55,14 +52,7 @@ class OAuthToken(Token): @staticmethod def is_oauth(headers: CaseInsensitiveDict) -> bool: - oauth_structure = { - "access_token", - "expires_at", - "expires_in", - "token_type", - "refresh_token", - } - return all(key in headers for key in oauth_structure) + return all(key in headers for key in Token.members()) def update(self, fresh_access: BaseTokenDict): """