Beispiel #1
0
    def _fallback_view(request):
        # Maybe we failed to match any definitions for the request method?
        if request.method not in service.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = service.defined_methods
            raise response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        for method, _, args in service.definitions:
            if method != request.method:
                continue
            if 'accept' in args:
                acceptable.extend(
                    service.get_acceptable(method, filter_callables=True))
                if 'acceptable' in request.info:
                    for content_type in request.info['acceptable']:
                        if content_type not in acceptable:
                            acceptable.append(content_type)

                # Now check if that was actually the source of the problem.
                if not request.accept.best_match(acceptable):
                    response = HTTPNotAcceptable()
                    response.content_type = "application/json"
                    response.body = json.dumps(acceptable)
                    raise response

        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(service.name)
Beispiel #2
0
    def _fallback_view(request):
        # Maybe we failed to match any definitions for the request method?
        if request.method not in service.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = service.defined_methods
            return response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        for method, _, args in service.definitions:
            if method != request.method:
                continue
            if 'accept' in args:
                acceptable.extend(
                        service.get_acceptable(method, filter_callables=True))
                if 'acceptable' in request.info:
                    for content_type in request.info['acceptable']:
                        if content_type not in acceptable:
                            acceptable.append(content_type)

                # Now check if that was actually the source of the problem.
                if not request.accept.best_match(acceptable):
                    response = HTTPNotAcceptable()
                    response.content_type = "application/json"
                    response.body = json.dumps(acceptable)
                    return response

        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(service.name)
Beispiel #3
0
    def _fallback_view(self, request):
        """Fallback view for this service, called when nothing else matches.

        This method provides the view logic to be executed when the request
        does not match any explicitly-defined view.  Its main responsibility
        is to produce an accurate error response, such as HTTPMethodNotAllowed
        or HTTPNotAcceptable.
        """
        # Maybe we failed to match any definitions for the request method?
        if request.method not in self.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = self.defined_methods
            return response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        for api_kwargs in self.definitions:
            if api_kwargs['request_method'] != request.method:
                continue
            if 'accept' in api_kwargs:
                accept = to_list(api_kwargs.get('accept'))
                acceptable.extend(a for a in accept if not callable(a))
                if 'acceptable' in request.info:
                    for content_type in request.info['acceptable']:
                        if content_type not in acceptable:
                            acceptable.append(content_type)
        # Now check if that was actually the source of the problem.
        if not request.accept.best_match(acceptable):
            response = HTTPNotAcceptable()
            response.content_type = "application/json"
            response.body = json.dumps(acceptable)
            return response
        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(self.name)
Beispiel #4
0
    def _fallback_view(self, request):
        """Fallback view for this service, called when nothing else matches.

        This method provides the view logic to be executed when the request
        does not match any explicitly-defined view.  Its main responsibility
        is to produce an accurate error response, such as HTTPMethodNotAllowed
        or HTTPNotAcceptable.
        """
        # Maybe we failed to match any definitions for the request method?
        if request.method not in self.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = self.defined_methods
            return response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        for api_kwargs in self.definitions:
            if api_kwargs["request_method"] != request.method:
                continue
            if "accept" in api_kwargs:
                accept = to_list(api_kwargs.get("accept"))
                acceptable.extend(a for a in accept if not callable(a))
                if "acceptable" in request.info:
                    for content_type in request.info["acceptable"]:
                        if content_type not in acceptable:
                            acceptable.append(content_type)
        # Now check if that was actually the source of the problem.
        if not request.accept.best_match(acceptable):
            response = HTTPNotAcceptable()
            response.content_type = "application/json"
            response.body = json.dumps(acceptable)
            return response
        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(self.name)