Skip to content

Commit fe01ad4

Browse files
authored
Update examples to use config __init__ args (ets-labs#527)
* Update application-single-container example * Update application-multiple-containers example * Update decoupled-packages example * Update movie lister example * Update CLI tutorial * Update sanic example * Update sanic example with wiring_config * Update fastapi example * Update fastapi-simple example * Update fastapi-sqlalchemy example * Update flask-blueprints example * Update flask example and tutorial * Update aiohttp example and tutorial * Update asyncio-daemon example and tutorial
1 parent 6030950 commit fe01ad4

File tree

42 files changed

+310
-364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+310
-364
lines changed

docs/tutorials/aiohttp.rst

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ Now it's time to install the project requirements. We will use next packages:
127127

128128
- ``dependency-injector`` - the dependency injection framework
129129
- ``aiohttp`` - the web framework
130-
- ``aiohttp-devtools`` - the helper library that will provide a development server with live
131-
reloading
132130
- ``pyyaml`` - the YAML files parsing library, used for the reading of the configuration files
133131
- ``pytest-aiohttp`` - the helper library for the testing of the ``aiohttp`` application
134132
- ``pytest-cov`` - the helper library for measuring the test coverage
@@ -139,7 +137,6 @@ Put next lines into the ``requirements.txt`` file:
139137
140138
dependency-injector
141139
aiohttp
142-
aiohttp-devtools
143140
pyyaml
144141
pytest-aiohttp
145142
pytest-cov
@@ -232,26 +229,31 @@ Put next into the ``application.py``:
232229
])
233230
return app
234231
232+
233+
if __name__ == "__main__":
234+
app = create_app()
235+
web.run_app(app)
236+
235237
Now we're ready to run our application
236238

237239
Do next in the terminal:
238240

239241
.. code-block:: bash
240242
241-
adev runserver giphynavigator/application.py --livereload
243+
python -m giphynavigator.application
242244
243245
The output should be something like:
244246

245247
.. code-block:: bash
246248
247-
[18:52:59] Starting aux server at http://localhost:8001 ◆
248-
[18:52:59] Starting dev server at http://localhost:8000 ●
249+
======== Running on http://0.0.0.0:8080 ========
250+
(Press CTRL+C to quit)
249251
250252
Let's check that it works. Open another terminal session and use ``httpie``:
251253

252254
.. code-block:: bash
253255
254-
http http://127.0.0.1:8000/
256+
http http://0.0.0.0:8080/
255257
256258
You should see:
257259

@@ -261,7 +263,7 @@ You should see:
261263
Content-Length: 844
262264
Content-Type: application/json; charset=utf-8
263265
Date: Wed, 29 Jul 2020 21:01:50 GMT
264-
Server: Python/3.9 aiohttp/3.6.2
266+
Server: Python/3.10 aiohttp/3.6.2
265267
266268
{
267269
"gifs": [],
@@ -328,8 +330,10 @@ Now we need to add ``GiphyClient`` into the container. The ``GiphyClient`` has t
328330
that have to be injected: the API key and the request timeout. We will need to use two more
329331
providers from the ``dependency_injector.providers`` module:
330332

331-
- ``Factory`` provider that will create the ``GiphyClient`` client.
332-
- ``Configuration`` provider that will provide the API key and the request timeout.
333+
- ``Factory`` provider. It will create a ``GiphyClient`` client.
334+
- ``Configuration`` provider. It will provide an API key and a request timeout for the ``GiphyClient``
335+
client. We will specify the location of the configuration file. The configuration provider will parse
336+
the configuration file when we create a container instance.
333337

334338
Edit ``containers.py``:
335339

@@ -345,26 +349,16 @@ Edit ``containers.py``:
345349
346350
class Container(containers.DeclarativeContainer):
347351
348-
config = providers.Configuration()
352+
config = providers.Configuration(yaml_files=["config.yml"])
349353
350354
giphy_client = providers.Factory(
351355
giphy.GiphyClient,
352356
api_key=config.giphy.api_key,
353357
timeout=config.giphy.request_timeout,
354358
)
355359
356-
.. note::
357-
358-
We have used the configuration value before it was defined. That's the principle how the
359-
``Configuration`` provider works.
360-
361-
Use first, define later.
362-
363-
Now let's add the configuration file.
364-
365-
We will use YAML.
366-
367-
Create an empty file ``config.yml`` in the root root of the project:
360+
Now let's add the configuration file. We will use YAML. Create an empty file ``config.yml`` in
361+
the root root of the project:
368362

369363
.. code-block:: bash
370364
:emphasize-lines: 9
@@ -387,17 +381,14 @@ and put next into it:
387381
giphy:
388382
request_timeout: 10
389383
390-
We will use an environment variable ``GIPHY_API_KEY`` to provide the API key.
391384
392-
Now we need to edit ``create_app()`` to make two things when application starts:
393-
394-
- Load the configuration file the ``config.yml``.
395-
- Load the API key from the ``GIPHY_API_KEY`` environment variable.
385+
We will use the ``GIPHY_API_KEY`` environment variable to provide the API key. Let’s edit
386+
``create_app()`` to fetch the key value from it.
396387

397388
Edit ``application.py``:
398389

399390
.. code-block:: python
400-
:emphasize-lines: 11-12
391+
:emphasize-lines: 11
401392
402393
"""Application module."""
403394
@@ -409,7 +400,6 @@ Edit ``application.py``:
409400
410401
def create_app() -> web.Application:
411402
container = Container()
412-
container.config.from_yaml("config.yml")
413403
container.config.giphy.api_key.from_env("GIPHY_API_KEY")
414404
415405
app = web.Application()
@@ -420,6 +410,10 @@ Edit ``application.py``:
420410
return app
421411
422412
413+
if __name__ == "__main__":
414+
app = create_app()
415+
web.run_app(app)
416+
423417
Now we need to create an API key and set it to the environment variable.
424418

425419
As for now, don’t worry, just take this one:
@@ -502,7 +496,7 @@ Edit ``containers.py``:
502496
503497
class Container(containers.DeclarativeContainer):
504498
505-
config = providers.Configuration()
499+
config = providers.Configuration(yaml_files=["config.yml"])
506500
507501
giphy_client = providers.Factory(
508502
giphy.GiphyClient,
@@ -555,47 +549,50 @@ Edit ``handlers.py``:
555549
},
556550
)
557551
558-
To make the injection work we need to wire the container instance with the ``handlers`` module.
559-
This needs to be done once. After it's done we can use ``Provide`` markers to specify as many
560-
injections as needed for any handler.
552+
To make the injection work we need to wire the container with the ``handlers`` module.
553+
Let's configure the container to automatically make wiring with the ``handlers`` module when we
554+
create a container instance.
561555

562-
Edit ``application.py``:
556+
Edit ``containers.py``:
563557

564558
.. code-block:: python
565-
:emphasize-lines: 13
559+
:emphasize-lines: 10
566560
567-
"""Application module."""
561+
"""Containers module."""
568562
569-
from aiohttp import web
563+
from dependency_injector import containers, providers
570564
571-
from .containers import Container
572-
from . import handlers
565+
from . import giphy, services
573566
574567
575-
def create_app() -> web.Application:
576-
container = Container()
577-
container.config.from_yaml("config.yml")
578-
container.config.giphy.api_key.from_env("GIPHY_API_KEY")
579-
container.wire(modules=[handlers])
568+
class Container(containers.DeclarativeContainer):
580569
581-
app = web.Application()
582-
app.container = container
583-
app.add_routes([
584-
web.get("/", handlers.index),
585-
])
586-
return app
570+
wiring_config = containers.WiringConfiguration(modules=[".handlers"])
571+
572+
config = providers.Configuration(yaml_files=["config.yml"])
573+
574+
giphy_client = providers.Factory(
575+
giphy.GiphyClient,
576+
api_key=config.giphy.api_key,
577+
timeout=config.giphy.request_timeout,
578+
)
579+
580+
search_service = providers.Factory(
581+
services.SearchService,
582+
giphy_client=giphy_client,
583+
)
587584
588-
Make sure the app is running or use:
585+
Make sure the app is running:
589586

590587
.. code-block:: bash
591588
592-
adev runserver giphynavigator/application.py --livereload
589+
python -m giphynavigator.application
593590
594591
and make a request to the API in the terminal:
595592

596593
.. code-block:: bash
597594
598-
http http://localhost:8000/ query=="wow,it works" limit==5
595+
http http://0.0.0.0:8080/ query=="wow,it works" limit==5
599596
600597
You should see:
601598

@@ -605,7 +602,7 @@ You should see:
605602
Content-Length: 492
606603
Content-Type: application/json; charset=utf-8
607604
Date: Fri, 09 Oct 2020 01:35:48 GMT
608-
Server: Python/3.9 aiohttp/3.6.2
605+
Server: Python/3.10 aiohttp/3.6.2
609606
610607
{
611608
"gifs": [
@@ -810,24 +807,24 @@ You should see:
810807

811808
.. code-block::
812809
813-
platform darwin -- Python 3.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
814-
plugins: cov-2.10.0, aiohttp-0.3.0, asyncio-0.14.0
810+
platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
811+
plugins: asyncio-0.16.0, anyio-3.3.4, aiohttp-0.3.0, cov-3.0.0
815812
collected 3 items
816813
817814
giphynavigator/tests.py ... [100%]
818815
819-
---------- coverage: platform darwin, python 3.9 -----------
816+
---------- coverage: platform darwin, python 3.10.0-final-0 ----------
820817
Name Stmts Miss Cover
821818
---------------------------------------------------
822819
giphynavigator/__init__.py 0 0 100%
823-
giphynavigator/application.py 12 0 100%
824-
giphynavigator/containers.py 6 0 100%
820+
giphynavigator/application.py 13 2 85%
821+
giphynavigator/containers.py 7 0 100%
825822
giphynavigator/giphy.py 14 9 36%
826823
giphynavigator/handlers.py 10 0 100%
827824
giphynavigator/services.py 9 1 89%
828825
giphynavigator/tests.py 37 0 100%
829826
---------------------------------------------------
830-
TOTAL 88 10 89%
827+
TOTAL 90 12 87%
831828
832829
.. note::
833830

0 commit comments

Comments
 (0)