1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-16 15:20: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' STATE_FILE = 'wideq_state.json'
def load_state(): class Memo(object):
"""Load the current state data for this example. """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: try:
with open(STATE_FILE) as f: with open(self.filename) as f:
return json.load(f) self.state = json.load(f)
except IOError: 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): def call(self, fn, key=None):
"""Dump the current state to disk. """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: key = key or fn.__name__
json.dump(state, f) if key in self.state:
return self.state[key]
else:
ret = fn()
self.state[key] = ret
self.save()
return ret
def print_devices(devices): def print_devices(devices):
@ -28,36 +47,43 @@ def print_devices(devices):
print(device['alias']) print(device['alias'])
def example(): def endpoints():
state = load_state()
# Get the URLs for the API.
if 'oauth_base' not in state and 'api_root' not in state:
gw = wideq.gateway_info() gw = wideq.gateway_info()
state['oauth_base'] = gw['empUri'] return {
state['api_root'] = gw['thinqUri'] 'oauth_base': gw['empUri'],
save_state(state) 'api_root': gw['thinqUri'],
oauth_base = state['oauth_base'] }
api_root = state['api_root']
# 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) login_url = wideq.oauth_url(oauth_base)
print('Log in here:') print('Log in here:')
print(login_url) print(login_url)
print('Then paste the URL where the browser is redirected:') print('Then paste the URL where the browser is redirected:')
callback_url = input() callback_url = input()
state['access_token'] = wideq.parse_oauth_callback(callback_url) return wideq.parse_oauth_callback(callback_url)
save_state(state)
access_token = state['access_token']
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 we don't have a session, log in.
if 'session_id' not in state: session_id = memo.call(lambda: start_session(api_root, access_token),
session_info = wideq.login(api_root, access_token) 'session_id')
state['session_id'] = session_info['jsessionId']
save_state(state)
print_devices(session_info['item'])
session_id = state['session_id']
# Request a list of devices. # Request a list of devices.
devices = wideq.get_devices(api_root, access_token, session_id) devices = wideq.get_devices(api_root, access_token, session_id)