예제 #1
0
 def _handle_request(self, method, args, kwargs):
     func = getattr(self._plugin, method)
     requested_parameters = set(
         Toolbox.get_parameter_names(func)) - {'self'}
     difference = set(kwargs.keys()) - requested_parameters
     if difference:
         # Analog error message as the default CherryPy behavior
         return {
             'success':
             False,
             'exception':
             'Unexpected query string parameters: {0}'.format(
                 ', '.join(difference))
         }
     difference = requested_parameters - set(kwargs.keys())
     if difference:
         # Analog error message as the default CherryPy behavior
         return {
             'success':
             False,
             'exception':
             'Missing parameters: {0}'.format(', '.join(difference))
         }
     try:
         return {'success': True, 'response': func(*args, **kwargs)}
     except Exception as exception:
         return {
             'success': False,
             'exception': str(exception),
             'stacktrace': traceback.format_exc()
         }
예제 #2
0
def capture_fields(func):
    field_names = Toolbox.get_parameter_names(func)
    field_names.pop(0)  # Remove `self`

    @wraps(func)
    def new_init(self, *args, **kwargs):
        self._loaded_fields = set(field_names[:len(args)] +
                                  list(kwargs.keys()))
        self._init_done = True
        func(self, *args, **kwargs)

    return new_init
예제 #3
0
 def _handle_request(self, method, args, kwargs):
     func = getattr(self._plugin, method)
     # Always expect a web_request
     web_request = PluginWebRequest.deserialize(
         kwargs['plugin_web_request'])
     passed_parameters = set(web_request.params.keys())
     if web_request.version > 1:
         passed_parameters.add('plugin_web_request')
     requested_parameters = set(
         Toolbox.get_parameter_names(func)) - {'self'}
     difference = set(passed_parameters) - requested_parameters
     if difference and web_request.version == 1:
         # Analog error message as the default CherryPy behavior
         return {
             'success':
             False,
             'exception':
             'Unexpected query string parameters: {0}'.format(
                 ', '.join(difference))
         }
     difference = requested_parameters - (set(passed_parameters)
                                          | {'request_body'})
     if difference:
         # Analog error message as the default CherryPy behavior
         return {
             'success':
             False,
             'exception':
             'Missing parameters: {0}'.format(', '.join(difference))
         }
     try:
         to_pass_arguments = {}  # type: Dict[str, Any]
         for req_param in requested_parameters:
             if req_param == 'plugin_web_request':
                 to_pass_arguments[req_param] = web_request
             elif req_param == 'request_body':
                 to_pass_arguments[req_param] = web_request.body
             else:
                 to_pass_arguments[req_param] = web_request.params[
                     req_param]
         func_return = func(*args, **to_pass_arguments)
         try:
             if isinstance(func_return, PluginWebResponse):
                 serialized_func_return = func_return.serialize()
             else:
                 serialized_func_return = PluginWebResponse(
                     status_code=200,
                     body=func_return,
                     version=web_request.version).serialize()
         except AttributeError as ex:
             error_msg = "[RUNTIME] Could not serialize the returned object for function call {} with return data: {}".format(
                 method, func_return)
             self._writer.log(error_msg)
             return {'success': False, 'exception': str(error_msg)}
         return {'success': True, 'response': serialized_func_return}
     except Exception as exception:
         return {
             'success': False,
             'exception': str(exception),
             'stacktrace': traceback.format_exc()
         }