1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-16 07:10:09 -07:00

Add (de)serialize methods to Gateway and Auth

This makes the parts belonging to the class more local and encapsulated.
This will make adding API v2 easier.
This commit is contained in:
Frederik Gladhorn 2020-01-10 20:05:04 +01:00 committed by Frederik Gladhorn
parent f44aa91b24
commit d35c3ff817
2 changed files with 30 additions and 23 deletions

View File

@ -11,8 +11,6 @@ from typing import Any, Dict, Generator, List, Optional
from . import core
DEFAULT_COUNTRY = 'US'
DEFAULT_LANGUAGE = 'en-US'
#: Represents an unknown enum value.
_UNKNOWN = 'Unknown'
@ -79,8 +77,8 @@ class Client(object):
gateway: Optional[core.Gateway] = None,
auth: Optional[core.Auth] = None,
session: Optional[core.Session] = None,
country: str = DEFAULT_COUNTRY,
language: str = DEFAULT_LANGUAGE) -> None:
country: str = core.DEFAULT_COUNTRY,
language: str = core.DEFAULT_LANGUAGE) -> None:
# The three steps required to get access to call the API.
self._gateway: Optional[core.Gateway] = gateway
self._auth: Optional[core.Auth] = auth
@ -146,12 +144,7 @@ class Client(object):
client = cls()
if 'gateway' in state:
data = state['gateway']
client._gateway = core.Gateway(
data['auth_base'], data['api_root'], data['oauth_root'],
data.get('country', DEFAULT_COUNTRY),
data.get('language', DEFAULT_LANGUAGE),
)
client._gateway = core.Gateway.deserialize(state['gateway'])
if 'auth' in state:
data = state['auth']
@ -181,19 +174,10 @@ class Client(object):
}
if self._gateway:
out['gateway'] = {
'auth_base': self._gateway.auth_base,
'api_root': self._gateway.api_root,
'oauth_root': self._gateway.oauth_root,
'country': self._gateway.country,
'language': self._gateway.language,
}
out['gateway'] = self._gateway.serialize()
if self._auth:
out['auth'] = {
'access_token': self._auth.access_token,
'refresh_token': self._auth.refresh_token,
}
out['auth'] = self._auth.serialize()
if self._session:
out['session'] = self._session.session_id
@ -218,8 +202,8 @@ class Client(object):
"""
client = cls(
country=country or DEFAULT_COUNTRY,
language=language or DEFAULT_LANGUAGE,
country=country or core.DEFAULT_COUNTRY,
language=language or core.DEFAULT_LANGUAGE,
)
client._auth = core.Auth(client.gateway, None, refresh_token)
client.refresh()

View File

@ -18,6 +18,8 @@ CLIENT_ID = 'LGAO221A02'
OAUTH_SECRET_KEY = 'c053c2a6ddeb7ad97cb0eed0dcb31cf8'
OAUTH_CLIENT_KEY = 'LGAO221A02'
DATE_FORMAT = '%a, %d %b %Y %H:%M:%S +0000'
DEFAULT_COUNTRY = 'US'
DEFAULT_LANGUAGE = 'en-US'
def gen_uuid() -> str:
@ -253,6 +255,21 @@ class Gateway(object):
def oauth_url(self):
return oauth_url(self.auth_base, self.country, self.language)
def serialize(self) -> Dict[str, str]:
return {
'auth_base': self.auth_base,
'api_root': self.api_root,
'oauth_root': self.oauth_root,
'country': self.country,
'language': self.language,
}
@classmethod
def deserialize(cls, data: Dict[str, Any]) -> 'Gateway':
return cls(data['auth_base'], data['api_root'], data['oauth_root'],
data.get('country', DEFAULT_COUNTRY),
data.get('language', DEFAULT_LANGUAGE))
class Auth(object):
def __init__(self, gateway, access_token, refresh_token):
@ -286,6 +303,12 @@ class Auth(object):
self.refresh_token)
return Auth(self.gateway, new_access_token, self.refresh_token)
def serialize(self) -> Dict[str, str]:
return {
'access_token': self.access_token,
'refresh_token': self.refresh_token,
}
class Session(object):
def __init__(self, auth, session_id) -> None: