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 6
6
7
7
import asyncio
8
8
import collections
9
+ import contextlib
9
10
10
11
import aiohttp
11
12
from aiohttp import web
@@ -28,15 +29,17 @@ def __init__(self, country_code):
28
29
def get_flag (base_url , cc ):
29
30
url = '{}/{cc}/{cc}.gif' .format (base_url , cc = cc .lower ())
30
31
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
+
40
43
41
44
# BEGIN FLAGS2_ASYNCIO_EXECUTOR
42
45
@asyncio .coroutine
You can’t perform that action at this time.
0 commit comments