mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-19 08:40:25 -07:00
Some memoization
This commit is contained in:
parent
009ed8e330
commit
e79b67051c
52
example.py
52
example.py
@ -1,17 +1,53 @@
|
||||
import wideq
|
||||
import json
|
||||
|
||||
STATE_FILE = 'wideq_state.json'
|
||||
|
||||
|
||||
def load_state():
|
||||
"""Load the current state data for this example.
|
||||
"""
|
||||
|
||||
try:
|
||||
with open(STATE_FILE) as f:
|
||||
return json.load(f)
|
||||
except IOError:
|
||||
return {}
|
||||
|
||||
|
||||
def save_state(state):
|
||||
"""Dump the current state to disk.
|
||||
"""
|
||||
|
||||
with open(STATE_FILE, 'w') as f:
|
||||
json.dump(state, f)
|
||||
|
||||
|
||||
def example():
|
||||
gw = wideq.gateway_info()
|
||||
oauth_base = gw['empUri']
|
||||
api_root = gw['thinqUri']
|
||||
print(wideq.oauth_url(oauth_base))
|
||||
state = load_state()
|
||||
|
||||
access_token = wideq.parse_oauth_callback(input())
|
||||
print(access_token)
|
||||
# Get the URLs for the API.
|
||||
if 'oauth_base' not in state and 'api_root' not in state:
|
||||
gw = wideq.gateway_info()
|
||||
state['oauth_base'] = gw['empUri']
|
||||
state['api_root'] = gw['thinqUri']
|
||||
oauth_base = state['oauth_base']
|
||||
api_root = state['api_root']
|
||||
|
||||
session_info = wideq.login(api_root, access_token)
|
||||
print(session_info)
|
||||
# If we don't have an access token, authenticate.
|
||||
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)
|
||||
access_token = state['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)
|
||||
print(session_info)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user