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

Use a chain of objects for getting a session

This commit is contained in:
Adrian Sampson 2018-01-08 11:37:59 -08:00
parent 696f728752
commit 82fca74b05

View File

@ -28,39 +28,114 @@ def print_devices(devices):
print(device['alias']) 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:')
print(login_url)
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)
def example(): def example():
state = load_state() state = load_state()
# Get the URLs for the API. # Get the gateway, which contains the base URLs and hostnames for
if 'oauth_base' not in state and 'api_root' not in state: # accessing the API.
gw = wideq.gateway_info() if 'gateway' in state:
state['oauth_base'] = gw['empUri'] gateway = Gateway.load(state['gateway'])
state['api_root'] = gw['thinqUri'] else:
save_state(state) gateway = Gateway.discover()
oauth_base = state['oauth_base']
api_root = state['api_root']
# If we don't have an access token, authenticate. state['gateway'] = gateway.dump()
if 'access_token' not in state:
login_url = wideq.oauth_url(oauth_base)
print('Log in here:')
print(login_url)
print('Then paste the URL where the browser is redirected:')
callback_url = input()
state['access_token'] = wideq.parse_oauth_callback(callback_url)
save_state(state) save_state(state)
access_token = state['access_token']
# If we don't have a session, log in. # Authenticate the user.
if 'session_id' not in state: if 'auth' in state:
session_info = wideq.login(api_root, access_token) auth = Auth.load(gateway, state['auth'])
state['session_id'] = session_info['jsessionId'] else:
auth = authenticate(gateway)
state['auth'] = auth.dump()
save_state(state) save_state(state)
print_devices(session_info['item'])
session_id = state['session_id']
# Request a list of devices. # Start a session.
devices = wideq.get_devices(api_root, access_token, session_id) if 'session' in state:
session = Session.load(auth, state['session'])
devices = None
else:
session, devices = auth.start_session()
state['session'] = session.dump()
save_state(state)
# Request a list of devices, if we didn't get them "for free"
# already by starting the session.
if not devices:
devices = session.get_devices()
print_devices(devices) print_devices(devices)