@@ -127,8 +127,6 @@ Now it's time to install the project requirements. We will use next packages:
127
127
128
128
- ``dependency-injector `` - the dependency injection framework
129
129
- ``aiohttp `` - the web framework
130
- - ``aiohttp-devtools `` - the helper library that will provide a development server with live
131
- reloading
132
130
- ``pyyaml `` - the YAML files parsing library, used for the reading of the configuration files
133
131
- ``pytest-aiohttp `` - the helper library for the testing of the ``aiohttp `` application
134
132
- ``pytest-cov `` - the helper library for measuring the test coverage
@@ -139,7 +137,6 @@ Put next lines into the ``requirements.txt`` file:
139
137
140
138
dependency-injector
141
139
aiohttp
142
- aiohttp-devtools
143
140
pyyaml
144
141
pytest-aiohttp
145
142
pytest-cov
@@ -232,26 +229,31 @@ Put next into the ``application.py``:
232
229
])
233
230
return app
234
231
232
+
233
+ if __name__ == " __main__" :
234
+ app = create_app()
235
+ web.run_app(app)
236
+
235
237
Now we're ready to run our application
236
238
237
239
Do next in the terminal:
238
240
239
241
.. code-block :: bash
240
242
241
- adev runserver giphynavigator/application.py --livereload
243
+ python -m giphynavigator.application
242
244
243
245
The output should be something like:
244
246
245
247
.. code-block :: bash
246
248
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)
249
251
250
252
Let's check that it works. Open another terminal session and use ``httpie ``:
251
253
252
254
.. code-block :: bash
253
255
254
- http http://127 .0.0.1:8000 /
256
+ http http://0 .0.0.0:8080 /
255
257
256
258
You should see:
257
259
@@ -261,7 +263,7 @@ You should see:
261
263
Content-Length: 844
262
264
Content-Type: application/json; charset=utf-8
263
265
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
265
267
266
268
{
267
269
"gifs" : [],
@@ -328,8 +330,10 @@ Now we need to add ``GiphyClient`` into the container. The ``GiphyClient`` has t
328
330
that have to be injected: the API key and the request timeout. We will need to use two more
329
331
providers from the ``dependency_injector.providers `` module:
330
332
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.
333
337
334
338
Edit ``containers.py ``:
335
339
@@ -345,26 +349,16 @@ Edit ``containers.py``:
345
349
346
350
class Container (containers .DeclarativeContainer ):
347
351
348
- config = providers.Configuration()
352
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
349
353
350
354
giphy_client = providers.Factory(
351
355
giphy.GiphyClient,
352
356
api_key = config.giphy.api_key,
353
357
timeout = config.giphy.request_timeout,
354
358
)
355
359
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:
368
362
369
363
.. code-block :: bash
370
364
:emphasize-lines: 9
@@ -387,17 +381,14 @@ and put next into it:
387
381
giphy :
388
382
request_timeout : 10
389
383
390
- We will use an environment variable ``GIPHY_API_KEY `` to provide the API key.
391
384
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.
396
387
397
388
Edit ``application.py ``:
398
389
399
390
.. code-block :: python
400
- :emphasize- lines: 11 - 12
391
+ :emphasize- lines: 11
401
392
402
393
""" Application module."""
403
394
@@ -409,7 +400,6 @@ Edit ``application.py``:
409
400
410
401
def create_app () -> web.Application:
411
402
container = Container()
412
- container.config.from_yaml(" config.yml" )
413
403
container.config.giphy.api_key.from_env(" GIPHY_API_KEY" )
414
404
415
405
app = web.Application()
@@ -420,6 +410,10 @@ Edit ``application.py``:
420
410
return app
421
411
422
412
413
+ if __name__ == " __main__" :
414
+ app = create_app()
415
+ web.run_app(app)
416
+
423
417
Now we need to create an API key and set it to the environment variable.
424
418
425
419
As for now, don’t worry, just take this one:
@@ -502,7 +496,7 @@ Edit ``containers.py``:
502
496
503
497
class Container (containers .DeclarativeContainer ):
504
498
505
- config = providers.Configuration()
499
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
506
500
507
501
giphy_client = providers.Factory(
508
502
giphy.GiphyClient,
@@ -555,47 +549,50 @@ Edit ``handlers.py``:
555
549
},
556
550
)
557
551
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 .
561
555
562
- Edit ``application .py ``:
556
+ Edit ``containers .py ``:
563
557
564
558
.. code-block :: python
565
- :emphasize- lines: 13
559
+ :emphasize- lines: 10
566
560
567
- """ Application module."""
561
+ """ Containers module."""
568
562
569
- from aiohttp import web
563
+ from dependency_injector import containers, providers
570
564
571
- from .containers import Container
572
- from . import handlers
565
+ from . import giphy, services
573
566
574
567
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 ):
580
569
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
+ )
587
584
588
- Make sure the app is running or use :
585
+ Make sure the app is running:
589
586
590
587
.. code-block :: bash
591
588
592
- adev runserver giphynavigator/application.py --livereload
589
+ python -m giphynavigator.application
593
590
594
591
and make a request to the API in the terminal:
595
592
596
593
.. code-block :: bash
597
594
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
599
596
600
597
You should see:
601
598
@@ -605,7 +602,7 @@ You should see:
605
602
Content-Length: 492
606
603
Content-Type: application/json; charset=utf-8
607
604
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
609
606
610
607
{
611
608
"gifs" : [
@@ -810,24 +807,24 @@ You should see:
810
807
811
808
.. code-block ::
812
809
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
815
812
collected 3 items
816
813
817
814
giphynavigator/tests.py ... [100%]
818
815
819
- ---------- coverage: platform darwin, python 3.9 - ----------
816
+ ---------- coverage: platform darwin, python 3.10.0-final-0 ----------
820
817
Name Stmts Miss Cover
821
818
---------------------------------------------------
822
819
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%
825
822
giphynavigator/giphy.py 14 9 36%
826
823
giphynavigator/handlers.py 10 0 100%
827
824
giphynavigator/services.py 9 1 89%
828
825
giphynavigator/tests.py 37 0 100%
829
826
---------------------------------------------------
830
- TOTAL 88 10 89 %
827
+ TOTAL 90 12 87 %
831
828
832
829
.. note ::
833
830
0 commit comments