-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
152 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Usage with `Fastapi`: | ||
|
||
```python | ||
import datetime | ||
import contextlib | ||
import typing | ||
|
||
import fastapi | ||
from starlette import status | ||
from starlette.testclient import TestClient | ||
|
||
from tests import container | ||
|
||
|
||
@contextlib.asynccontextmanager | ||
async def lifespan_manager(_: fastapi.FastAPI) -> typing.AsyncIterator[None]: | ||
try: | ||
yield | ||
finally: | ||
await container.DIContainer.tear_down() | ||
|
||
|
||
app = fastapi.FastAPI(lifespan=lifespan_manager) | ||
|
||
|
||
@app.get("/") | ||
async def read_root( | ||
some_dependency: typing.Annotated[ | ||
container.DependentFactory, | ||
fastapi.Depends(container.DIContainer.dependent_factory), | ||
], | ||
) -> datetime.datetime: | ||
return some_dependency.async_resource | ||
|
||
|
||
client = TestClient(app) | ||
|
||
response = client.get("/") | ||
assert response.status_code == status.HTTP_200_OK | ||
assert response.json() == "async resource" | ||
|
||
``` |
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,55 @@ | ||
# IOC-container with dependencies | ||
|
||
```python | ||
import dataclasses | ||
import logging | ||
import typing | ||
|
||
from that_depends import BaseContainer, providers | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_sync_resource() -> typing.Iterator[str]: | ||
logger.debug("Resource initiated") | ||
try: | ||
yield "sync resource" | ||
finally: | ||
logger.debug("Resource destructed") | ||
|
||
|
||
async def create_async_resource() -> typing.AsyncIterator[str]: | ||
logger.debug("Async resource initiated") | ||
try: | ||
yield "async resource" | ||
finally: | ||
logger.debug("Async resource destructed") | ||
|
||
|
||
@dataclasses.dataclass(kw_only=True, slots=True) | ||
class IndependentFactory: | ||
dep1: str | ||
dep2: int | ||
|
||
|
||
@dataclasses.dataclass(kw_only=True, slots=True) | ||
class DependentFactory: | ||
independent_factory: IndependentFactory | ||
sync_resource: str | ||
async_resource: str | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
sync_resource = providers.Resource(create_sync_resource) | ||
async_resource = providers.AsyncResource(create_async_resource) | ||
|
||
independent_factory = providers.Factory(IndependentFactory, dep1="text", dep2=123) | ||
dependent_factory = providers.Factory( | ||
DependentFactory, | ||
independent_factory=independent_factory, | ||
sync_resource=sync_resource, | ||
async_resource=async_resource, | ||
) | ||
|
||
``` |
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,40 @@ | ||
# Usage with `Litestar`: | ||
|
||
```python | ||
import typing | ||
import fastapi | ||
import contextlib | ||
from litestar import Litestar, get | ||
from litestar.di import Provide | ||
from litestar.status_codes import HTTP_200_OK | ||
from litestar.testing import TestClient | ||
|
||
from tests import container | ||
|
||
|
||
@get("/") | ||
async def index(injected: str) -> str: | ||
return injected | ||
|
||
|
||
@contextlib.asynccontextmanager | ||
async def lifespan_manager(_: fastapi.FastAPI) -> typing.AsyncIterator[None]: | ||
try: | ||
yield | ||
finally: | ||
await container.DIContainer.tear_down() | ||
|
||
|
||
app = Litestar( | ||
route_handlers=[index], | ||
dependencies={"injected": Provide(container.DIContainer.async_resource)}, | ||
lifespan=[lifespan_manager], | ||
) | ||
|
||
|
||
def test_litestar_di() -> None: | ||
with (TestClient(app=app) as client): | ||
response = client.get("/") | ||
assert response.status_code == HTTP_200_OK, response.text | ||
assert response.text == "async resource" | ||
``` |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
### Factory | ||
# Factory | ||
- Initialized on every call. | ||
- Class or simple function is allowed. | ||
```python | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
### List | ||
# List | ||
- List provider contains other providers. | ||
- Resolves into list of dependencies. | ||
|
||
|
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