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:
parent
a712ceb9ec
commit
a28dd98d77
98
example.py
98
example.py
@ -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.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
def __init__(self, filename):
|
||||||
with open(STATE_FILE) as f:
|
self.filename = filename
|
||||||
return json.load(f)
|
self.load()
|
||||||
except IOError:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
"""Load the current memoization data."""
|
||||||
|
try:
|
||||||
|
with open(self.filename) as f:
|
||||||
|
self.state = json.load(f)
|
||||||
|
except IOError:
|
||||||
|
self.state = {}
|
||||||
|
|
||||||
def save_state(state):
|
def save(self):
|
||||||
"""Dump the current state to disk.
|
"""Dump the current state to disk."""
|
||||||
"""
|
with open(self.filename, 'w') as f:
|
||||||
|
json.dump(self.state, f)
|
||||||
|
|
||||||
with open(STATE_FILE, 'w') as f:
|
def call(self, fn, key=None):
|
||||||
json.dump(state, f)
|
"""Call a function using memoization.
|
||||||
|
|
||||||
|
You can either specify a memoization key explicitly or, by
|
||||||
|
default, use the function's `__name__`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
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):
|
def print_devices(devices):
|
||||||
@ -28,36 +47,43 @@ def print_devices(devices):
|
|||||||
print(device['alias'])
|
print(device['alias'])
|
||||||
|
|
||||||
|
|
||||||
|
def endpoints():
|
||||||
|
gw = wideq.gateway_info()
|
||||||
|
return {
|
||||||
|
'oauth_base': gw['empUri'],
|
||||||
|
'api_root': gw['thinqUri'],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
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():
|
def example():
|
||||||
state = load_state()
|
memo = Memo(STATE_FILE)
|
||||||
|
|
||||||
# Get the URLs for the API.
|
# Get the URLs for the API.
|
||||||
if 'oauth_base' not in state and 'api_root' not in state:
|
ep = memo.call(endpoints)
|
||||||
gw = wideq.gateway_info()
|
oauth_base = ep['oauth_base']
|
||||||
state['oauth_base'] = gw['empUri']
|
api_root = ep['api_root']
|
||||||
state['api_root'] = gw['thinqUri']
|
|
||||||
save_state(state)
|
|
||||||
oauth_base = state['oauth_base']
|
|
||||||
api_root = state['api_root']
|
|
||||||
|
|
||||||
# If we don't have an access token, authenticate.
|
# Authenticate to get an access token.
|
||||||
if 'access_token' not in state:
|
access_token = memo.call(lambda: login(oauth_base), 'access_token')
|
||||||
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']
|
|
||||||
|
|
||||||
# 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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user