litestar
litestar copied to clipboard
Enhancement: Add Recipes section to Docs
This is a suggetion- we could add a cache reserved kwarg, which will cause starlite to inject the cache backend to route handlers:
from starlite import Cache, get
get("/")
async def handler(cache: Cache) -> dict | None:
return await cache.get(...)
This is merely an ergonomics improvement, because users can access the cache already by going the somewhat longer route of doing this:
from starlite import Request
get("/")
async def handler(request: Request) -> dict | None:
return await request.app.cache.get(...)
Given that it is pretty easy for a user to write a small convenience dependency to do the same, e.g.,:
@get("/")
def using_the_cache(cache: Cache) -> ...:
...
def provide_cache(request: Request) -> Cache:
return request.app.cache
app = Starlite([using_the_cache], dependencies={"cache": Provide(provide_cache)})
Add to that it would be breaking for anyone already using cache as a parameter name in a signature and I'm not sure it is worth the churn.
Maybe something we could consider is curating a collection of dependencies that users could use if they want - basically would save them the hassle of having to write the dependency function for common things like this. E.g., from starlite.dependencies.lib import provide_cache. It could be a good middle ground. Thoughts?
That's a very good idea!
What utils and dependencies do you have in mind?
Idea purely inspired by this issue, so I don't really have any thoughts beyond that, but I'm sure there would be a few different things that can be accessed through the request via a couple of hops like the cache..
What about a structured log instance that is pre-bound to some app/request context?
We could always start with recipes in the docs to see if there's enough interest to warrant the work of maintaining them.
Ok. But we still need to inventory what we want to add - even as recipes
Shall we make a new issue, or hijack this one?
Shall we make a new issue, or hijack this one?
Hijack
@peterschutt so what should we include?
Hello, thanks for the awesome framework.
If you're looking for ideas for recipes, I'm right now looking at overriding dependencies when writing tests. I'm trying it now, I can contribute to that if you all decided it's a good recipe.
Hello, thanks for the awesome framework.
If you're looking for ideas for recipes, I'm right now looking at overriding dependencies when writing tests. I'm trying it now, I can contribute to that if you all decided it's a good recipe.
You're welcome 😉
Sure, we are keen on PRs