Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/HH-199950' into EXP-84050
Browse files Browse the repository at this point in the history
  • Loading branch information
HH ReleaseBot committed Nov 29, 2023
2 parents bf65cb3 + b1f8ad8 commit fcdcd8d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/dependency_injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ class Page(PageHandler):
self.json.put({'result': session})
```

If you have several dependencies without results, you can put them all to one dependency marker
```python
from frontik.dependency_manager import dependency


async def check_host(handler: PageHandler) -> None:
if handler.request.host != 'example':
raise HttpError()


async def check_session(session=dependency(get_session_dependency)) -> None:
if session.role != 'admin':
raise HttpError()


class Page(PageHandler):
async def get_page(self, _=dependency(check_host, check_session)):
...
```

Dependency can be sync or async functions. When page is executed all ready to run
async dependencies run in parallel with asyncio.gather(). If something finishes the page
(call self.finish() or raise Exception), then we stop executing the remaining dependencies
Expand Down
4 changes: 2 additions & 2 deletions frontik/dependency_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def dependency(*deps: Preprocessor | Callable) -> Any:
"""
add dependency to page_method, it will be run before page_method and provide result
async def get_page(self, session=dep(get_session)):
async def get_page(self, session=dependency(get_session)):
...
"""
if len(deps) == 1:
Expand All @@ -39,7 +39,7 @@ def async_dependencies(async_deps: list[Callable]) -> Callable:
"""
add dependencies that will be run in parallel with page_method
@async_dep([get_session, get_data])
@async_dependencies([get_session, get_data])
async def get_page(self):
...
"""
Expand Down

0 comments on commit fcdcd8d

Please sign in to comment.