Commit c3512cfc authored by Łukasz Nowak's avatar Łukasz Nowak

Improve header requirement wrapper.

Allow to require any header.

Configure GET/POST on /instance to require application/json in Accept and
Content-Type headers.
parent fa72de38
...@@ -39,18 +39,23 @@ import xml_marshaller ...@@ -39,18 +39,23 @@ import xml_marshaller
import json import json
import transaction import transaction
def requireAcceptApplicationJson(fn): def requireHeader(header_dict):
def wrapperRequireAcceptApplicationJson(self, *args, **kwargs): def outer(fn):
accept = self.REQUEST.getHeader('Accept') def wrapperRequireHeader(self, *args, **kwargs):
if accept is not None and accept == 'application/json': problem_dict = {}
return fn(self, *args, **kwargs) for header, value in header_dict.iteritems():
else: if self.REQUEST.getHeader(header) != value:
self.REQUEST.response.setStatus(400) problem_dict[header] = 'Header with value %r is required.' % value
self.REQUEST.response.setBody(json.dumps({"error": if not problem_dict:
"Accept: application/json header is required"})) return fn(self, *args, **kwargs)
return self.REQUEST.response else:
wrapperRequireAcceptApplicationJson.__doc__ = fn.__doc__ self.REQUEST.response.setStatus(400)
return wrapperRequireAcceptApplicationJson self.REQUEST.response.setBody(json.dumps(problem_dict))
return self.REQUEST.response
wrapperRequireHeader.__doc__ = fn.__doc__
return wrapperRequireHeader
return outer
def responseSupport(anonymous=False): def responseSupport(anonymous=False):
def outer(fn): def outer(fn):
...@@ -156,7 +161,8 @@ class InstancePublisher(GenericPublisher): ...@@ -156,7 +161,8 @@ class InstancePublisher(GenericPublisher):
return response return response
@responseSupport() @responseSupport()
@requireAcceptApplicationJson @requireHeader({'Accept': 'application/json',
'Content-Type': 'application/json'})
def __call__(self): def __call__(self):
"""Instance GET/POST support""" """Instance GET/POST support"""
if self.REQUEST['REQUEST_METHOD'] == 'POST': if self.REQUEST['REQUEST_METHOD'] == 'POST':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment