Skip to content

Commit 70eaf35

Browse files
committed
Headers
Added X-Count-Total for pagination of GET method, Parse X-HTTP-Method-Override from client, Parse Request Accept content-type from URL and Header committer: Ralf Schlatterbeck <[email protected]>
1 parent 689ed1c commit 70eaf35

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

roundup/rest.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
and/or modify under the same terms as Python.
66
"""
77

8+
import urlparse
9+
import os
810
import json
911
import pprint
1012
import sys
@@ -74,8 +76,6 @@ def __init__(self, client, db):
7476
tracker = self.client.env['TRACKER_NAME']
7577
self.base_path = '%s://%s/%s/rest/' % (protocol, host, tracker)
7678

77-
print self.base_path
78-
7979
def get_collection(self, class_name, input):
8080
if not self.db.security.hasPermission('View', self.db.getuid(),
8181
class_name):
@@ -87,6 +87,7 @@ def get_collection(self, class_name, input):
8787
if self.db.security.hasPermission('View', self.db.getuid(),
8888
class_name,
8989
itemid=item_id)]
90+
self.client.setHeader("X-Count-Total", str(len(result)))
9091
return 200, result
9192

9293
def get_element(self, class_name, item_id, input):
@@ -230,21 +231,39 @@ def dispatch(self, method, uri, input):
230231
# 2 - attribute
231232
resource_uri = uri.split("/")[1]
232233

233-
self.client.setHeader("Access-Control-Allow-Methods",
234-
"HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH")
234+
# if X-HTTP-Method-Override is set, follow the override method
235+
method = self.client.request.headers.getheader('X-HTTP-Method-Override') or method
236+
237+
# get the request format for response
238+
# priority : extension from uri (/rest/issue.json),
239+
# header (Accept: application/json, application/xml)
240+
# default (application/json)
241+
242+
# format_header need a priority parser
243+
format_ext = os.path.splitext(urlparse.urlparse(uri).path)[1][1:]
244+
format_header = self.client.request.headers.getheader('Accept')[12:]
245+
format_output = format_ext or format_header or "json"
246+
247+
self.client.setHeader("Access-Control-Allow-Origin", "*")
235248
self.client.setHeader("Access-Control-Allow-Headers",
236-
"Content-Type, Authorization,"
249+
"Content-Type, Authorization, "
237250
"X-HTTP-Method-Override")
238-
self.client.setHeader("Allow",
239-
"HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH")
240251

241252
output = None
242253
try:
243254
if resource_uri in self.db.classes:
255+
self.client.setHeader("Allow",
256+
"HEAD, OPTIONS, POST, DELETE")
257+
self.client.setHeader("Access-Control-Allow-Methods",
258+
"HEAD, OPTIONS, POST, DELETE")
244259
response_code, output = getattr(self, "%s_collection" % method.lower())(
245260
resource_uri, input)
246261
else:
247262
class_name, item_id = hyperdb.splitDesignator(resource_uri)
263+
self.client.setHeader("Allow",
264+
"HEAD, OPTIONS, GET, PUT, DELETE, PATCH")
265+
self.client.setHeader("Access-Control-Allow-Methods",
266+
"HEAD, OPTIONS, GET, PUT, DELETE, PATCH")
248267
response_code, output = getattr(self, "%s_element" % method.lower())(
249268
class_name, item_id, input)
250269

@@ -277,8 +296,12 @@ def dispatch(self, method, uri, input):
277296
print 'EXCEPTION AT', time.ctime()
278297
traceback.print_exc()
279298
finally:
280-
self.client.setHeader("Content-Type", "application/json")
281-
output = RoundupJSONEncoder().encode(output)
299+
if format_output.lower() == "json":
300+
self.client.setHeader("Content-Type", "application/json")
301+
output = RoundupJSONEncoder().encode(output)
302+
else:
303+
self.client.response_code = 406
304+
output = ""
282305

283306
return output
284307

0 commit comments

Comments
 (0)