mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 07:57:43 +03:00 
			
		
		
		
	Update aiohttp example
This commit is contained in:
		
							parent
							
								
									170263de4d
								
							
						
					
					
						commit
						4fab71c35b
					
				|  | @ -9,7 +9,8 @@ follows `Semantic versioning`_ | |||
| 
 | ||||
| 4.0.0 | ||||
| ----- | ||||
| - Add wiring. | ||||
| - Add ``wiring`` feature. | ||||
| - Update ``aiohttp`` example. | ||||
| 
 | ||||
| 3.44.0 | ||||
| ------ | ||||
|  |  | |||
|  | @ -1,8 +1,8 @@ | |||
| Aiohttp Dependency Injection Example | ||||
| ==================================== | ||||
| Dependency Injector + Aiohttp Example | ||||
| ===================================== | ||||
| 
 | ||||
| Application ``giphynavigator`` is an `Aiohttp <https://docs.aiohttp.org/>`_ + | ||||
| `Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ application. | ||||
| `Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ example application. | ||||
| 
 | ||||
| Run | ||||
| --- | ||||
|  | @ -107,11 +107,11 @@ The output should be something like: | |||
|    --------------------------------------------------- | ||||
|    giphynavigator/__init__.py          0      0   100% | ||||
|    giphynavigator/__main__.py          5      5     0% | ||||
|    giphynavigator/application.py      10      0   100% | ||||
|    giphynavigator/containers.py       10      0   100% | ||||
|    giphynavigator/application.py      12      0   100% | ||||
|    giphynavigator/containers.py        6      0   100% | ||||
|    giphynavigator/giphy.py            14      9    36% | ||||
|    giphynavigator/services.py          9      1    89% | ||||
|    giphynavigator/tests.py            35      0   100% | ||||
|    giphynavigator/views.py             7      0   100% | ||||
|    giphynavigator/tests.py            37      0   100% | ||||
|    giphynavigator/views.py             9      0   100% | ||||
|    --------------------------------------------------- | ||||
|    TOTAL                              90     15    83% | ||||
|    TOTAL                              92     15    84% | ||||
							
								
								
									
										5
									
								
								examples/miniapps/aiohttp-giphynav/config.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								examples/miniapps/aiohttp-giphynav/config.yml
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | |||
| giphy: | ||||
|   request_timeout: 10 | ||||
| default: | ||||
|   query: "Dependency Injector" | ||||
|   limit: 10 | ||||
|  | @ -2,20 +2,23 @@ | |||
| 
 | ||||
| from aiohttp import web | ||||
| 
 | ||||
| from .containers import ApplicationContainer | ||||
| from .containers import Container | ||||
| from . import views | ||||
| 
 | ||||
| 
 | ||||
| def create_app(): | ||||
|     """Create and return aiohttp application.""" | ||||
|     container = ApplicationContainer() | ||||
|     container = Container() | ||||
|     container.config.from_yaml('config.yml') | ||||
|     container.config.giphy.api_key.from_env('GIPHY_API_KEY') | ||||
| 
 | ||||
|     app: web.Application = container.app() | ||||
|     container.wire(modules=[views]) | ||||
| 
 | ||||
|     app = web.Application() | ||||
|     app.container = container | ||||
| 
 | ||||
|     app.add_routes([ | ||||
|         web.get('/', container.index_view.as_view()), | ||||
|         web.get('/', views.index), | ||||
|     ]) | ||||
| 
 | ||||
|     return app | ||||
|  | @ -0,0 +1,21 @@ | |||
| """Containers module.""" | ||||
| 
 | ||||
| from dependency_injector import containers, providers | ||||
| 
 | ||||
| from . import giphy, services | ||||
| 
 | ||||
| 
 | ||||
| class Container(containers.DeclarativeContainer): | ||||
| 
 | ||||
|     config = providers.Configuration() | ||||
| 
 | ||||
|     giphy_client = providers.Factory( | ||||
|         giphy.GiphyClient, | ||||
|         api_key=config.giphy.api_key, | ||||
|         timeout=config.giphy.request_timeout, | ||||
|     ) | ||||
| 
 | ||||
|     search_service = providers.Factory( | ||||
|         services.SearchService, | ||||
|         giphy_client=giphy_client, | ||||
|     ) | ||||
|  | @ -10,7 +10,9 @@ from giphynavigator.giphy import GiphyClient | |||
| 
 | ||||
| @pytest.fixture | ||||
| def app(): | ||||
|     return create_app() | ||||
|     app = create_app() | ||||
|     yield app | ||||
|     app.container.unwire() | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
|  | @ -73,5 +75,5 @@ async def test_index_default_params(client, app): | |||
| 
 | ||||
|     assert response.status == 200 | ||||
|     data = await response.json() | ||||
|     assert data['query'] == app.container.config.search.default_query() | ||||
|     assert data['limit'] == app.container.config.search.default_limit() | ||||
|     assert data['query'] == app.container.config.default.query() | ||||
|     assert data['limit'] == app.container.config.default.limit() | ||||
|  | @ -1,15 +1,17 @@ | |||
| """Views module.""" | ||||
| 
 | ||||
| from aiohttp import web | ||||
| from dependency_injector.wiring import Provide | ||||
| 
 | ||||
| from .services import SearchService | ||||
| from .containers import Container | ||||
| 
 | ||||
| 
 | ||||
| async def index( | ||||
|         request: web.Request, | ||||
|         search_service: SearchService, | ||||
|         default_query: str, | ||||
|         default_limit: int, | ||||
|         search_service: SearchService = Provide[Container.search_service], | ||||
|         default_query: str = Provide[Container.config.default.query], | ||||
|         default_limit: int = Provide[Container.config.default.limit.as_int()], | ||||
| ) -> web.Response: | ||||
|     query = request.query.get('query', default_query) | ||||
|     limit = int(request.query.get('limit', default_limit)) | ||||
|  | @ -1,5 +0,0 @@ | |||
| giphy: | ||||
|   request_timeout: 10 | ||||
| search: | ||||
|   default_query: "Dependency Injector" | ||||
|   default_limit: 10 | ||||
|  | @ -1,33 +0,0 @@ | |||
| """Application containers module.""" | ||||
| 
 | ||||
| from dependency_injector import containers, providers | ||||
| from dependency_injector.ext import aiohttp | ||||
| from aiohttp import web | ||||
| 
 | ||||
| from . import giphy, services, views | ||||
| 
 | ||||
| 
 | ||||
| class ApplicationContainer(containers.DeclarativeContainer): | ||||
|     """Application container.""" | ||||
| 
 | ||||
|     app = aiohttp.Application(web.Application) | ||||
| 
 | ||||
|     config = providers.Configuration() | ||||
| 
 | ||||
|     giphy_client = providers.Factory( | ||||
|         giphy.GiphyClient, | ||||
|         api_key=config.giphy.api_key, | ||||
|         timeout=config.giphy.request_timeout, | ||||
|     ) | ||||
| 
 | ||||
|     search_service = providers.Factory( | ||||
|         services.SearchService, | ||||
|         giphy_client=giphy_client, | ||||
|     ) | ||||
| 
 | ||||
|     index_view = aiohttp.View( | ||||
|         views.index, | ||||
|         search_service=search_service, | ||||
|         default_query=config.search.default_query, | ||||
|         default_limit=config.search.default_limit, | ||||
|     ) | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user