@@ -112,7 +112,7 @@ You should see something like:
112
112
(venv) $ python -c " import dependency_injector; print(dependency_injector.__version__)"
113
113
4.37.0
114
114
(venv) $ python -c " import flask; print(flask.__version__)"
115
- 1.1 .2
115
+ 2.0 .2
116
116
117
117
*Versions can be different. That's fine. *
118
118
@@ -444,9 +444,10 @@ and run in the terminal:
444
444
Now we need to add Github API client the container. We will need to add two more providers from
445
445
the ``dependency_injector.providers `` module:
446
446
447
- - ``Factory `` provider that will create ``Github `` client.
448
- - ``Configuration `` provider that will be used for providing the API token and the request timeout
449
- for the ``Github `` client.
447
+ - ``Factory `` provider. It will create a ``Github `` client.
448
+ - ``Configuration `` provider. It will provide an API token and a request timeout for the ``Github `` client.
449
+ We will specify the location of the configuration file. The configuration provider will parse
450
+ the configuration file when we create a container instance.
450
451
451
452
Edit ``containers.py ``:
452
453
@@ -461,31 +462,22 @@ Edit ``containers.py``:
461
462
462
463
class Container (containers .DeclarativeContainer ):
463
464
464
- config = providers.Configuration()
465
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
465
466
466
467
github_client = providers.Factory(
467
468
Github,
468
469
login_or_token = config.github.auth_token,
469
470
timeout = config.github.request_timeout,
470
471
)
471
472
472
- .. note ::
473
-
474
- We have used the configuration value before it was defined. That's the principle how
475
- ``Configuration `` provider works.
476
-
477
- Use first, define later.
478
473
479
474
.. note ::
480
475
481
476
Don't forget to remove the Ellipsis ``... `` from the container. We don't need it anymore
482
477
since we container is not empty.
483
478
484
- Now let's add the configuration file.
485
-
486
- We will use YAML.
487
-
488
- Create an empty file ``config.yml `` in the root of the project:
479
+ Now let's add the configuration file. We will use YAML. Create an empty file ``config.yml ``
480
+ in the root of the project:
489
481
490
482
.. code-block :: bash
491
483
:emphasize-lines: 11
@@ -530,17 +522,13 @@ and install it:
530
522
531
523
pip install -r requirements.txt
532
524
533
- We will use environment variable ``GITHUB_TOKEN `` to provide the API token.
534
-
535
- Now we need to edit ``create_app() `` to make two things when application starts:
536
-
537
- - Load the configuration file the ``config.yml ``.
538
- - Load the API token from the ``GITHUB_TOKEN `` environment variable.
525
+ We will use the ``GITHUB_TOKEN `` environment variable to provide the API token. Let's edit
526
+ ``create_app() `` to fetch the token value from it.
539
527
540
528
Edit ``application.py ``:
541
529
542
530
.. code-block :: python
543
- :emphasize- lines: 12 - 13
531
+ :emphasize- lines: 12
544
532
545
533
""" Application module."""
546
534
@@ -553,7 +541,6 @@ Edit ``application.py``:
553
541
554
542
def create_app () -> Flask:
555
543
container = Container()
556
- container.config.from_yaml(" config.yml" )
557
544
container.config.github.auth_token.from_env(" GITHUB_TOKEN" )
558
545
559
546
app = Flask(__name__ )
@@ -684,7 +671,7 @@ Edit ``containers.py``:
684
671
685
672
class Container (containers .DeclarativeContainer ):
686
673
687
- config = providers.Configuration()
674
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
688
675
689
676
github_client = providers.Factory(
690
677
Github,
@@ -732,38 +719,39 @@ Edit ``views.py``:
732
719
repositories = repositories,
733
720
)
734
721
735
- To make the injection work we need to wire the container instance with the ``views `` module.
736
- This needs to be done once. After it's done we can use `` Provide `` markers to specify as many
737
- injections as needed for any view .
722
+ To make the injection work we need to wire the container with the ``views `` module.
723
+ Let's configure the container to automatically make wiring with the `` views `` module when we
724
+ create a container instance .
738
725
739
- Edit ``application .py ``:
726
+ Edit ``containers .py ``:
740
727
741
728
.. code-block :: python
742
- :emphasize- lines: 14
729
+ :emphasize- lines: 11
743
730
744
- """ Application module."""
731
+ """ Containers module."""
745
732
746
- from flask import Flask
747
- from flask_bootstrap import Bootstrap
733
+ from dependency_injector import containers, providers
734
+ from github import Github
748
735
749
- from .containers import Container
750
- from . import views
736
+ from . import services
751
737
752
738
753
- def create_app () -> Flask:
754
- container = Container()
755
- container.config.from_yaml(" config.yml" )
756
- container.config.github.auth_token.from_env(" GITHUB_TOKEN" )
757
- container.wire(modules = [views])
739
+ class Container (containers .DeclarativeContainer ):
758
740
759
- app = Flask(__name__ )
760
- app.container = container
761
- app.add_url_rule(" /" , " index" , views.index)
741
+ wiring_config = containers.WiringConfiguration(modules = [" .views" ])
762
742
763
- bootstrap = Bootstrap()
764
- bootstrap.init_app(app)
743
+ config = providers.Configuration(yaml_files = [" config.yml" ])
765
744
766
- return app
745
+ github_client = providers.Factory(
746
+ Github,
747
+ login_or_token = config.github.auth_token,
748
+ timeout = config.github.request_timeout,
749
+ )
750
+
751
+ search_service = providers.Factory(
752
+ services.SearchService,
753
+ github_client = github_client,
754
+ )
767
755
768
756
Make sure the app is running or use ``flask run `` and open ``http://127.0.0.1:5000/ ``.
769
757
@@ -960,23 +948,23 @@ You should see:
960
948
961
949
.. code-block :: bash
962
950
963
- platform darwin -- Python 3.9 , pytest-5.4.3 , py-1.9 .0, pluggy-0.13.1
964
- plugins: flask-1 .0.0, cov-2.10 .0
951
+ platform darwin -- Python 3.10.0 , pytest-6.2.5 , py-1.10 .0, pluggy-1.0.0
952
+ plugins: cov-3 .0.0, flask-1.2 .0
965
953
collected 2 items
966
954
967
955
githubnavigator/tests.py .. [100%]
968
956
969
- ---------- coverage: platform darwin, python 3.9 - ----------
957
+ ---------- coverage: platform darwin, python 3.10.0-final-0 ----------
970
958
Name Stmts Miss Cover
971
959
----------------------------------------------------
972
960
githubnavigator/__init__.py 0 0 100%
973
- githubnavigator/application.py 15 0 100%
974
- githubnavigator/containers.py 7 0 100%
961
+ githubnavigator/application.py 13 0 100%
962
+ githubnavigator/containers.py 8 0 100%
975
963
githubnavigator/services.py 14 0 100%
976
964
githubnavigator/tests.py 34 0 100%
977
965
githubnavigator/views.py 10 0 100%
978
966
----------------------------------------------------
979
- TOTAL 80 0 100%
967
+ TOTAL 79 0 100%
980
968
981
969
.. note ::
982
970
0 commit comments