Skip to content

Commit

Permalink
Add (de)serialize methods to Gateway and Auth
Browse files Browse the repository at this point in the history
This makes the parts belonging to the class more local and encapsulated.
This will make adding API v2 easier.
  • Loading branch information
gladhorn committed Jan 22, 2020
1 parent f44aa91 commit d35c3ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
30 changes: 7 additions & 23 deletions wideq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

from . import core

DEFAULT_COUNTRY = 'US'
DEFAULT_LANGUAGE = 'en-US'
#: Represents an unknown enum value.
_UNKNOWN = 'Unknown'

Expand Down Expand Up @@ -79,8 +77,8 @@ def __init__(self,
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
Expand Down Expand Up @@ -146,12 +144,7 @@ def load(cls, state: Dict[str, Any]) -> 'Client':
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']
Expand Down Expand Up @@ -181,19 +174,10 @@ def dump(self) -> Dict[str, Any]:
}

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
Expand All @@ -218,8 +202,8 @@ def from_token(cls, refresh_token,
"""

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()
Expand Down
23 changes: 23 additions & 0 deletions wideq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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:
Expand Down Expand Up @@ -253,6 +255,21 @@ def discover(cls, country, language) -> 'Gateway':
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):
Expand Down Expand Up @@ -286,6 +303,12 @@ def refresh(self):
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:
Expand Down

0 comments on commit d35c3ff

Please sign in to comment.