-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dependency injections (#569)
* add dependency injections --------- Co-authored-by: Fernando Rodriguez <[email protected]> Co-authored-by: Sanskar Jethi <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ba0bd6c
commit 8887036
Showing
17 changed files
with
569 additions
and
82 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
110 changes: 110 additions & 0 deletions
110
docs_src/src/pages/documentation/api_reference/dependency_injection.mdx
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,110 @@ | ||
export const description = | ||
'On this page, we will learn about dependency injection in Robyn.' | ||
|
||
|
||
## Dependency Injection | ||
|
||
Batman wanted to learn about dependency injection in Robyn. Robyn introduced him to the concept of dependency injection and how it can be used in Robyn. | ||
|
||
|
||
|
||
Robyn has two types of dependency injection: | ||
One is for the application level and the other is for the router level. | ||
|
||
### Application Level Dependency Injection | ||
|
||
<Row> | ||
<Col> | ||
Application level dependency injection is used to inject dependencies into the application. These dependencies are available to all the requests. | ||
</Col> | ||
<Col> | ||
|
||
<CodeGroup title="Request" tag="GET" label="/hello_world"> | ||
|
||
```python {{ title: 'untyped' }} | ||
from robyn import Robyn, ALLOW_CORS | ||
|
||
app = Robyn(__file__) | ||
GLOBAL_DEPENDENCY = "GLOBAL DEPENDENCY" | ||
|
||
app.inject_global(GLOBAL_DEPENDENCY=GLOBAL_DEPENDENCY) | ||
|
||
@app.get("/sync/global_di") | ||
def sync_global_di(request, global_dependencies): | ||
return global_dependencies["GLOBAL_DEPENDENCY"] | ||
``` | ||
|
||
```python {{ title: 'typed' }} | ||
from robyn import Robyn, ALLOW_CORS | ||
|
||
app = Robyn(__file__) | ||
GLOBAL_DEPENDENCY = "GLOBAL DEPENDENCY" | ||
|
||
app.inject_global(GLOBAL_DEPENDENCY=GLOBAL_DEPENDENCY) | ||
|
||
@app.get("/sync/global_di") | ||
def sync_global_di(request, global_dependencies): | ||
return global_dependencies["GLOBAL_DEPENDENCY"] | ||
|
||
``` | ||
</CodeGroup> | ||
</Col> | ||
</Row> | ||
|
||
|
||
### Router Level Dependency Injection | ||
|
||
<Row> | ||
<Col> | ||
Router level dependency injection is used to inject dependencies into the router. These dependencies are available to all the requests of that router. | ||
</Col> | ||
<Col> | ||
|
||
<CodeGroup title="Request" tag="GET" label="/hello_world"> | ||
|
||
```python {{ title: 'untyped' }} | ||
from robyn import Robyn, ALLOW_CORS | ||
|
||
app = Robyn(__file__) | ||
ROUTER_DEPENDENCY = "ROUTER DEPENDENCY" | ||
|
||
app.inject(ROUTER_DEPENDENCY=ROUTER_DEPENDENCY) | ||
|
||
@app.get("/sync/global_di") | ||
def sync_global_di(request, router_dependencies): | ||
return router_dependencies["ROUTER_DEPENDENCY"] | ||
``` | ||
|
||
```python {{ title: 'typed' }} | ||
from robyn import Robyn, ALLOW_CORS | ||
|
||
app = Robyn(__file__) | ||
ROUTER_DEPENDENCY = "ROUTER DEPENDENCY" | ||
|
||
app.inject(ROUTER_DEPENDENCY=ROUTER_DEPENDENCY) | ||
|
||
@app.get("/sync/global_di") | ||
def sync_global_di(request, router_dependencies): | ||
return router_dependencies["ROUTER_DEPENDENCY"] | ||
``` | ||
</CodeGroup> | ||
</Col> | ||
</Row> | ||
|
||
<Row> | ||
<Col> | ||
Note: `router_dependencies`, `global_dependencies` , `request` are named parameters and **must** be named as such. The order of the parameters does not matter. | ||
</Col> | ||
</Row> | ||
|
||
--- | ||
|
||
|
||
## What's next? | ||
|
||
Batman, being the familiar with the dark side wanted to know about Exceptions! | ||
|
||
Robyn introduced him to the concept of exceptions and how he can use them to handle errors in his application. | ||
|
||
- [Exceptions](/documentation/api_reference/exceptions) | ||
|
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from robyn import SubRouter | ||
|
||
di_subrouter = SubRouter(__file__, "/di_subrouter") | ||
GLOBAL_DEPENDENCY = "GLOBAL DEPENDENCY OVERRIDE" | ||
ROUTER_DEPENDENCY = "ROUTER DEPENDENCY" | ||
di_subrouter.inject_global(GLOBAL_DEPENDENCY=GLOBAL_DEPENDENCY) | ||
di_subrouter.inject(ROUTER_DEPENDENCY=ROUTER_DEPENDENCY) | ||
|
||
|
||
@di_subrouter.get("/subrouter_router_di") | ||
def sync_subrouter_route_dependency(router_dependencies): | ||
return router_dependencies["ROUTER_DEPENDENCY"] | ||
|
||
|
||
@di_subrouter.get("/subrouter_global_di") | ||
def sync_subrouter_global_dependency(global_dependencies): | ||
return global_dependencies["GLOBAL_DEPENDENCY"] |
Oops, something went wrong.