예제 #1
0
 def test_init(self):
     unimplemented_verb = exception.UnimplementedVerbError("GET")
     self.assertEqual(unimplemented_verb.http_status,
                      STATUS.NOT_IMPLEMENTED)
     self.assertEqual(unimplemented_verb.message,
                      "API does not implement the HTTP Verb")
     self.assertEqual(unimplemented_verb.stack_trace, [{"verb": "GET"}])
예제 #2
0
 def options(self, *args, **kwargs):
     unimplemented_verb_error = exception.UnimplementedVerbError(VERB.OPTIONS)
     unimplemented_verb_error.request = self.request
     raise unimplemented_verb_error
예제 #3
0
 def delete(self, *args, **kwargs):
     unimplemented_verb_error = exception.UnimplementedVerbError(VERB.DELETE)
     unimplemented_verb_error.request = self.request
     raise unimplemented_verb_error
예제 #4
0
 def patch(self, *args, **kwargs):
     unimplemented_verb_error = exception.UnimplementedVerbError(VERB.PATCH)
     unimplemented_verb_error.request = self.request
     raise unimplemented_verb_error
예제 #5
0
 def head(self, *args, **kwargs):
     unimplemented_verb_error = exception.UnimplementedVerbError(VERB.HEAD)
     unimplemented_verb_error.request = self.request
     raise unimplemented_verb_error
예제 #6
0
    def __call__(self, environ, start_response):

        self.logger.info("handler %s.%s; callable execution start" % (self.__module__, self.__class__.__name__))

        try:
            # register additional serializers and de-serializers
            self.request.register_deserializers(self.register_deserializers())
            self.response.register_serializers(self.register_serializers())

            request_method = self.request.method

            # ensure we support the HTTP verb
            if not VERB.is_supported_verb(self.request.method):
                unimplemented_verb_error = exception.UnimplementedVerbError(self.request.method)
                unimplemented_verb_error.request = self.request
                raise unimplemented_verb_error

            #: Setup serializers
            self._setup_serializers()

            #: Authentication
            if self.__provider_config__.authentication is not None:
                self.__provider_config__.authentication.debug = self.debug
                self.provider_authentication = self.__provider_config__.authentication
                self.provider_authentication.request = self.request

            #: Configuration as provided by the API or default of a VerbConfig object
            verb_parser_config = self.__parser_config__.get_config_for_verb(request_method)

            #: Dress up the request and response with verb configuration
            if verb_parser_config is not None and verb_parser_config.response_template is not None:

                #: Set the response template and attribute filter
                self.response.template = verb_parser_config.response_template

                response_attr_filter_template = verb_parser_config. \
                    response_attribute_filter_template

                # minification support for response attribute filters
                rewrite_template_model = None
                if self.request.is_minified is True:

                    if isinstance(self.response.template, types.Array):
                        rewrite_template_model = self.response.template.element_template
                    else:
                        rewrite_template_model = self.response.template

                #: Response attribute filter
                self.response.attribute_filter = self.request.get_response_attribute_filter(
                    response_attr_filter_template,
                    rewrite_template_model
                )

                #: If the header is omitted then we ensure the response has a default template
                #: at this point we can assume that we are going to sent down a response
                if self.response.attribute_filter is None:
                    self.response.attribute_filter = response_attr_filter_template

            #: Parameter sets
            if verb_parser_config is not None and len(verb_parser_config.parameter_sets) > 0:

                for parameter_set in verb_parser_config.parameter_sets:

                    if not isinstance(parameter_set, parser.ParameterSet):
                        raise TypeError("%s not a subclass of ParameterSet" % parameter_set.__class__.__name__)

                    try:
                        validated_parameter_set = parameter_set.validate(self.request)

                        if validated_parameter_set is not None:
                            self.request.parameter_set = validated_parameter_set
                            break

                    except exception.DataValidationException as exp:
                        self.logger.error(exp)
                        error_response = ErrorResponse(exp, self.response.selected_serializer)
                        return error_response(environ, start_response)

            # parse body
            if not request_method == VERB.GET and verb_parser_config is not None:
                self.request.attribute_filter = verb_parser_config.request_attribute_filter
                #: Setting this runs the parser for the body
                #: Request will determine which serializer to use based on Content-Type
                self.request.body_template = verb_parser_config.body_template

            #: Warm up
            self.handler_will_run()

            try:
                #:
                #: See if the handler supports the called method
                #: prestans sets a sensible HTTP status code
                #:
                if request_method == VERB.GET:
                    self.get(*self._args, **self._kwargs)
                elif request_method == VERB.HEAD:
                    self.head(*self._args, **self._kwargs)
                elif request_method == VERB.POST:
                    self.post(*self._args, **self._kwargs)
                elif request_method == VERB.PUT:
                    self.put(*self._args, **self._kwargs)
                elif request_method == VERB.PATCH:
                    self.patch(*self._args, **self._kwargs)
                elif request_method == VERB.DELETE:
                    self.delete(*self._args, **self._kwargs)
                elif request_method == VERB.OPTIONS:
                    self.options(*self._args, **self._kwargs)
            except (exception.PermanentRedirect, exception.TemporaryRedirect) as exp:
                self._redirect(exp.url, exp.http_status)
            # re-raise all prestans exceptions
            except exception.Base as exp:
                if isinstance(exception, exception.HandlerException):
                    exp.request = self.request

                raise exp
            # handle any non-prestans exceptions
            except Exception as exp:
                self.handler_raised_exception(exp)
            # always run the tear down method
            finally:
                self.handler_did_run()

            self.logger.info("handler %s.%s; callable execution ends" % (
                self.__module__,
                self.__class__.__name__
            ))

            return self.response(environ, start_response)

        except exception.UnimplementedVerbError as exp:
            self.logger.error(exp)
            error_response = ErrorResponse(exp, self.response.selected_serializer)
            return error_response(environ, start_response)