fastapi-utils在所有端点之间分享了所有依赖关系
当我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您是在询问是否专门设计用于解决问题A的功能,而不再解决问题。解决一个不是为了设计的问题。
换句话说;不,这是不可能的。它旨在使所有端点在该类中捆绑在一起,即所需的依赖项。如果您需要不同的依赖关系组合,则可以:
最后一个注意事项:捆绑对您的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:
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.