-
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.
- Loading branch information
Showing
7 changed files
with
302 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from nanodi.nanodi import Depends, inject, shutdown_resources | ||
from nanodi.nanodi import Depends, inject, resource, shutdown_resources | ||
|
||
__all__ = ["Depends", "inject", "shutdown_resources"] | ||
__all__ = ["Depends", "inject", "shutdown_resources", "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
|
||
if TYPE_CHECKING: | ||
from types import TracebackType | ||
|
||
|
||
class Resource: | ||
def init(self) -> Any: | ||
raise NotImplementedError | ||
|
||
def close(self) -> None: | ||
raise NotImplementedError | ||
|
||
def __enter__(self) -> Any: | ||
return self.init() | ||
|
||
def __exit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc: BaseException | None, | ||
traceback: TracebackType | None, | ||
) -> None: | ||
self.close() | ||
|
||
|
||
class AsyncResource: | ||
async def init(self) -> Any: | ||
raise NotImplementedError | ||
|
||
async def close(self) -> None: | ||
raise NotImplementedError | ||
|
||
async def __aenter__(self) -> Any: | ||
return await self.init() | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc: BaseException | None, | ||
traceback: TracebackType | None, | ||
) -> None: | ||
await self.close() |
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,41 @@ | ||
from __future__ import annotations | ||
|
||
from nanodi import Depends, inject | ||
|
||
|
||
def get_redis() -> str: | ||
yield "Redis" | ||
|
||
|
||
@inject | ||
def get_sessions_storage(redis: str = Depends(get_redis)) -> str: | ||
return f"SessionsStorage({redis})" | ||
|
||
|
||
def get_postgres_connection() -> str: | ||
return "Postgres" | ||
|
||
|
||
@inject | ||
def get_db(postgres: str = Depends(get_postgres_connection)) -> str: | ||
return f"{postgres} DB" | ||
|
||
|
||
@inject | ||
def get_users_repository(db: str = Depends(get_db)) -> str: | ||
return f"UsersRepository({db})" | ||
|
||
|
||
@inject | ||
def get_users_service( | ||
users_repository: str = Depends(get_users_repository), | ||
sessions_storage: str = Depends(get_sessions_storage), | ||
) -> str: | ||
return f"UsersService({users_repository}, {sessions_storage})" | ||
|
||
|
||
def test_resolve_complex_service(): | ||
users_service = get_users_service() | ||
|
||
expected = "UsersService(UsersRepository(Postgres DB), SessionsStorage(Redis))" | ||
assert users_service == expected |
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
Oops, something went wrong.