Example #1
0
 def action_view():
     if request.headers.get('Content-Type', None) != "application/json":
         return json.dumps({
             "error": 'Content-Type must be "application/json"'
         }), 400
     try:
         payload = JSONPayload.from_string(request.data)
     except ValueError:
         return json.dumps({
             "error": "Invalid JSON"
         }), 400
     # If function takes no arguments, request must be empty
     if 'accepts' not in self.spec and payload:
         return json.dumps({
             "error": "%s takes no arguments. Request content must be empty" % self.name 
         }), 400
     # If function takes arguments, request cannot be empty
     if 'accepts' in self.spec and not payload:
         return json.dumps({
             "error": "%s takes arguments. Request content cannot be empty" % self.name
         }), 400
     try:
         data = apply_to_action_func(self.raw_func, payload)
     # If the user threw an APIError
     except APIError as err:
         return json.dumps({
             "error": err.args[0]
         }), err.http_code
     # Any other exception should be handled gracefully
     except Exception as e:
         if debug: raise e
         return json.dumps({
             "error": "Internal Server Error"
         }), 500
     return json.dumps(data)
Example #2
0
 def __call__(self, *args, **kwargs):
     # This seems redundant, but is necessary to make sure local
     # actions behave same as remote ones
     data = serialize_action_arguments(*args, **kwargs)
     return apply_to_action_func(self.raw_func, data)