diff --git a/example.py b/example.py index a84392d..d302720 100644 --- a/example.py +++ b/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__':