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

Move OO interface to the library

This commit is contained in:
Adrian Sampson 2018-01-08 11:44:08 -08:00
parent 82fca74b05
commit 7ff25e9eea
2 changed files with 66 additions and 66 deletions

View File

@ -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()

View File

@ -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)