def validate_request(self, path, action, body=None, query=None):
        """Check if the given request is valid.
           Validates the body and the query

           # Rules to validate the BODY:
           # Let's limit this to mime types that either contain 'text' or 'json'
           # 1. if body is None, there must not be any required parameters in
           #    the given schema
           # 2. if the mime type contains 'json', body must not be '', but can
           #    be {}
           # 3. if the mime type contains 'text', body can be any string
           # 4. if no mime type ('consumes') is given.. DISALLOW
           # 5. if the body is empty ('' or {}), there must not be any required parameters
           # 6. if there is something in the body, it must adhere to the given schema
           #    -> will call the validate body function

        Args:
            path: path of the request.
            action: action of the request(get, post, delete...).
            body: body of the request.
            query: dict with the query parameters.

        Returns:
            True if the request is valid, False otherwise.

        TODO:
            - For every http method, we might want to have some general checks
               before we go deeper into the parameters
            - Check form data parameters
        """
        path_name, path_spec = self.get_path_spec(path)

        if path_spec is None:  # reject unknown path
            logging.warn("there is no path")
            return False

        if action not in path_spec.keys():  # reject unknown http method
            logging.warn("this http method is unknown '{0}'".format(action))
            return False

        action_spec = path_spec[action]

        # check general post body guidelines (body + mime type)
        if action == 'post':
            is_ok, msg = _validate_post_body(body, action_spec)
            if not is_ok:
                logging.warn("the general post body did not validate due to '{0}'".format(msg))
                return False

        # If the body is empty and it validated so far, we can return here
        # unless there is something in the query parameters we need to check
        body_is_empty = body in [None, {}, '']
        if body_is_empty and query is None:
            return True

        # Check body parameters
        is_ok, msg = self._validate_body_parameters(body, action_spec)
        if not is_ok:
            logging.warn("the parameters in the body did not validate due to '{0}'".format(msg))
            return False

        # Check query parameters
        if query is not None and not self._validate_query_parameters(query, action_spec):
            return False

        return True
def validate_request ( self , path , action , body = None , query = None ) : path_name , path_spec = self . get_path_spec ( path ) if path_spec is None : logging . warn ( "there is no path" ) return False if action not in path_spec . keys ( ) : logging . warn ( "this http method is unknown '{0}'" . format ( action ) ) return False action_spec = path_spec [ action ] if action == 'post' : is_ok , msg = _validate_post_body ( body , action_spec ) if not is_ok : logging . warn ( "the general post body did not validate due to '{0}'" . format ( msg ) ) return False body_is_empty = body in [ None , { } , '' ] if body_is_empty and query is None : return True is_ok , msg = self . _validate_body_parameters ( body , action_spec ) if not is_ok : logging . warn ( "the parameters in the body did not validate due to '{0}'" . format ( msg ) ) return False if query is not None and not self . _validate_query_parameters ( query , action_spec ) : return False return True
Check if the given request is valid.
           Validates the body and the query

           # Rules to validate the BODY:
           # Let's limit this to mime types that either contain 'text' or 'json'
           # 1. if body is None, there must not be any required parameters in
           #    the given schema
           # 2. if the mime type contains 'json', body must not be '', but can
           #    be {}
           # 3. if the mime type contains 'text', body can be any string
           # 4. if no mime type ('consumes') is given.. DISALLOW
           # 5. if the body is empty ('' or {}), there must not be any required parameters
           # 6. if there is something in the body, it must adhere to the given schema
           #    -> will call the validate body function

        Args:
            path: path of the request.
            action: action of the request(get, post, delete...).
            body: body of the request.
            query: dict with the query parameters.

        Returns:
            True if the request is valid, False otherwise.

        TODO:
            - For every http method, we might want to have some general checks
               before we go deeper into the parameters
            - Check form data parameters