-
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
1 parent
4e72ba0
commit ee2204e
Showing
5 changed files
with
164 additions
and
1 deletion.
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,4 @@ | ||
### New features | ||
|
||
- Demonstrate `SlackException` in the `POST /fastapi-bootcamp/error-demo` endpoint. | ||
- Demonstrate custom FastAPI dependencies in the `GET /fastapi-bootcamp/dependency-demo` endpoint. |
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
68 changes: 68 additions & 0 deletions
68
src/fastapibootcamp/dependencies/demostatefuldependency.py
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,68 @@ | ||
"""A class-based FastAPI dependency for demonstration purposes.""" | ||
|
||
from __future__ import annotations | ||
|
||
import random | ||
|
||
from ..exceptions import DemoInternalError | ||
|
||
__all__ = ["example_stateful_dependency", "ExampleStatefulDependency"] | ||
|
||
|
||
class ExampleStatefulDependency: | ||
"""A stateful FastAPI dependency for demonstration purposes.""" | ||
|
||
def __init__(self) -> None: | ||
# For this demo we're just using a semi-random string as the state. In | ||
# real applications, this could be a database connection, a client | ||
# to a remote service, etc. This "state" is reused over the life of | ||
# this application instance. It's not shared between instances, though, | ||
self._state: str | None = None | ||
|
||
async def init(self) -> None: | ||
"""Initialize the dependency.""" | ||
# This initialization is called in main.py in the lifespan context | ||
self._state = f"{random.choice(ADJECTIVES)} {random.choice(ANIMALS)}" | ||
|
||
async def __call__(self) -> str: | ||
"""Provide the dependency. | ||
This gets called by the fastapi Depends() function when your | ||
path operation function is called. | ||
""" | ||
if self._state is None: | ||
raise DemoInternalError( | ||
"ExamplePersistentDependency not initialized" | ||
) | ||
|
||
return self._state | ||
|
||
async def aclose(self) -> None: | ||
"""Close the dependency.""" | ||
self.state = None | ||
|
||
|
||
# This is the instance of the dependency that's referenced in path operation | ||
# functions with the fastapi.Depends() function. Note that it needs to be | ||
# initialized before it can be used. This is done in the lifespan context | ||
# manager in main.py. Another option is to initialize it on the first use. | ||
example_stateful_dependency = ExampleStatefulDependency() | ||
|
||
|
||
ADJECTIVES = [ | ||
"speedy", | ||
"ponderous", | ||
"furious", | ||
"careful", | ||
"mammoth", | ||
"crafty", | ||
] | ||
|
||
ANIMALS = [ | ||
"cat", | ||
"dog", | ||
"sloth", | ||
"snail", | ||
"rabbit", | ||
"turtle", | ||
] |
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