mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
ae3024588c
* Bump version to 4.3.9: FastAPI example * Reengineer wiring * Add @inject decorator * Add .workspace dir to gitignore * Add generic typing for @inject * Add type cast for @inject * Update movie lister example * Update cli application tutorial * Update demo example * Update wiring docs and examples * Update aiohttp example and tutorial * Update multiple containers example * Update single container example * Update decoupled packages example * Update django example * Update asyncio daemon example and tutorial * Update FastAPI example * Update flask example and tutorial * Update sanic example * Add wiring registry * Add new line to .gitignore * Add @inject to the test samples * Fix flake8 errors
80 lines
2.2 KiB
ReStructuredText
80 lines
2.2 KiB
ReStructuredText
.. _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 <https://fastapi.tiangolo.com/>`_.
|
|
|
|
The example application is a REST API that searches for funny GIFs on the `Giphy <https://giphy.com/>`_.
|
|
|
|
The source code is available on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/fastapi>`_.
|
|
|
|
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 <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/fastapi>`_.
|
|
|
|
.. disqus::
|