@@ -177,16 +177,16 @@ Edit ``handlers.py``:
177
177
178
178
179
179
async def index (request : web.Request) -> web.Response:
180
- query = request.query.get(' query' , ' Dependency Injector' )
181
- limit = int (request.query.get(' limit' , 10 ))
180
+ query = request.query.get(" query" , " Dependency Injector" )
181
+ limit = int (request.query.get(" limit" , 10 ))
182
182
183
183
gifs = []
184
184
185
185
return web.json_response(
186
186
{
187
- ' query' : query,
188
- ' limit' : limit,
189
- ' gifs' : gifs,
187
+ " query" : query,
188
+ " limit" : limit,
189
+ " gifs" : gifs,
190
190
},
191
191
)
192
192
@@ -228,7 +228,7 @@ Put next into the ``application.py``:
228
228
app = web.Application()
229
229
app.container = container
230
230
app.add_routes([
231
- web.get(' / ' , handlers.index),
231
+ web.get(" / " , handlers.index),
232
232
])
233
233
return app
234
234
@@ -304,19 +304,19 @@ and put next into it:
304
304
305
305
class GiphyClient :
306
306
307
- API_URL = ' https://api.giphy.com/v1'
307
+ API_URL = " https://api.giphy.com/v1"
308
308
309
309
def __init__ (self , api_key , timeout ):
310
310
self ._api_key = api_key
311
311
self ._timeout = ClientTimeout(timeout)
312
312
313
313
async def search (self , query , limit ):
314
314
""" Make search API call and return result."""
315
- url = f ' { self .API_URL } /gifs/search '
315
+ url = f " { self .API_URL } /gifs/search "
316
316
params = {
317
- ' q ' : query,
318
- ' api_key' : self ._api_key,
319
- ' limit' : limit,
317
+ " q " : query,
318
+ " api_key" : self ._api_key,
319
+ " limit" : limit,
320
320
}
321
321
async with ClientSession(timeout = self ._timeout) as session:
322
322
async with session.get(url, params = params) as response:
@@ -409,13 +409,13 @@ Edit ``application.py``:
409
409
410
410
def create_app () -> web.Application:
411
411
container = Container()
412
- container.config.from_yaml(' config.yml' )
413
- container.config.giphy.api_key.from_env(' GIPHY_API_KEY' )
412
+ container.config.from_yaml(" config.yml" )
413
+ container.config.giphy.api_key.from_env(" GIPHY_API_KEY" )
414
414
415
415
app = web.Application()
416
416
app.container = container
417
417
app.add_routes([
418
- web.get(' / ' , handlers.index),
418
+ web.get(" / " , handlers.index),
419
419
])
420
420
return app
421
421
@@ -483,7 +483,7 @@ and put next into it:
483
483
484
484
result = await self ._giphy_client.search(query, limit)
485
485
486
- return [{' url' : gif[' url' ]} for gif in result[' data' ]]
486
+ return [{" url" : gif[" url" ]} for gif in result[" data" ]]
487
487
488
488
The ``SearchService `` has a dependency on the ``GiphyClient ``. This dependency will be
489
489
injected when we add ``SearchService `` to the container.
@@ -531,7 +531,7 @@ Edit ``handlers.py``:
531
531
""" Handlers module."""
532
532
533
533
from aiohttp import web
534
- from dependency_injector.wiring import inject, Provide
534
+ from dependency_injector.wiring import Provide, inject
535
535
536
536
from .services import SearchService
537
537
from .containers import Container
@@ -542,16 +542,16 @@ Edit ``handlers.py``:
542
542
request : web.Request,
543
543
search_service : SearchService = Provide[Container.search_service],
544
544
) -> web.Response:
545
- query = request.query.get(' query' , ' Dependency Injector' )
546
- limit = int (request.query.get(' limit' , 10 ))
545
+ query = request.query.get(" query" , " Dependency Injector" )
546
+ limit = int (request.query.get(" limit" , 10 ))
547
547
548
548
gifs = await search_service.search(query, limit)
549
549
550
550
return web.json_response(
551
551
{
552
- ' query' : query,
553
- ' limit' : limit,
554
- ' gifs' : gifs,
552
+ " query" : query,
553
+ " limit" : limit,
554
+ " gifs" : gifs,
555
555
},
556
556
)
557
557
@@ -574,14 +574,14 @@ Edit ``application.py``:
574
574
575
575
def create_app () -> web.Application:
576
576
container = Container()
577
- container.config.from_yaml(' config.yml' )
578
- container.config.giphy.api_key.from_env(' GIPHY_API_KEY' )
577
+ container.config.from_yaml(" config.yml" )
578
+ container.config.giphy.api_key.from_env(" GIPHY_API_KEY" )
579
579
container.wire(modules = [handlers])
580
580
581
581
app = web.Application()
582
582
app.container = container
583
583
app.add_routes([
584
- web.get(' / ' , handlers.index),
584
+ web.get(" / " , handlers.index),
585
585
])
586
586
return app
587
587
@@ -651,7 +651,7 @@ Edit ``handlers.py``:
651
651
""" Handlers module."""
652
652
653
653
from aiohttp import web
654
- from dependency_injector.wiring import inject, Provide
654
+ from dependency_injector.wiring import Provide, inject
655
655
656
656
from .services import SearchService
657
657
from .containers import Container
@@ -664,16 +664,16 @@ Edit ``handlers.py``:
664
664
default_query : str = Provide[Container.config.default.query],
665
665
default_limit : int = Provide[Container.config.default.limit.as_int()],
666
666
) -> web.Response:
667
- query = request.query.get(' query' , default_query)
668
- limit = int (request.query.get(' limit' , default_limit))
667
+ query = request.query.get(" query" , default_query)
668
+ limit = int (request.query.get(" limit" , default_limit))
669
669
670
670
gifs = await search_service.search(query, limit)
671
671
672
672
return web.json_response(
673
673
{
674
- ' query' : query,
675
- ' limit' : limit,
676
- ' gifs' : gifs,
674
+ " query" : query,
675
+ " limit" : limit,
676
+ " gifs" : gifs,
677
677
},
678
678
)
679
679
@@ -745,60 +745,60 @@ and put next into it:
745
745
async def test_index (client , app ):
746
746
giphy_client_mock = mock.AsyncMock(spec = GiphyClient)
747
747
giphy_client_mock.search.return_value = {
748
- ' data' : [
749
- {' url' : ' https://giphy.com/gif1.gif' },
750
- {' url' : ' https://giphy.com/gif2.gif' },
748
+ " data" : [
749
+ {" url" : " https://giphy.com/gif1.gif" },
750
+ {" url" : " https://giphy.com/gif2.gif" },
751
751
],
752
752
}
753
753
754
754
with app.container.giphy_client.override(giphy_client_mock):
755
755
response = await client.get(
756
- ' / ' ,
756
+ " / " ,
757
757
params = {
758
- ' query' : ' test' ,
759
- ' limit' : 10 ,
758
+ " query" : " test" ,
759
+ " limit" : 10 ,
760
760
},
761
761
)
762
762
763
763
assert response.status == 200
764
764
data = await response.json()
765
765
assert data == {
766
- ' query' : ' test' ,
767
- ' limit' : 10 ,
768
- ' gifs' : [
769
- {' url' : ' https://giphy.com/gif1.gif' },
770
- {' url' : ' https://giphy.com/gif2.gif' },
766
+ " query" : " test" ,
767
+ " limit" : 10 ,
768
+ " gifs" : [
769
+ {" url" : " https://giphy.com/gif1.gif" },
770
+ {" url" : " https://giphy.com/gif2.gif" },
771
771
],
772
772
}
773
773
774
774
775
775
async def test_index_no_data (client , app ):
776
776
giphy_client_mock = mock.AsyncMock(spec = GiphyClient)
777
777
giphy_client_mock.search.return_value = {
778
- ' data' : [],
778
+ " data" : [],
779
779
}
780
780
781
781
with app.container.giphy_client.override(giphy_client_mock):
782
- response = await client.get(' / ' )
782
+ response = await client.get(" / " )
783
783
784
784
assert response.status == 200
785
785
data = await response.json()
786
- assert data[' gifs' ] == []
786
+ assert data[" gifs" ] == []
787
787
788
788
789
789
async def test_index_default_params (client , app ):
790
790
giphy_client_mock = mock.AsyncMock(spec = GiphyClient)
791
791
giphy_client_mock.search.return_value = {
792
- ' data' : [],
792
+ " data" : [],
793
793
}
794
794
795
795
with app.container.giphy_client.override(giphy_client_mock):
796
- response = await client.get(' / ' )
796
+ response = await client.get(" / " )
797
797
798
798
assert response.status == 200
799
799
data = await response.json()
800
- assert data[' query' ] == app.container.config.default.query()
801
- assert data[' limit' ] == app.container.config.default.limit()
800
+ assert data[" query" ] == app.container.config.default.query()
801
+ assert data[" limit" ] == app.container.config.default.limit()
802
802
803
803
Now let's run it and check the coverage:
804
804
0 commit comments