fastapi-utils在所有端点之间分享了所有依赖关系

发布于 2025-02-10 00:48:53 字数 1095 浏览 0 评论 0原文

当我使用 fastapapi-utils 时,我遇到了一个问题。

使用此库,您可以完成一个类别,包括一组关联的端点。您还可以在此类中找到您的依赖项作为其属性。 但是问题在于,这些依赖项将在每个端点中使用。

示例:

router = InferringRouter()


@cbv(router)
class SomeAPI:
    session_user: User = Depends(get_session_user)

    @router.get("/get_session_user_id")
    def requires_session_user(self) -> dict[str, str]:
        return {"The id of the session user": self.session_user.id}

    @router.post("/process_parameter")
    def exclude_session_user(self, some_parameter: str) -> dict[str, str]
        # Process 'some_parameter'. Some calculations, etc.
        return {"message": "The item was processed successfully."}
    

'dubl_session_user'将在此端点实际上不需要时需要对用户进行身份验证。

如何从特定端点排除某些依赖关系?是否可以?而且,如果这是不可能的,并且有人贡献了这个图书馆,为什么在文档

I ran into a problem when I used fastapi-utils.

Using this library you get able to make one class including a group of associated endpoints. You also can locate your dependencies in this class as its attributes.
But the problem is this dependencies will be used in each endpoint.

Example:

router = InferringRouter()


@cbv(router)
class SomeAPI:
    session_user: User = Depends(get_session_user)

    @router.get("/get_session_user_id")
    def requires_session_user(self) -> dict[str, str]:
        return {"The id of the session user": self.session_user.id}

    @router.post("/process_parameter")
    def exclude_session_user(self, some_parameter: str) -> dict[str, str]
        # Process 'some_parameter'. Some calculations, etc.
        return {"message": "The item was processed successfully."}
    

The 'exclude_session_user' will require user to be authenticated when this endpoint doesn't actually need this.

How can I exclude some dependencies from a specific endpoint? Is it possible? And if it's impossible, and someone contributed this library, why this problem isn't explained in the documentation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2025-02-17 00:48:53

基本上,您是在询问是否专门设计用于解决问题A的功能,而不再解决问题。解决一个不是为了设计的问题。

换句话说;不,这是不可能的。它旨在使所有端点在该类中捆绑在一起,即所需的依赖项。如果您需要不同的依赖关系组合,则可以:

  • 只需使用普通的fastapi方式,并在每个端点上声明所需的依赖项(或者您的依赖项不返回任何内容,您可以在特定路由器中的所有端点上指定它们,或在您的整个应用程序中!)
  • 继续使用fastapi-util,但创建具有需要不同依赖性组合的端点的不同类。

最后一个注意事项:捆绑对您的API的认证和未经认可的呼叫不明显。错误很容易犯,并且应该对所有人进行身份验证的终点对每个人都开放。

Basically you are asking if a feature, that is specifically designed to solve problem A, to no longer solve problem A but rather problem B. And if that is not possible, you then want someone else to explain in the documentation that this doesn't solve a problem it wasn't designed for.

In other words; no this is not possible. It is designed to give all endpoints bundled in that class, the required dependencies. If you want different combinations of dependencies, then you can either:

  • Just use the normal FastAPI way and declare the dependencies you want on each endpoint (or if your dependencies do not return anything you could specify them on all endpoints in a specific router, or on your entire application!)
  • Keep on using fastapi-utils, but create different classes that hold endpoints that require different combinations of dependencies.

One last note: it is ill-advised to bundle authenticated and non-authenticated calls to your API. Mistakes are easily made and endpoints that should've had authentication suddenly are open for everyone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文