Exemple #1
0
    def add_route(
        self,
        path: str,
        endpoint: typing.Callable,
        methods: typing.List[str] = None,
        name: str = None,
        include_in_schema: bool = True,
        status_code: int = 200,
        response_schema: marshmallow.Schema = None,
        request_schemas: typing.Dict[str, marshmallow.Schema] = None,
    ):
        # If @schemas is used, it has precedence
        # i.e.:
        # @app.route('/', arg1: FooSchema)
        # @app.schemas('/', arg2: BarSchema, response_schema=FooBarSchema)
        # def endpoint(arg1, arg2):
        #       pass
        if getattr(endpoint, '_request_schemas', None):
            merged_schemas = request_schemas.copy()
            merged_schemas.update(endpoint._request_schemas)
            endpoint._request_schemas = merged_schemas
        else:
            endpoint._request_schemas = request_schemas

        if getattr(endpoint, '_response_schema', None) is None:
            endpoint._response_schema = response_schema

        self.routes.append(
            Route(path,
                  endpoint=endpoint,
                  methods=methods,
                  name=name,
                  include_in_schema=include_in_schema,
                  router=self,
                  status_code=status_code))