-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
works tests for litestar (injection and overriding) (#15)
* works tests for litestar (injection and overriding) * upd docs and tests
- Loading branch information
1 parent
1bf61fe
commit 5dd23fc
Showing
5 changed files
with
111 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Litestar | ||
|
||
By now you should have a container with providers. | ||
Now all you have to do is define request handlers according to the following rules: | ||
1. use **@inject** decorator **before** http-method Litestar decorator; | ||
|
||
2. added for each injected parameter to the request handler a typing of the form | ||
`Union[<your type>, Any]` for Python versions below 3.10 or `<your type> | Any`, | ||
as well as the `Provide` marker from the `injection` package indicating the provider | ||
|
||
## Example | ||
|
||
```python3 | ||
from typing import Any, Union | ||
|
||
from litestar import Litestar, get | ||
from injection import Provide, inject, DeclarativeContainer, providers | ||
|
||
|
||
class Redis: | ||
def __init__(self, *, url: str, port: int): | ||
self.uri = url + ":" + str(port) | ||
self.url = url | ||
self.port = port | ||
|
||
def get(self, key): | ||
return key | ||
|
||
|
||
class Container(DeclarativeContainer): | ||
redis = providers.Singleton( | ||
Redis, | ||
port=9873, | ||
url="redis://...", | ||
) | ||
num = providers.Object(9402) | ||
|
||
|
||
@get( | ||
"/some_resource", | ||
status_code=200, | ||
) | ||
@inject | ||
async def litestar_endpoint( | ||
redis: Union[Redis, Any] = Provide(Container.redis), | ||
num: Union[int, Any] = Provide(Container.num), | ||
) -> dict: | ||
value = redis.get(800) | ||
return {"detail": value, "num2": num} | ||
|
||
|
||
@get( | ||
"/num_endpoint", | ||
status_code=200, | ||
) | ||
@inject | ||
async def litestar_endpoint_object_provider( | ||
num: Union[int, Any] = Provide(Container.num), | ||
) -> dict: | ||
return {"detail": num} | ||
|
||
|
||
_handlers = [ | ||
litestar_endpoint, | ||
litestar_endpoint_object_provider, | ||
] | ||
|
||
app = Litestar(route_handlers=_handlers) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters