From 7ff25e9eea222382107c6e0e8c933e47b46f9d52 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 8 Jan 2018 11:44:08 -0800 Subject: [PATCH] Move OO interface to the library --- example.py | 71 ++++-------------------------------------------------- wideq.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/example.py b/example.py index c2841c7..a8cf2e2 100644 --- a/example.py +++ b/example.py @@ -28,67 +28,6 @@ def print_devices(devices): print(device['alias']) -class Gateway(object): - def __init__(self, oauth_base, api_root): - self.oauth_base = oauth_base - self.api_root = api_root - - @classmethod - def discover(cls): - gw = wideq.gateway_info() - return cls(gw['empUri'], gw['thinqUri']) - - @classmethod - def load(cls, data): - return cls(data['oauth_base'], data['api_root']) - - def dump(self): - return {'oauth_base': self.oauth_base, 'api_root': self.api_root} - - def oauth_url(self): - return wideq.oauth_url(self.oauth_base) - - -class Auth(object): - def __init__(self, gateway, access_token): - self.gateway = gateway - self.access_token = access_token - - def start_session(self): - """Start an API session for the logged-in user. Return the - Session object and the user's devices. - """ - - session_info = wideq.login(self.gateway.api_root, self.access_token) - session_id = session_info['jsessionId'] - return Session(self, session_id), session_info['item'] - - def dump(self): - return self.access_token - - @classmethod - def load(cls, gateway, data): - return cls(gateway, data) - - -class Session(object): - def __init__(self, auth, session_id): - self.auth = auth - self.session_id = session_id - - def get_devices(self): - return wideq.get_devices(self.auth.gateway.api_root, - self.auth.access_token, - self.session_id) - - def dump(self): - return self.session_id - - @classmethod - def load(cls, auth, data): - return cls(auth, data) - - def authenticate(gateway): login_url = gateway.oauth_url() print('Log in here:') @@ -96,7 +35,7 @@ def authenticate(gateway): print('Then paste the URL where the browser is redirected:') callback_url = input() access_token = wideq.parse_oauth_callback(callback_url) - return Auth(gateway, access_token) + return wideq.Auth(gateway, access_token) def example(): @@ -105,16 +44,16 @@ def example(): # Get the gateway, which contains the base URLs and hostnames for # accessing the API. if 'gateway' in state: - gateway = Gateway.load(state['gateway']) + gateway = wideq.Gateway.load(state['gateway']) else: - gateway = Gateway.discover() + gateway = wideq.Gateway.discover() state['gateway'] = gateway.dump() save_state(state) # Authenticate the user. if 'auth' in state: - auth = Auth.load(gateway, state['auth']) + auth = wideq.Auth.load(gateway, state['auth']) else: auth = authenticate(gateway) @@ -123,7 +62,7 @@ def example(): # Start a session. if 'session' in state: - session = Session.load(auth, state['session']) + session = wideq.Session.load(auth, state['session']) devices = None else: session, devices = auth.start_session() diff --git a/wideq.py b/wideq.py index 2d9a8c1..ef5db87 100644 --- a/wideq.py +++ b/wideq.py @@ -92,3 +92,64 @@ def get_devices(api_root, access_token, session_id): } res = requests.post(url, json=req_data, headers=headers) return res.json()[DATA_ROOT]['item'] + + +class Gateway(object): + def __init__(self, oauth_base, api_root): + self.oauth_base = oauth_base + self.api_root = api_root + + @classmethod + def discover(cls): + gw = gateway_info() + return cls(gw['empUri'], gw['thinqUri']) + + @classmethod + def load(cls, data): + return cls(data['oauth_base'], data['api_root']) + + def dump(self): + return {'oauth_base': self.oauth_base, 'api_root': self.api_root} + + def oauth_url(self): + return oauth_url(self.oauth_base) + + +class Auth(object): + def __init__(self, gateway, access_token): + self.gateway = gateway + self.access_token = access_token + + def start_session(self): + """Start an API session for the logged-in user. Return the + Session object and the user's devices. + """ + + session_info = login(self.gateway.api_root, self.access_token) + session_id = session_info['jsessionId'] + return Session(self, session_id), session_info['item'] + + def dump(self): + return self.access_token + + @classmethod + def load(cls, gateway, data): + return cls(gateway, data) + + +class Session(object): + def __init__(self, auth, session_id): + self.auth = auth + self.session_id = session_id + + def get_devices(self): + return get_devices(self.auth.gateway.api_root, + self.auth.access_token, + self.session_id) + + def dump(self): + return self.session_id + + @classmethod + def load(cls, auth, data): + return cls(auth, data)