File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed
Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change 66
77import asyncio
88import collections
9+ import contextlib
910
1011import aiohttp
1112from aiohttp import web
@@ -28,15 +29,17 @@ def __init__(self, country_code):
2829def get_flag (base_url , cc ):
2930 url = '{}/{cc}/{cc}.gif' .format (base_url , cc = cc .lower ())
3031 resp = yield from aiohttp .request ('GET' , url )
31- if resp .status == 200 :
32- image = yield from resp .read ()
33- return image
34- elif resp .status == 404 :
35- raise web .HTTPNotFound ()
36- else :
37- raise aiohttp .HttpProcessingError (
38- code = resp .status , message = resp .reason ,
39- headers = resp .headers )
32+ with contextlib .closing (resp ):
33+ if resp .status == 200 :
34+ image = yield from resp .read ()
35+ return image
36+ elif resp .status == 404 :
37+ raise web .HTTPNotFound ()
38+ else :
39+ raise aiohttp .HttpProcessingError (
40+ code = resp .status , message = resp .reason ,
41+ headers = resp .headers )
42+
4043
4144# BEGIN FLAGS2_ASYNCIO_EXECUTOR
4245@asyncio .coroutine
You can’t perform that action at this time.
0 commit comments