Skip to content
This repository was archived by the owner on Oct 6, 2021. It is now read-only.

Commit a43e837

Browse files
author
Jeffrey Sun
committed
Fix demo_server scope to render the correct services and also improve error handling
1 parent 89a7352 commit a43e837

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

examples/demo_server.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from six.moves.urllib.parse import parse_qs, urlparse
99

1010
from kloudless import get_authorization_url, get_token_from_code, Account
11+
from kloudless.exceptions import APIException
1112
from kloudless.util import logger
1213

1314
logger.setLevel('DEBUG')
@@ -33,6 +34,12 @@ def http_redirect(self, url):
3334
self.send_header("Cache-Control", "no-store, must-revalidate")
3435
self.end_headers()
3536

37+
def http_json_response(self, status_code, data):
38+
self.send_response(status_code)
39+
self.send_header("Content-type", "application/json")
40+
self.end_headers()
41+
self.wfile.write(json.dumps(data).encode('utf8'))
42+
3643
def do_GET(self):
3744

3845
global state_holder
@@ -75,7 +82,7 @@ def do_GET(self):
7582

7683
url, state = get_authorization_url(app_id,
7784
redirect_uri=redirect_url,
78-
scope='any:normal.storage')
85+
scope='storage')
7986
state_holder = state # Store state for security check later
8087

8188
# Redirect user to start first leg of authorization flow
@@ -87,7 +94,7 @@ def do_GET(self):
8794
###############################
8895
url, state = get_authorization_url(app_id,
8996
redirect_uri=redirect_url,
90-
scope='any:normal.calendar')
97+
scope='calendar')
9198
state_holder = state # Store state for security check later
9299

93100
# Redirect user to start first leg of authorization flow
@@ -97,44 +104,42 @@ def do_GET(self):
97104
######################
98105
# Callback endpoint
99106
######################
100-
101-
params = self.get_query_params(self.path)
102-
103-
# Exchange token from authorization code
104-
token = get_token_from_code(app_id=app_id, api_key=api_key,
105-
orig_state=state_holder,
106-
orig_redirect_uri=redirect_url,
107-
**params)
108-
109-
account = Account(token=token)
110-
# Request to https://api.kloudless.com/account/me
111-
account_resp = account.get()
112-
resp_data = {
113-
'msg': 'Successfully retrieving JSON data after connecting'
114-
' your account.',
115-
'account': account_resp.data
116-
}
117-
118-
apis = account_resp.data['apis']
119-
if 'storage' in apis:
120-
# Request to https://api.kloudless.com/account/me/storage/folders/root/contents
121-
root_folder_contents = account.get(
122-
'storage/folders/root/contents'
123-
)
124-
resp_data['root_folder_contents'] = root_folder_contents.data
125-
126-
elif 'calendar' in apis:
127-
# Request to https://api.kloudless.com/account/me/cal/calendars/primary/events
128-
primary_calendar_events = account.get(
129-
'cal/calendars/primary/events'
130-
)
131-
resp_data['primary_calendar_events'] = (
132-
primary_calendar_events.data)
133-
134-
self.send_response(200)
135-
self.send_header("Content-type", "application/json")
136-
self.end_headers()
137-
self.wfile.write(json.dumps(resp_data).encode('utf8'))
107+
try:
108+
params = self.get_query_params(self.path)
109+
110+
# Exchange token from authorization code
111+
token = get_token_from_code(app_id=app_id, api_key=api_key,
112+
orig_state=state_holder,
113+
orig_redirect_uri=redirect_url,
114+
**params)
115+
116+
account = Account(token=token)
117+
# Request to https://api.kloudless.com/account/me
118+
account_resp = account.get()
119+
resp_data = {
120+
'msg': 'Successfully retrieving JSON data after connecting'
121+
' your account.',
122+
'account': account_resp.data
123+
}
124+
125+
apis = account_resp.data['apis']
126+
if 'calendar' in apis:
127+
# Request to https://api.kloudless.com/account/me/cal/calendars/primary/events
128+
primary_calendar_events = account.get(
129+
'cal/calendars/primary/events'
130+
)
131+
resp_data['primary_calendar_events'] = (
132+
primary_calendar_events.data)
133+
elif 'storage' in apis:
134+
# Request to https://api.kloudless.com/account/me/storage/folders/root/contents
135+
root_folder_contents = account.get(
136+
'storage/folders/root/contents'
137+
)
138+
resp_data['root_folder_contents'] = root_folder_contents.data
139+
except APIException as e:
140+
self.http_json_response(e.status, e.error_data)
141+
else:
142+
self.http_json_response(200, resp_data)
138143

139144

140145
if __name__ == '__main__':

0 commit comments

Comments
 (0)