mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 18:07:44 +03:00 
			
		
		
		
	Refactor aiohttp tutorial: Minimal application section
This commit is contained in:
		
							parent
							
								
									f4d58fc186
								
							
						
					
					
						commit
						b25a1395d0
					
				| 
						 | 
					@ -116,7 +116,7 @@ Initial project layout::
 | 
				
			||||||
   │   ├── __init__.py
 | 
					   │   ├── __init__.py
 | 
				
			||||||
   │   ├── application.py
 | 
					   │   ├── application.py
 | 
				
			||||||
   │   ├── containers.py
 | 
					   │   ├── containers.py
 | 
				
			||||||
   │   └── views.py
 | 
					   │   └── handlers.py
 | 
				
			||||||
   ├── venv/
 | 
					   ├── venv/
 | 
				
			||||||
   └── requirements.txt
 | 
					   └── requirements.txt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -164,14 +164,14 @@ The requirements are setup. Now we will build a minimal application.
 | 
				
			||||||
Minimal application
 | 
					Minimal application
 | 
				
			||||||
-------------------
 | 
					-------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
In this section we will build a minimal application. It will have an endpoint that we can call.
 | 
					In this section we will build a minimal application. It will have an endpoint that
 | 
				
			||||||
The endpoint will answer in the right format and will have no data.
 | 
					will answer our requests in json format. There will be no payload for now.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Edit ``views.py``:
 | 
					Edit ``handlers.py``:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. code-block:: python
 | 
					.. code-block:: python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   """Views module."""
 | 
					   """Handlers module."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   from aiohttp import web
 | 
					   from aiohttp import web
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -190,34 +190,25 @@ Edit ``views.py``:
 | 
				
			||||||
           },
 | 
					           },
 | 
				
			||||||
       )
 | 
					       )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Now let's create the main part of our application - the container. Container will keep all of the
 | 
					Now let's create a container. Container will keep all of the application components and their dependencies.
 | 
				
			||||||
application components and their dependencies. First two providers we need to add are
 | 
					 | 
				
			||||||
the ``aiohttp`` application provider and the view provider.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Put next into the ``containers.py``:
 | 
					Edit ``containers.py``:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. code-block:: python
 | 
					.. code-block:: python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   """Application containers module."""
 | 
					   """Containers module."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   from dependency_injector import containers
 | 
					   from dependency_injector import containers
 | 
				
			||||||
   from dependency_injector.ext import aiohttp
 | 
					 | 
				
			||||||
   from aiohttp import web
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
   from . import views
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   class ApplicationContainer(containers.DeclarativeContainer):
 | 
					   class Container(containers.DeclarativeContainer):
 | 
				
			||||||
       """Application container."""
 | 
					       ...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
       app = aiohttp.Application(web.Application)
 | 
					Container is empty for now. We will add the providers in the following sections.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
       index_view = aiohttp.View(views.index)
 | 
					Finally we need to create ``aiohttp`` application factory. It will create and configure container
 | 
				
			||||||
 | 
					and ``web.Application``. It is traditionally called ``create_app()``.
 | 
				
			||||||
At the last we need to create the ``aiohttp`` application factory. It is traditionally called
 | 
					We will assign ``index`` handler to handle user requests to the root ``/`` of our web application.
 | 
				
			||||||
``create_app()``. It will create the container. Then it will use the container to create
 | 
					 | 
				
			||||||
the ``aiohttp`` application. Last step is to configure the routing - we will assign
 | 
					 | 
				
			||||||
``index_view`` from the container to handle the requests to the root ``/`` of our REST API server.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Put next into the ``application.py``:
 | 
					Put next into the ``application.py``:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -227,20 +218,18 @@ Put next into the ``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()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
       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're ready to run our application
 | 
					Now we're ready to run our application
 | 
				
			||||||
| 
						 | 
					@ -258,7 +247,7 @@ The output should be something like:
 | 
				
			||||||
   [18:52:59] Starting aux server at http://localhost:8001 ◆
 | 
					   [18:52:59] Starting aux server at http://localhost:8001 ◆
 | 
				
			||||||
   [18:52:59] Starting dev server at http://localhost:8000 ●
 | 
					   [18:52:59] Starting dev server at http://localhost:8000 ●
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Let's use ``httpie`` to check that it works:
 | 
					Let's check that it works. Open another terminal session and use ``httpie``:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. code-block:: bash
 | 
					.. code-block:: bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -300,7 +289,7 @@ Create ``giphy.py`` module in the ``giphynavigator`` package:
 | 
				
			||||||
   │   ├── application.py
 | 
					   │   ├── application.py
 | 
				
			||||||
   │   ├── containers.py
 | 
					   │   ├── containers.py
 | 
				
			||||||
   │   ├── giphy.py
 | 
					   │   ├── giphy.py
 | 
				
			||||||
   │   └── views.py
 | 
					   │   └── handlers.py
 | 
				
			||||||
   ├── venv/
 | 
					   ├── venv/
 | 
				
			||||||
   └── requirements.txt
 | 
					   └── requirements.txt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -393,7 +382,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
 | 
				
			||||||
   │   └── views.py
 | 
					   │   └── h.py
 | 
				
			||||||
   ├── venv/
 | 
					   ├── venv/
 | 
				
			||||||
   ├── config.yml
 | 
					   ├── config.yml
 | 
				
			||||||
   └── requirements.txt
 | 
					   └── requirements.txt
 | 
				
			||||||
| 
						 | 
					@ -553,7 +542,7 @@ Edit ``views.py``:
 | 
				
			||||||
.. code-block:: python
 | 
					.. code-block:: python
 | 
				
			||||||
   :emphasize-lines: 5,8-11,15
 | 
					   :emphasize-lines: 5,8-11,15
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   """Views module."""
 | 
					   """Handlers module."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   from aiohttp import web
 | 
					   from aiohttp import web
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -679,7 +668,7 @@ Edit ``views.py``:
 | 
				
			||||||
.. code-block:: python
 | 
					.. code-block:: python
 | 
				
			||||||
   :emphasize-lines: 11-12,14-15
 | 
					   :emphasize-lines: 11-12,14-15
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   """Views module."""
 | 
					   """Handlers module."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   from aiohttp import web
 | 
					   from aiohttp import web
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,6 @@ from . import handlers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def create_app() -> web.Application:
 | 
					def create_app() -> web.Application:
 | 
				
			||||||
    """Create and return aiohttp application."""
 | 
					 | 
				
			||||||
    container = Container()
 | 
					    container = Container()
 | 
				
			||||||
    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')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user