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

Crude memoization helper

This commit is contained in:
Adrian Sampson 2018-01-08 08:53:05 -08:00
parent a712ceb9ec
commit a28dd98d77

View File

@ -4,23 +4,42 @@ import json
STATE_FILE = 'wideq_state.json'
def load_state():
"""Load the current state data for this example.
class Memo(object):
"""Store memoization data persistently in a JSON file.
"""
def __init__(self, filename):
self.filename = filename
self.load()
def load(self):
"""Load the current memoization data."""
try:
with open(STATE_FILE) as f:
return json.load(f)
with open(self.filename) as f:
self.state = json.load(f)
except IOError:
return {}
self.state = {}
def save(self):
"""Dump the current state to disk."""
with open(self.filename, 'w') as f:
json.dump(self.state, f)
def save_state(state):
"""Dump the current state to disk.
def call(self, fn, key=None):
"""Call a function using memoization.
You can either specify a memoization key explicitly or, by
default, use the function's `__name__`.
"""
with open(STATE_FILE, 'w') as f:
json.dump(state, f)
key = key or fn.__name__
if key in self.state:
return self.state[key]
else:
ret = fn()
self.state[key] = ret
self.save()
return ret
def print_devices(devices):
@ -28,36 +47,43 @@ def print_devices(devices):
print(device['alias'])
def example():
state = load_state()
# Get the URLs for the API.
if 'oauth_base' not in state and 'api_root' not in state:
def endpoints():
gw = wideq.gateway_info()
state['oauth_base'] = gw['empUri']
state['api_root'] = gw['thinqUri']
save_state(state)
oauth_base = state['oauth_base']
api_root = state['api_root']
return {
'oauth_base': gw['empUri'],
'api_root': gw['thinqUri'],
}
# If we don't have an access token, authenticate.
if 'access_token' not in state:
def login(oauth_base):
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)
access_token = state['access_token']
return wideq.parse_oauth_callback(callback_url)
def start_session(api_root, access_token):
session_info = wideq.login(api_root, access_token)
print_devices(session_info['item'])
return session_info['jsessionId']
def example():
memo = Memo(STATE_FILE)
# Get the URLs for the API.
ep = memo.call(endpoints)
oauth_base = ep['oauth_base']
api_root = ep['api_root']
# Authenticate to get an access token.
access_token = memo.call(lambda: login(oauth_base), 'access_token')
# If we don't have a session, log in.
if 'session_id' not in state:
session_info = wideq.login(api_root, access_token)
state['session_id'] = session_info['jsessionId']
save_state(state)
print_devices(session_info['item'])
session_id = state['session_id']
session_id = memo.call(lambda: start_session(api_root, access_token),
'session_id')
# Request a list of devices.
devices = wideq.get_devices(api_root, access_token, session_id)