[QUESTION] Middleware and dependencies
See original GitHub issueDescription
I’m wondering how middlewares and dependencies can work together, if at all.
That is, I’d like to exploit dependencies(and dependency caching) in my middleware, but I’m not sure that’s supported right now?
Basic idea is:
- my middleware declares, or otherwise obtain the dependency. The dependency is cached(if configured so in the declaration).
- A route also declares the same dependency. The dependency was cached by the middleware, so the same value is provided.
For example:
Session = sqlalchemy.orm.sessionmaker()
def get_database():
return sqlalchemy.create_engine(os.getenv("DATABASE_URL"))
@app.middleware("http")
async def get_org_repo(request: Request, call_next, database = Depends(get_database)):
try:
session = Session(bind=database)
request.state.session = session
response = await call_next(request)
finally:
# cleanup
session.close()
return response
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:9 (5 by maintainers)
Top Results From Across the Web
design patterns - Middleware dependencies
Questions : What's the best way to share data between middlewares? Monkey-patching a response object seems very fragile. How can I make it...
Read more >fastapi dependency vs middleware - python - Stack Overflow
The middleware can be seen as a superset of a Dependency, as the latter is a sort of middleware that returns a value...
Read more >What is Middleware? - IBM
Robotics middleware simplifies the process of integrating robotic hardware, firmware and software from multiple manufacturers and locations.
Read more >ASP.NET Core Top 20 Most Important Interview Questions
What is middleware? ... It is software that is injected into the application pipeline to handle requests and responses. They are just like...
Read more >Don't Get Dragged into the Black Hole of Middleware Complexity
When applications connect through middleware, as many do, the task becomes nearly impossible. This is because in addition to direct dependencies ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is it possible to have access to db dependency when using starlette AuthenticationMiddleware? The BasicAuthBackend requires a db dependency. https://www.starlette.io/authentication/
@DrPyser Below, I’ve included a stab at an approach that could implement dependencies for "MiddlewareResource"s. The main feature loss is arbitrary dependency functions; to get around this I’ve just required that any Depends of a MiddlewareResource is itself a MiddlewareResource.
Before including the implementation, here is an example showing the API:
Subclasses of
MiddlewareResource
behave like singletons that are scoped to a resource instance – callingResource(request)
multiple times on the same request returns the same instance.This allows you to then have “traditional” FastAPI dependencies that look like this:
In order to use
MiddlewareResource
instances, you need to add the following middleware:Here’s the implementation; it probably could benefit from a little additional tweaking, so use in its current state at your own peril 😬: