-
-
Notifications
You must be signed in to change notification settings - Fork 968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experiment a high-level HTTPMiddleware #1691
Conversation
Just curious, did you look at my branch (linked in #1678 (comment))? I ask because if you did not it seems we arrived at the exact same solution independently, which is a good sign. |
@adriangb Woah, I did not look at that branch — a very good sign indeed! |
Agreed. It seems like we even did the same thing with
I think we should just make it an async generator and always an async generator.
There is one alternative: if response = yield
yield Response() We can support this by checking if the generator stops or is still available. Another issue to think about: do we want to allow (safely) reading the request body?
|
dc0bdb9
to
9084939
Compare
a0aa64a
to
91fbe42
Compare
91fbe42
to
ce67633
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aclosing
is only a couple LOC, we can just vendor it
6342425
to
c49c6b9
Compare
c49c6b9
to
cf16303
Compare
@adriangb As for bodies -- I'm tempted we explicitly don't allow a helper |
Yeah I think that's a fair position to have. I think it can be done without issues (ref adriangb@0bf1f3f). But we can always add that feature later, it wouldn't be breaking this API. |
I switched things so that |
b70f13d
to
9090aa7
Compare
9090aa7
to
3b8e091
Compare
This looks great! FWIW, I support not allowing accessing or modifying request or response body. It's a fair limitation, in my opinion. |
This is looking great. I think the last issue we have to carry over from #1694 is what to do if a user tries to set |
a18bb82
to
b325d2c
Compare
@adriangb I made an attempt at controlling what attributes can be read/written in the context of the middleware. It works, but I'm far from satisfied by it. Overall that seems overly complex and defensive... The core problem is, Starlette's Maybe, for now (? how 'temporary' would that actually be?), we should do |
I think your attempt is quite reasonable. I share your same concerns, but as a counter point this API (a I also don't particularly like the tuple response, especially as something temporary (changing it would be a breaking change). |
What should we do with this PR? |
The conclusion from discussion in the meeting was that it's probably best to just push users towards writing pure ASGI middleware or solutions provided by higher-level packages like FastAPI. The logic behind this decision is that users generally are not writing one-off middleware and any sort of reusable middleware component can take the time to be implemented as a pure ASGI middleware. So I think we should close this PR? Florimond, what do you think? Maybe this can be a 3rd party package? |
Closing it just to organize a bit the PRs. Feel free to reopen if you think it's still relevant. 🙏 |
Refs #1678, cc @adriangb
I experimented with what a helper middleware to do some simple request/response peeks and edits might look like, and yet be pure ASGI -- no Starlette dependencies at all, accept any ASGI app as a
parent
.I was inspired by what we did for HTTPX auth flows. Turns out we can use the same generator-based API to provide a somewhat clean interface that goes like this:
The structure of this API does NOT allow:
scope
only (request method, URL, headers, etc), so we haven't got any access toreceive()
.I copy-pasted the
BaseHTTPMiddleware
tests, switching toHTTPMiddleware
, and got everything passing.I also looked up some example usage on https://grep.app and found this fairly complex usage from Netflix's Dispatch project. Here's the diff showing how they might upgrade:
Some missing pieces might be:
Response
class, but we might want to use a specific 'stub' that clearly limits what's allowed within the "on-response-headers" part of the middleware. E.g. it should be clear that it's not possible to patchresponse.body
.