mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-14 18:12:21 +03:00
Refactor aiohttp tutorial: Giphy API client secion
This commit is contained in:
parent
b25a1395d0
commit
b5c4738c7f
|
@ -334,21 +334,16 @@ providers from the ``dependency_injector.providers`` module:
|
||||||
Edit ``containers.py``:
|
Edit ``containers.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 3,7,15,17-21
|
:emphasize-lines: 3-5,10-16
|
||||||
|
|
||||||
"""Application containers module."""
|
"""Containers module."""
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.ext import aiohttp
|
|
||||||
from aiohttp import web
|
|
||||||
|
|
||||||
from . import giphy, views
|
from . import giphy
|
||||||
|
|
||||||
|
|
||||||
class ApplicationContainer(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
"""Application container."""
|
|
||||||
|
|
||||||
app = aiohttp.Application(web.Application)
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
@ -358,8 +353,6 @@ Edit ``containers.py``:
|
||||||
timeout=config.giphy.request_timeout,
|
timeout=config.giphy.request_timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
index_view = aiohttp.View(views.index)
|
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
We have used the configuration value before it was defined. That's the principle how the
|
We have used the configuration value before it was defined. That's the principle how the
|
||||||
|
@ -382,7 +375,7 @@ Create an empty file ``config.yml`` in the root root of the project:
|
||||||
│ ├── application.py
|
│ ├── application.py
|
||||||
│ ├── containers.py
|
│ ├── containers.py
|
||||||
│ ├── giphy.py
|
│ ├── giphy.py
|
||||||
│ └── h.py
|
│ └── handlers.py
|
||||||
├── venv/
|
├── venv/
|
||||||
├── config.yml
|
├── config.yml
|
||||||
└── requirements.txt
|
└── requirements.txt
|
||||||
|
@ -410,24 +403,23 @@ Edit ``application.py``:
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
from .containers import ApplicationContainer
|
from .containers import Container
|
||||||
|
from . import handlers
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app() -> web.Application:
|
||||||
"""Create and return aiohttp application."""
|
container = Container()
|
||||||
container = ApplicationContainer()
|
|
||||||
container.config.from_yaml('config.yml')
|
container.config.from_yaml('config.yml')
|
||||||
container.config.giphy.api_key.from_env('GIPHY_API_KEY')
|
container.config.giphy.api_key.from_env('GIPHY_API_KEY')
|
||||||
|
|
||||||
app: web.Application = container.app()
|
app = web.Application()
|
||||||
app.container = container
|
app.container = container
|
||||||
|
|
||||||
app.add_routes([
|
app.add_routes([
|
||||||
web.get('/', container.index_view.as_view()),
|
web.get('/', handlers.index),
|
||||||
])
|
])
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
Now we need to create an API key and set it to the environment variable.
|
Now we need to create an API key and set it to the environment variable.
|
||||||
|
|
||||||
As for now, don’t worry, just take this one:
|
As for now, don’t worry, just take this one:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user