Exemple #1
0
    def set_default_headers_schema(self, headers_schema):
        """
        Sets the schema to be used by default to validate incoming headers.

        :param marshmallow.Schema headers_schema:
        """
        self.default_headers_schema = normalize_schema(headers_schema)
 def setUp(self):
     super(RequireOutputMixinTest, self).setUp()
     self.schema = normalize_schema(ActuallyRequireOnDumpMixinSchema)
     self.data = {
         "value_required": "abc",
         "value_optional": None,
         "validation_required": datetime.now(),
         "one_of_validation": "a",
     }
Exemple #3
0
    def add_handler(
        self,
        func,
        rule,
        method="GET",
        endpoint=None,
        response_body_schema=None,
        query_string_schema=None,
        request_body_schema=None,
        headers_schema=USE_DEFAULT,
        authenticators=USE_DEFAULT,
        tags=None,
        mimetype=USE_DEFAULT,
        hidden=False,
    ):
        """
        Registers a function as a request handler.

        :param func:
            The Flask "view_func"
        :param str rule:
            The Flask "rule"
        :param str method:
            The HTTP method this handler accepts
        :param str endpoint:
        :param dict[int, marshmallow.Schema] response_body_schema:
            Dictionary mapping response codes to schemas to use to marshal
            the response. For now this assumes everything is JSON.
        :param marshmallow.Schema query_string_schema:
            Schema to use to deserialize query string arguments.
        :param marshmallow.Schema request_body_schema:
            Schema to use to deserialize the request body. For now this
            assumes everything is JSON.
        :param Type[USE_DEFAULT]|None|marshmallow.Schema headers_schema:
            Schema to use to grab and validate headers.
        :param Type[USE_DEFAULT]|None|List(Authenticator)|Authenticator authenticators:
            A list of authenticator objects to authenticate incoming requests.
            If left as USE_DEFAULT, the Rebar's default will be used.
            Set to None to make this an unauthenticated handler.
        :param Sequence[str] tags:
            Arbitrary strings to tag the handler with. These will translate to Swagger operation tags.
        :param Type[USE_DEFAULT]|None|str mimetype:
            Content-Type header to add to the response schema
        :param bool hidden:
            if hidden, documentation is not created for this request handler by default
        """
        # Fix #115: if we were passed bare classes we'll go ahead and instantiate
        headers_schema = normalize_schema(headers_schema)
        request_body_schema = normalize_schema(request_body_schema)
        query_string_schema = normalize_schema(query_string_schema)
        if response_body_schema:
            # Ensure we wrap in appropriate default (200) dict if we were passed a single Schema or class:
            if not isinstance(response_body_schema, Mapping):
                response_body_schema = {200: response_body_schema}
            # use normalize_schema to convert any class reference(s) to instantiated schema(s):
            response_body_schema = {
                code: normalize_schema(schema)
                for (code, schema) in response_body_schema.items()
            }

        # authenticators can be a list of Authenticators, a single Authenticator, USE_DEFAULT, or None
        if isinstance(authenticators,
                      Authenticator) or authenticators is USE_DEFAULT:
            authenticators = [authenticators]
        elif authenticators is None:
            authenticators = []

        self._paths[rule][method] = PathDefinition(
            func=func,
            path=rule,
            method=method,
            endpoint=endpoint,
            response_body_schema=response_body_schema,
            query_string_schema=query_string_schema,
            request_body_schema=request_body_schema,
            headers_schema=headers_schema,
            authenticators=authenticators,
            tags=tags,
            mimetype=mimetype,
            hidden=hidden,
        )