Refactor Flask tutorial: Connect to the GitHub

This commit is contained in:
Roman Mogylatov 2020-10-08 12:39:59 -04:00
parent b5eaf1f558
commit f1c0f9ce3f

View File

@ -439,7 +439,7 @@ and run in the terminal:
.. code-block:: bash .. code-block:: bash
pip install --upgrade -r requirements.txt pip install -r requirements.txt
Now we need to add Github API client the container. We will need to add two more providers from Now we need to add Github API client the container. We will need to add two more providers from
the ``dependency_injector.providers`` module: the ``dependency_injector.providers`` module:
@ -448,30 +448,18 @@ the ``dependency_injector.providers`` module:
- ``Configuration`` provider that will be used for providing the API token and the request timeout - ``Configuration`` provider that will be used for providing the API token and the request timeout
for the ``Github`` client. for the ``Github`` client.
Let's do it.
Edit ``containers.py``: Edit ``containers.py``:
.. code-block:: python .. code-block:: python
:emphasize-lines: 3,7,19,21-25 :emphasize-lines: 3-4,9,11-15
"""Application containers module.""" """Containers module."""
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.ext import flask
from flask import Flask
from flask_bootstrap import Bootstrap
from github import Github from github import Github
from . import views
class Container(containers.DeclarativeContainer):
class ApplicationContainer(containers.DeclarativeContainer):
"""Application container."""
app = flask.Application(Flask, __name__)
bootstrap = flask.Extension(Bootstrap)
config = providers.Configuration() config = providers.Configuration()
@ -481,8 +469,6 @@ Edit ``containers.py``:
timeout=config.github.request_timeout, timeout=config.github.request_timeout,
) )
index_view = flask.View(views.index)
.. note:: .. note::
We have used the configuration value before it was defined. That's the principle how We have used the configuration value before it was defined. That's the principle how
@ -490,11 +476,16 @@ Edit ``containers.py``:
Use first, define later. Use first, define later.
.. note::
Don't forget to remove the Ellipsis ``...`` from the container. We don't need it anymore
since we container is not empty.
Now let's add the configuration file. Now let's add the configuration file.
We will use YAML. We will use YAML.
Create an empty file ``config.yml`` in the root root of the project: Create an empty file ``config.yml`` in the root of the project:
.. code-block:: bash .. code-block:: bash
:emphasize-lines: 11 :emphasize-lines: 11
@ -537,7 +528,7 @@ and install it:
.. code-block:: bash .. code-block:: bash
pip install --upgrade -r requirements.txt pip install -r requirements.txt
We will use environment variable ``GITHUB_TOKEN`` to provide the API token. We will use environment variable ``GITHUB_TOKEN`` to provide the API token.
@ -549,27 +540,29 @@ Now we need to edit ``create_app()`` to make two things when application starts:
Edit ``application.py``: Edit ``application.py``:
.. code-block:: python .. code-block:: python
:emphasize-lines: 9-10 :emphasize-lines: 12-13
"""Application module.""" """Application module."""
from .containers import ApplicationContainer from flask import Flask
from flask_bootstrap import Bootstrap
from .containers import Container
from . import views
def create_app(): def create_app() -> Flask:
"""Create and return Flask application.""" container = Container()
container = ApplicationContainer()
container.config.from_yaml('config.yml') container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN') container.config.github.auth_token.from_env('GITHUB_TOKEN')
app = container.app() app = Flask(__name__)
app.container = container app.container = container
app.add_url_rule('/', 'index', views.index)
bootstrap = container.bootstrap() bootstrap = Bootstrap()
bootstrap.init_app(app) bootstrap.init_app(app)
app.add_url_rule('/', view_func=container.index_view.as_view())
return app return app
Now we need create an API token. Now we need create an API token.