Allow to create request scopes outside of the middleware #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I started to implement something very similar to this project on my own, but was very happy to find this nice little library, which almost does all the stuff I need in order to use
injector
in my FastAPI application. So thanks for the work first of all!However, it's lacking one particular feature: It would be great if there's a possibility to create request scopes also outside of the middleware. Background of this is that in my application I need to process messages I get from a message queue. Same as for requests it would be nice if it's possible to scope the lifetime of services to the handling of a single message. I could imagine there are more, similar cases where this might be helpful, for example background tasks (i.e. celery) or cronjobs. For sure I could write my own scope for this, but then I would more or less need to duplicate all of your work, so I thought it would make more sense to integrate it directly into your library.
To achieve this, I moved the logic of assigning request ids from the
InjectorMiddleware
to a new class calledRequestScopeFactory
, which allows create request scopes also outside the middleware. This could then be used for example like:What do you think about this?