简体   繁体   English

中间件影响 API 上的所有请求

[英]Middleware affect all requests on an API

I'm working with fastapi and suppose I have registered 2 middlewares called A and B (for example, they can add to each request the connection to a database) .我正在使用 fastapi 并假设我已经注册了 2 个称为 A 和 B 的中间件(例如,它们可以向每个请求添加到数据库的连接)。 Suppose also that I have 2 endpoints, and the first one only need A middleware and the second one only need B middleware.还假设我有 2 个端点,第一个只需要 A 中间件,第二个只需要 B 中间件。 I used dependencies to establish the needs of each endpoint .我使用依赖关系来确定每个端点的需求。 When I use the first endpoint, the request is equiped with the result of both middlewares A and B, I want to restrict the effect of middlewares only when they are needed, I thought that was done using dependencies.当我使用第一个端点时,请求配备了中间件A和B的结果,我想只在需要时限制中间件的效果,我认为这是使用依赖关系完成的。 How can I do that?.我怎样才能做到这一点?。 Thank you!!谢谢!!

如果我正确理解您的问题,该实践似乎要么在您的中间件中创建自定义逻辑(例如检查调用了哪个路径/路由),要么使用文档中描述的声明自定义 Request/APIRoute 对象的高级功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM