Example #1
0
    def wrapped(*args, **kwargs):
        if authenticators:
            first_error = None
            for authenticator in authenticators:
                try:
                    authenticator.authenticate()
                    break  # Short-circuit on first successful authentication
                except (errors.Unauthorized, errors.Forbidden) as e:
                    first_error = first_error or e
            else:
                raise first_error or errors.Unauthorized

        if query_string_schema:
            g.validated_args = get_query_string_params_or_400(
                schema=query_string_schema)

        if request_body_schema:
            g.validated_body = get_json_body_params_or_400(
                schema=request_body_schema)

        if headers_schema:
            g.validated_headers = get_header_params_or_400(
                schema=headers_schema)

        rv = f(*args, **kwargs)

        if not response_body_schema:
            return rv

        if isinstance(rv, current_app.response_class):
            schema = response_body_schema[rv.status_code]
            # The schema may be set to None to bypass marshaling (e.g. for 204 responses).
            if schema is None:
                return rv
            # Otherwise, ensure the response body conforms to the promised schema.
            schema.loads(rv.data)  # May raise ValidationError.
            return rv

        data, status_code, headers = _unpack_view_func_return_value(rv)
        schema = response_body_schema[status_code]  # May raise KeyError.

        # The schema may be set to None to bypass marshaling (e.g. for 204 responses).
        if schema is None:
            return response(data=data,
                            status_code=status_code,
                            headers=headers,
                            mimetype=mimetype)

        marshaled = marshal(data=data, schema=schema)
        return response(data=marshaled,
                        status_code=status_code,
                        headers=headers,
                        mimetype=mimetype)
Example #2
0
    def wrapped(*args, **kwargs):
        if authenticators:
            first_error = None
            for authenticator in authenticators:
                try:
                    authenticator.authenticate()
                    break  # Short-circuit on first successful authentication
                except errors.Unauthorized as e:
                    first_error = first_error or e
            else:
                raise first_error or errors.Unauthorized

        if query_string_schema:
            g.validated_args = get_query_string_params_or_400(
                schema=query_string_schema)

        if request_body_schema:
            g.validated_body = get_json_body_params_or_400(
                schema=request_body_schema)

        if headers_schema:
            g.validated_headers = get_header_params_or_400(
                schema=headers_schema)

        rv = f(*args, **kwargs)

        if not response_body_schema:
            return rv

        data, status_code, headers = _unpack_view_func_return_value(rv)

        schema = response_body_schema[status_code]  # May raise KeyError.

        # The schema may be declared as None to bypass marshaling (e.g. for 204 responses).
        if schema is None:
            return response(data=data,
                            status_code=status_code,
                            headers=headers,
                            mimetype=mimetype)

        marshaled = marshal(data=data, schema=schema)
        return response(data=marshaled,
                        status_code=status_code,
                        headers=headers,
                        mimetype=mimetype)
Example #3
0
    def wrapped(*args, **kwargs):
        if authenticator:
            authenticator.authenticate()

        if query_string_schema:
            g.validated_args = get_query_string_params_or_400(
                schema=query_string_schema)

        if request_body_schema:
            g.validated_body = get_json_body_params_or_400(
                schema=request_body_schema)

        if headers_schema:
            g.validated_headers = get_header_params_or_400(
                schema=headers_schema)

        rv = f(*args, **kwargs)

        if not response_body_schema:
            return rv

        data, status_code, headers = _unpack_view_func_return_value(rv)

        schema = response_body_schema[status_code]  # May raise KeyError.

        # The schema may be declared as None to bypass marshaling (e.g. for 204 responses).
        if schema is None:
            return response(data=data,
                            status_code=status_code,
                            headers=headers,
                            mimetype=mimetype)

        marshaled = marshal(data=data, schema=schema)
        return response(data=marshaled,
                        status_code=status_code,
                        headers=headers,
                        mimetype=mimetype)