diff --git a/README.rst b/README.rst index b1722e63..45dae861 100644 --- a/README.rst +++ b/README.rst @@ -68,7 +68,7 @@ Key features of the ``Dependency Injector``: or process pool, etc. Can be used for per-function execution scope in tandem with wiring. See `Resource provider `_. - **Wiring**. Injects dependencies into functions and methods. Helps integrating with - other frameworks: Django, Flask, Aiohttp, etc. + other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc. See `Wiring `_. - **Typing**. Provides typing stubs, ``mypy``-friendly. See `Typing and mypy `_. diff --git a/docs/examples/fastapi.rst b/docs/examples/fastapi.rst new file mode 100644 index 00000000..eaa51b78 --- /dev/null +++ b/docs/examples/fastapi.rst @@ -0,0 +1,79 @@ +.. _fastapi-example: + +FastAPI example +=============== + +.. meta:: + :keywords: Python,Dependency Injection,FastAPI,Example + :description: This example demonstrates a usage of the FastAPI and Dependency Injector. + + +This example shows how to use ``Dependency Injector`` with `FastAPI `_. + +The example application is a REST API that searches for funny GIFs on the `Giphy `_. + +The source code is available on the `Github `_. + +Application structure +--------------------- + +Application has next structure: + +.. code-block:: bash + + ./ + ├── giphynavigator/ + │ ├── __init__.py + │ ├── application.py + │ ├── containers.py + │ ├── endpoints.py + │ ├── giphy.py + │ ├── services.py + │ └── tests.py + ├── config.yml + └── requirements.txt + +Container +--------- + +Declarative container is defined in ``giphynavigator/containers.py``: + +.. literalinclude:: ../../examples/miniapps/fastapi/giphynavigator/containers.py + :language: python + +Endpoints +--------- + +Endpoint has a dependency on search service. There are also some config options that are used as default values. +The dependencies are injected using :ref:`wiring` feature. + +Listing of ``giphynavigator/endpoints.py``: + +.. literalinclude:: ../../examples/miniapps/fastapi/giphynavigator/endpoints.py + :language: python + +Application factory +------------------- +Application factory creates container, wires it with the ``endpoints`` module, creates +``FastAPI`` app, and setup routes. + +Listing of ``giphynavigator/application.py``: + +.. literalinclude:: ../../examples/miniapps/fastapi/giphynavigator/application.py + :language: python + +Tests +----- + +Tests use :ref:`provider-overriding` feature to replace giphy client with a mock ``giphynavigator/tests.py``: + +.. literalinclude:: ../../examples/miniapps/fastapi/giphynavigator/tests.py + :language: python + :emphasize-lines: 29,57,72 + +Sources +------- + +Explore the sources on the `Github `_. + +.. disqus:: diff --git a/docs/examples/index.rst b/docs/examples/index.rst index 16b7fc0a..9b7a1134 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -17,5 +17,6 @@ Explore the examples to see the ``Dependency Injector`` in action. flask aiohttp sanic + fastapi .. disqus:: diff --git a/docs/index.rst b/docs/index.rst index 4527a6af..f3056cf5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -77,7 +77,7 @@ Key features of the ``Dependency Injector``: See :ref:`resource-provider`. - **Containers**. Provides declarative and dynamic containers. See :ref:`containers`. - **Wiring**. Injects dependencies into functions and methods. Helps integrating with - other frameworks: Django, Flask, Aiohttp, etc. See :ref:`wiring`. + other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc. See :ref:`wiring`. - **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`. - **Performance**. Fast. Written in ``Cython``. - **Maturity**. Mature and production-ready. Well-tested, documented and supported. diff --git a/docs/introduction/di_in_python.rst b/docs/introduction/di_in_python.rst index d3e58a10..702056b7 100644 --- a/docs/introduction/di_in_python.rst +++ b/docs/introduction/di_in_python.rst @@ -284,6 +284,7 @@ Choose one of the following as a next step: - :ref:`flask-example` - :ref:`aiohttp-example` - :ref:`sanic-example` + - :ref:`fastapi-example` - Pass the tutorials: - :ref:`flask-tutorial` - :ref:`aiohttp-tutorial` diff --git a/docs/introduction/key_features.rst b/docs/introduction/key_features.rst index e586514a..23aa6822 100644 --- a/docs/introduction/key_features.rst +++ b/docs/introduction/key_features.rst @@ -23,7 +23,7 @@ Key features of the ``Dependency Injector``: See :ref:`resource-provider`. - **Containers**. Provides declarative and dynamic containers. See :ref:`containers`. - **Wiring**. Injects dependencies into functions and methods. Helps integrating with - other frameworks: Django, Flask, Aiohttp, etc. See :ref:`wiring`. + other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc. See :ref:`wiring`. - **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`. - **Performance**. Fast. Written in ``Cython``. - **Maturity**. Mature and production-ready. Well-tested, documented and supported. diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 87259385..2ab56e1d 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,10 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +4.3.9 +----- +- Add ``FastAPI`` example. + 4.3.8 ----- - Add a hotfix to support wiring for ``FastAPI`` endpoints. diff --git a/docs/wiring.rst b/docs/wiring.rst index 412ba770..2508b07f 100644 --- a/docs/wiring.rst +++ b/docs/wiring.rst @@ -201,5 +201,6 @@ Take a look at other application examples: - :ref:`flask-example` - :ref:`aiohttp-example` - :ref:`sanic-example` +- :ref:`fastapi-example` .. disqus:: diff --git a/src/dependency_injector/__init__.py b/src/dependency_injector/__init__.py index 754868e9..06ffecd0 100644 --- a/src/dependency_injector/__init__.py +++ b/src/dependency_injector/__init__.py @@ -1,6 +1,6 @@ """Top-level package.""" -__version__ = '4.3.8' +__version__ = '4.3.9' """Version number. :type: str