8
8
from six .moves .urllib .parse import parse_qs , urlparse
9
9
10
10
from kloudless import get_authorization_url , get_token_from_code , Account
11
+ from kloudless .exceptions import APIException
11
12
from kloudless .util import logger
12
13
13
14
logger .setLevel ('DEBUG' )
@@ -33,6 +34,12 @@ def http_redirect(self, url):
33
34
self .send_header ("Cache-Control" , "no-store, must-revalidate" )
34
35
self .end_headers ()
35
36
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
+
36
43
def do_GET (self ):
37
44
38
45
global state_holder
@@ -75,7 +82,7 @@ def do_GET(self):
75
82
76
83
url , state = get_authorization_url (app_id ,
77
84
redirect_uri = redirect_url ,
78
- scope = 'any:normal. storage' )
85
+ scope = 'storage' )
79
86
state_holder = state # Store state for security check later
80
87
81
88
# Redirect user to start first leg of authorization flow
@@ -87,7 +94,7 @@ def do_GET(self):
87
94
###############################
88
95
url , state = get_authorization_url (app_id ,
89
96
redirect_uri = redirect_url ,
90
- scope = 'any:normal. calendar' )
97
+ scope = 'calendar' )
91
98
state_holder = state # Store state for security check later
92
99
93
100
# Redirect user to start first leg of authorization flow
@@ -97,44 +104,42 @@ def do_GET(self):
97
104
######################
98
105
# Callback endpoint
99
106
######################
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 )
138
143
139
144
140
145
if __name__ == '__main__' :
0 commit comments