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

Commit 7e589f7

Browse files
author
Jeffrey Sun
committed
Merge branch 'jeffrey/DEV-3955-demo-server-fix-and-DEV-3932-204-response-parsing-fix'
2 parents 89a7352 + 5f27b3b commit 7e589f7

File tree

6 files changed

+55
-44
lines changed

6 files changed

+55
-44
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## 2.0.1
4+
* Fix demo_server authorization scope to render correct services and also
5+
improve its error handling.
6+
* Fix JSON parsing issue for HTTP 204 response.
7+
38
## 2.0.0
49
* The 2.0.0 version is NOT backwards compatible with previous versions. The
510
README has been updated on how to install and use this version of the

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def start_authorization_flow(request):
157157
"""
158158
url, state = get_authorization_url(app_id=settings.KLOUDLESS_APP_ID,
159159
redirect_uri=settings.KLOUDLESS_REDIRECT_URL,
160-
scope='any:normal.storage')
160+
scope='storage')
161161

162162
request.session['authorization_state'] = state
163163
return HttpResponseRedirect(url)

docs/source/readme.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ and calling it via ``urls.py``.
170170
"""
171171
url, state = get_authorization_url(app_id=settings.KLOUDLESS_APP_ID,
172172
redirect_uri=settings.KLOUDLESS_REDIRECT_URL,
173-
scope='any:normal.storage')
173+
scope='storage')
174174
175175
request.session['authorization_state'] = state
176176
return HttpResponseRedirect(url)

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__':

kloudless/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def _create_response_object(self, response):
149149

150150
url = response.url
151151

152-
if 'application/json' not in response.headers.get('content-type', ''):
152+
if ('application/json' not in response.headers.get('content-type', '')
153+
or not response.content):
153154
return Response(self, url, response)
154155

155156
try:

kloudless/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from __future__ import unicode_literals
22

3-
VERSION = '2.0.0'
3+
VERSION = '2.0.1'

0 commit comments

Comments
 (0)