Skip to content

Commit 3736b3b

Browse files
committed
Change the way core function is called
Change the return header to allow all methods committer: Ralf Schlatterbeck <[email protected]>
1 parent 8caba00 commit 3736b3b

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

roundup/rest.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def format_object(self, *args, **kwargs):
2828
except Unauthorised, msg:
2929
code = 403
3030
data = msg
31-
except (hyperdb.DesignatorError, UsageError), msg:
31+
except UsageError, msg:
3232
code = 400
3333
data = msg
3434
except (AttributeError, Reject), msg:
@@ -72,7 +72,7 @@ class RestfulInstance(object):
7272
"""The RestfulInstance performs REST request from the client"""
7373

7474
def __init__(self, client, db):
75-
self.client = client # it might be unnecessary to receive the client
75+
self.client = client
7676
self.db = db
7777

7878
protocol = 'http'
@@ -140,6 +140,20 @@ def prop_from_arg(self, cl, key, value, itemid=None):
140140

141141
return prop
142142

143+
def error_obj(self, status, msg, source=None):
144+
"""Return an error object"""
145+
self.client.response_code = status
146+
result = {
147+
'error': {
148+
'status': status,
149+
'msg': msg
150+
}
151+
}
152+
if source is not None:
153+
result['error']['source'] = source
154+
155+
return result
156+
143157
@_data_decorator
144158
def get_collection(self, class_name, input):
145159
"""GET resource from class URI.
@@ -744,9 +758,9 @@ def dispatch(self, method, uri, input):
744758
# default (application/json)
745759

746760
# format_header need a priority parser
747-
format_ext = os.path.splitext(urlparse.urlparse(uri).path)[1][1:]
748-
format_header = headers.getheader('Accept')[12:]
749-
format_output = format_ext or format_header or "json"
761+
ext_type = os.path.splitext(urlparse.urlparse(uri).path)[1][1:]
762+
accept_header = headers.getheader('Accept')[12:]
763+
data_type = ext_type or accept_header or "json"
750764

751765
# check for pretty print
752766
try:
@@ -760,42 +774,39 @@ def dispatch(self, method, uri, input):
760774
"Access-Control-Allow-Headers",
761775
"Content-Type, Authorization, X-HTTP-Method-Override"
762776
)
763-
if resource_uri in self.db.classes:
764-
self.client.setHeader(
765-
"Allow",
766-
"HEAD, OPTIONS, GET, POST, DELETE"
767-
)
768-
self.client.setHeader(
769-
"Access-Control-Allow-Methods",
770-
"HEAD, OPTIONS, GET, POST, DELETE"
771-
)
772-
else:
773-
self.client.setHeader(
774-
"Allow",
775-
"HEAD, OPTIONS, GET, PUT, DELETE, PATCH"
776-
)
777-
self.client.setHeader(
778-
"Access-Control-Allow-Methods",
779-
"HEAD, OPTIONS, GET, PUT, DELETE, PATCH"
780-
)
777+
self.client.setHeader(
778+
"Allow",
779+
"HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH"
780+
)
781+
self.client.setHeader(
782+
"Access-Control-Allow-Methods",
783+
"HEAD, OPTIONS, GET, PUT, DELETE, PATCH"
784+
)
785+
try:
786+
class_name, item_id = hyperdb.splitDesignator(resource_uri)
787+
except hyperdb.DesignatorError, msg:
788+
class_name = resource_uri
789+
item_id = None
781790

782791
# Call the appropriate method
783-
output = None
784-
if resource_uri in self.db.classes:
785-
output = getattr(
786-
self, "%s_collection" % method.lower()
787-
)(resource_uri, input)
792+
if (class_name not in self.db.classes) or (len(uri_split) > 3):
793+
output = self.error_obj(404, "Not found")
794+
elif item_id is None:
795+
if len(uri_split) == 2:
796+
output = getattr(
797+
self, "%s_collection" % method.lower()
798+
)(class_name, input)
788799
else:
789-
class_name, item_id = hyperdb.splitDesignator(resource_uri)
790-
if len(uri_split) == 3:
800+
if len(uri_split) == 2:
791801
output = getattr(
792-
self, "%s_attribute" % method.lower()
793-
)(class_name, item_id, uri_split[2], input)
802+
self, "%s_element" % method.lower()
803+
)(class_name, item_id, input)
794804
else:
795805
output = getattr(
796-
self, "%s_element" % method.lower()
797-
)(class_name, item_id, input)
806+
self, "%s_attribute" % method.lower()
807+
)(class_name, item_id, uri_split[2], input)
798808

809+
# Format the content type
799810
if format_output.lower() == "json":
800811
self.client.setHeader("Content-Type", "application/json")
801812
if pretty_output:

0 commit comments

Comments
 (0)