mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 01:47:36 +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``:
 | 
			
		||||
 | 
			
		||||
.. 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.ext import aiohttp
 | 
			
		||||
   from aiohttp import web
 | 
			
		||||
 | 
			
		||||
   from . import giphy, views
 | 
			
		||||
   from . import giphy
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   class ApplicationContainer(containers.DeclarativeContainer):
 | 
			
		||||
       """Application container."""
 | 
			
		||||
 | 
			
		||||
       app = aiohttp.Application(web.Application)
 | 
			
		||||
   class Container(containers.DeclarativeContainer):
 | 
			
		||||
 | 
			
		||||
       config = providers.Configuration()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -358,8 +353,6 @@ Edit ``containers.py``:
 | 
			
		|||
           timeout=config.giphy.request_timeout,
 | 
			
		||||
       )
 | 
			
		||||
 | 
			
		||||
       index_view = aiohttp.View(views.index)
 | 
			
		||||
 | 
			
		||||
.. note::
 | 
			
		||||
 | 
			
		||||
   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
 | 
			
		||||
   │   ├── containers.py
 | 
			
		||||
   │   ├── giphy.py
 | 
			
		||||
   │   └── h.py
 | 
			
		||||
   │   └── handlers.py
 | 
			
		||||
   ├── venv/
 | 
			
		||||
   ├── config.yml
 | 
			
		||||
   └── requirements.txt
 | 
			
		||||
| 
						 | 
				
			
			@ -410,24 +403,23 @@ Edit ``application.py``:
 | 
			
		|||
 | 
			
		||||
   from aiohttp import web
 | 
			
		||||
 | 
			
		||||
   from .containers import ApplicationContainer
 | 
			
		||||
   from .containers import Container
 | 
			
		||||
   from . import handlers
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   def create_app():
 | 
			
		||||
       """Create and return aiohttp application."""
 | 
			
		||||
       container = ApplicationContainer()
 | 
			
		||||
   def create_app() -> web.Application:
 | 
			
		||||
       container = Container()
 | 
			
		||||
       container.config.from_yaml('config.yml')
 | 
			
		||||
       container.config.giphy.api_key.from_env('GIPHY_API_KEY')
 | 
			
		||||
 | 
			
		||||
       app: web.Application = container.app()
 | 
			
		||||
       app = web.Application()
 | 
			
		||||
       app.container = container
 | 
			
		||||
 | 
			
		||||
       app.add_routes([
 | 
			
		||||
           web.get('/', container.index_view.as_view()),
 | 
			
		||||
           web.get('/', handlers.index),
 | 
			
		||||
       ])
 | 
			
		||||
 | 
			
		||||
       return app
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user