1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-20 01:00:07 -07:00

Merge remote-tracking branch 'upstream/master'

This commit is contained in:
David 2020-01-22 12:19:26 -05:00
commit a62572e599
3 changed files with 33 additions and 26 deletions

View File

@ -235,14 +235,14 @@ def main() -> None:
args = parser.parse_args()
country_regex = re.compile(r"^[A-Z]{2,3}$")
if not country_regex.match(args.country):
print("Error: Country must be two or three letters" \
print("Error: Country must be two or three letters"
f" all upper case (e.g. US, NO, KR) got: '{args.country}'",
file=sys.stderr)
exit(1)
language_regex = re.compile(r"^[a-z]{2,3}-[A-Z]{2,3}$")
if not language_regex.match(args.language):
print("Error: Language must be a combination of language" \
" and country (e.g. en-US, no-NO, kr-KR)" \
print("Error: Language must be a combination of language"
" and country (e.g. en-US, no-NO, kr-KR)"
f" got: '{args.language}'",
file=sys.stderr)
exit(1)

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: