예제 #1
0
파일: routing.py 프로젝트: frjonsen/fastapi
    def include_router(
        self,
        router: "APIRouter",
        *,
        prefix: str = "",
        tags: Optional[List[str]] = None,
        dependencies: Optional[Sequence[params.Depends]] = None,
        responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
        default_response_class: Optional[Type[Response]] = None,
    ) -> None:
        if prefix:
            assert prefix.startswith("/"), "A path prefix must start with '/'"
            assert not prefix.endswith(
                "/"
            ), "A path prefix must not end with '/', as the routes will start with '/'"
        else:
            for r in router.routes:
                path = getattr(r, "path")
                name = getattr(r, "name", "unknown")
                if path is not None and not path:
                    raise Exception(
                        f"Prefix and path cannot be both empty (path operation: {name})"
                    )
        if responses is None:
            responses = {}

        for route in router.routes:
            if isinstance(route, APIRoute):

                by_alias = opt_or_default(
                    route.response_model_by_alias, self.response_model_by_alias
                )
                exclude_defaults = opt_or_default(
                    route.response_model_exclude_defaults,
                    self.response_model_exclude_defaults,
                )
                exclude_unset = opt_or_default(
                    route.response_model_exclude_unset,
                    self.response_model_exclude_unset,
                )
                exclude_none = opt_or_default(
                    route.response_model_exclude_none, self.response_model_exclude_none
                )

                combined_responses = {**responses, **route.responses}
                self.add_api_route(
                    prefix + route.path,
                    route.endpoint,
                    response_model=route.response_model,
                    status_code=route.status_code,
                    tags=(route.tags or []) + (tags or []),
                    dependencies=list(dependencies or [])
                    + list(route.dependencies or []),
                    summary=route.summary,
                    description=route.description,
                    response_description=route.response_description,
                    responses=combined_responses,
                    deprecated=route.deprecated,
                    methods=route.methods,
                    operation_id=route.operation_id,
                    response_model_include=route.response_model_include,
                    response_model_exclude=route.response_model_exclude,
                    response_model_by_alias=by_alias,
                    response_model_exclude_unset=exclude_unset,
                    response_model_exclude_defaults=exclude_defaults,
                    response_model_exclude_none=exclude_none,
                    include_in_schema=route.include_in_schema,
                    response_class=route.response_class or default_response_class,
                    name=route.name,
                    route_class_override=type(route),
                    callbacks=route.callbacks,
                )
            elif isinstance(route, routing.Route):
                self.add_route(
                    prefix + route.path,
                    route.endpoint,
                    methods=list(route.methods or []),
                    include_in_schema=route.include_in_schema,
                    name=route.name,
                )
            elif isinstance(route, APIWebSocketRoute):
                self.add_api_websocket_route(
                    prefix + route.path, route.endpoint, name=route.name
                )
            elif isinstance(route, routing.WebSocketRoute):
                self.add_websocket_route(
                    prefix + route.path, route.endpoint, name=route.name
                )
        for handler in router.on_startup:
            self.add_event_handler("startup", handler)
        for handler in router.on_shutdown:
            self.add_event_handler("shutdown", handler)
예제 #2
0
파일: routing.py 프로젝트: frjonsen/fastapi
    def head(
        self,
        path: str,
        *,
        response_model: Optional[Type[Any]] = None,
        status_code: int = 200,
        tags: Optional[List[str]] = None,
        dependencies: Optional[Sequence[params.Depends]] = None,
        summary: Optional[str] = None,
        description: Optional[str] = None,
        response_description: str = "Successful Response",
        responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
        deprecated: Optional[bool] = None,
        operation_id: Optional[str] = None,
        response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
        response_model_exclude: Optional[Union[SetIntStr, DictIntStrAny]] = None,
        response_model_by_alias: Optional[bool] = None,
        response_model_exclude_unset: Optional[bool] = None,
        response_model_exclude_defaults: Optional[bool] = None,
        response_model_exclude_none: Optional[bool] = None,
        include_in_schema: bool = True,
        response_class: Optional[Type[Response]] = None,
        name: Optional[str] = None,
        callbacks: Optional[List[APIRoute]] = None,
    ) -> Callable:

        by_alias = opt_or_default(response_model_by_alias, self.response_model_by_alias)
        exclude_defaults = opt_or_default(
            response_model_exclude_defaults, self.response_model_exclude_defaults,
        )
        exclude_unset = opt_or_default(
            response_model_exclude_unset, self.response_model_exclude_unset,
        )
        exclude_none = opt_or_default(
            response_model_exclude_none, self.response_model_exclude_none
        )

        return self.api_route(
            path=path,
            response_model=response_model,
            status_code=status_code,
            tags=tags or [],
            dependencies=dependencies,
            summary=summary,
            description=description,
            response_description=response_description,
            responses=responses or {},
            deprecated=deprecated,
            methods=["HEAD"],
            operation_id=operation_id,
            response_model_include=response_model_include,
            response_model_exclude=response_model_exclude,
            response_model_by_alias=by_alias,
            response_model_exclude_unset=exclude_unset,
            response_model_exclude_defaults=exclude_defaults,
            response_model_exclude_none=exclude_none,
            include_in_schema=include_in_schema,
            response_class=response_class or self.default_response_class,
            name=name,
            callbacks=callbacks,
        )
예제 #3
0
파일: routing.py 프로젝트: frjonsen/fastapi
    def add_api_route(
        self,
        path: str,
        endpoint: Callable,
        *,
        response_model: Optional[Type[Any]] = None,
        status_code: int = 200,
        tags: Optional[List[str]] = None,
        dependencies: Optional[Sequence[params.Depends]] = None,
        summary: Optional[str] = None,
        description: Optional[str] = None,
        response_description: str = "Successful Response",
        responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
        deprecated: Optional[bool] = None,
        methods: Optional[Union[Set[str], List[str]]] = None,
        operation_id: Optional[str] = None,
        response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
        response_model_exclude: Optional[Union[SetIntStr, DictIntStrAny]] = None,
        response_model_by_alias: Optional[bool] = None,
        response_model_exclude_unset: Optional[bool] = None,
        response_model_exclude_defaults: Optional[bool] = None,
        response_model_exclude_none: Optional[bool] = None,
        include_in_schema: bool = True,
        response_class: Optional[Type[Response]] = None,
        name: Optional[str] = None,
        route_class_override: Optional[Type[APIRoute]] = None,
        callbacks: Optional[List[APIRoute]] = None,
    ) -> None:
        route_class = route_class_override or self.route_class

        by_alias = opt_or_default(response_model_by_alias, self.response_model_by_alias)
        exclude_defaults = opt_or_default(
            response_model_exclude_defaults, self.response_model_exclude_defaults
        )
        exclude_unset = opt_or_default(
            response_model_exclude_unset, self.response_model_exclude_unset
        )
        exclude_none = opt_or_default(
            response_model_exclude_none, self.response_model_exclude_none
        )

        route = route_class(
            path,
            endpoint=endpoint,
            response_model=response_model,
            status_code=status_code,
            tags=tags or [],
            dependencies=dependencies,
            summary=summary,
            description=description,
            response_description=response_description,
            responses=responses or {},
            deprecated=deprecated,
            methods=methods,
            operation_id=operation_id,
            response_model_include=response_model_include,
            response_model_exclude=response_model_exclude,
            response_model_by_alias=by_alias,
            response_model_exclude_unset=exclude_unset,
            response_model_exclude_defaults=exclude_defaults,
            response_model_exclude_none=exclude_none,
            include_in_schema=include_in_schema,
            response_class=response_class or self.default_response_class,
            name=name,
            dependency_overrides_provider=self.dependency_overrides_provider,
            callbacks=callbacks,
        )
        self.routes.append(route)