Skip to content

Commit

Permalink
switched to aiohttp v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
popravich committed Jul 9, 2014
1 parent ea3108a commit 8d81eb2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 34 deletions.
10 changes: 8 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
CHANGES
-------

0.1.0 (XXXX-XX-XX)
0.1.1 (XXXX-XX-XX)
^^^^^^^^^^^^^^^^^^

Basic REST API
* Switched to aiohttp v0.9.0


0.1.0 (2014-07-07)
^^^^^^^^^^^^^^^^^^

* Basic REST API
18 changes: 10 additions & 8 deletions aiorest/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import aiohttp.server
import time

from aiohttp.streams import EOF_MARKER

from . import errors
from .request import Request

Expand All @@ -28,12 +30,11 @@ def handle_request(self, message, payload):
try:
if payload is not None:
req_body = bytearray()
try:
while True:
chunk = yield from payload.read()
req_body.extend(chunk)
except aiohttp.EofStream:
pass
while True:
chunk = yield from payload.readany()
req_body.extend(chunk)
if chunk is EOF_MARKER:
break
else:
req_body = None

Expand Down Expand Up @@ -86,12 +87,13 @@ def handle_request(self, message, payload):

def handle_error(self, status=500, message=None, payload=None,
exc=None, headers=None):
now = time.time()
if isinstance(exc, errors.RESTError):
now = time.time()
resp_impl = aiohttp.Response(self.writer, status, close=True)
resp_impl.add_header('Host', self.hostname)
exc.write_response(resp_impl)
self.log_access(message, None, resp_impl, time.time() - now)
self.keep_alive(False)
else:
super().handle_error(status, payload, message, exc, headers)
super().handle_error(status, message, payload,
exc=exc, headers=headers)
2 changes: 1 addition & 1 deletion examples/cookie_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def query():
resp = yield from aiohttp.request(
'GET', 'http://127.0.0.1:8080/count',
connector=connector, loop=loop)
data = yield from resp.read_and_close(decode=True)
data = yield from resp.json()
print('Count is', data)

loop.run_until_complete(query())
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def query():
resp = yield from aiohttp.request(
'GET', 'http://127.0.0.1:8080/hello-world',
loop=loop)
json_data = yield from resp.read_and_close(decode=True)
json_data = yield from resp.json()
print(json_data)

name = os.environ.get('USER', 'John')
resp = yield from aiohttp.request(
'GET', 'http://127.0.0.1:8080/hello/{}'.format(name))
json_data = yield from resp.read_and_close(decode=True)
json_data = yield from resp.json()
print(json_data)

loop.run_until_complete(query())
Expand Down
2 changes: 1 addition & 1 deletion examples/redis_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def query():
resp = yield from aiohttp.request(
'GET', 'http://127.0.0.1:8080/count',
connector=connector, loop=loop)
data = yield from resp.read_and_close(decode=True)
data = yield from resp.json()
print('Count is', data)

loop.run_until_complete(query())
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from setuptools import setup, find_packages

install_requires = ['aiohttp>=0.8.0']
install_requires = ['aiohttp>=0.9.0']

PY_VER = sys.version_info

Expand Down
12 changes: 6 additions & 6 deletions tests/cookie_session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_init_session(self, time_mock):
@asyncio.coroutine
def query():
resp = yield from aiohttp.request('GET', url, loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)
cookies = {k: v.value for k, v in resp.cookies.items()}
value = make_cookie({'foo': 'bar'}, 1)
Expand All @@ -127,7 +127,7 @@ def query():
'GET', url,
cookies={'test_cookie': make_cookie({'foo': 'bar'}, 1)},
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)

self.loop.run_until_complete(query())
Expand All @@ -144,31 +144,31 @@ def queries():
resp = yield from aiohttp.request('GET', url + "/2",
connector=connector,
loop=self.loop)
data = yield from resp.read_and_close(decode=True)
data = yield from resp.json()
self.assertEqual(resp.status, 200)
self.assertEqual(data, {'result': 3})

# do increment
resp = yield from aiohttp.request('GET', url,
connector=connector,
loop=self.loop)
data = yield from resp.read_and_close(decode=True)
data = yield from resp.json()
self.assertEqual(resp.status, 200)
self.assertEqual(data, {'result': 4})

# try to override start value
resp = yield from aiohttp.request('GET', url + '/3',
connector=connector,
loop=self.loop)
data = yield from resp.read_and_close(decode=True)
data = yield from resp.json()
self.assertEqual(resp.status, 200)
self.assertEqual(data, {'result': 5})

# session deleted; try count
resp = yield from aiohttp.request('GET', url,
connector=connector,
loop=self.loop)
data = yield from resp.read_and_close(decode=True)
data = yield from resp.json()
self.assertEqual(resp.status, 200)
self.assertEqual(data, {'result': 1})

Expand Down
12 changes: 6 additions & 6 deletions tests/cors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def query():
resp = yield from aiohttp.request('GET', url,
headers=headers,
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)
self.assertIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
self.assertEqual(resp.headers['ACCESS-CONTROL-ALLOW-ORIGIN'],
Expand All @@ -85,7 +85,7 @@ def query():
resp = yield from aiohttp.request('OPTIONS', url,
headers=headers,
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)
self.assertIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
self.assertEqual(resp.headers['ACCESS-CONTROL-ALLOW-ORIGIN'],
Expand All @@ -100,7 +100,7 @@ def test_preflight_404(self):
def query():
resp = yield from aiohttp.request('OPTIONS', url,
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 404)
self.assertNotIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)

Expand All @@ -114,7 +114,7 @@ def query():
resp = yield from aiohttp.request('GET', url + '/check_origin',
headers={},
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)
self.assertNotIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
self.assertNotIn('ACCESS-CONTROL-ALLOW-METHOD', resp.headers)
Expand All @@ -128,7 +128,7 @@ def query():
resp = yield from aiohttp.request('GET', url + '/check_origin',
headers=headers,
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)
self.assertNotIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
self.assertNotIn('ACCESS-CONTROL-ALLOW-METHOD', resp.headers)
Expand All @@ -142,7 +142,7 @@ def query():
resp = yield from aiohttp.request('GET', url + '/check_origin',
headers=headers,
loop=self.loop)
yield from resp.read_and_close()
yield from resp.read()
self.assertEqual(resp.status, 200)
self.assertIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)

Expand Down
14 changes: 7 additions & 7 deletions tests/server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def query():
headers={'Content-Type': 'application/json'},
loop=self.loop)
self.assertEqual(200, response.status)
data = yield from response.read_and_close()
data = yield from response.read()
self.assertEqual(b'{"success": true}', data)

self.loop.run_until_complete(query())
Expand All @@ -114,7 +114,7 @@ def test_simple_GET(self):
def query():
response = yield from aiohttp.request('GET', url, loop=self.loop)
self.assertEqual(200, response.status)
data = yield from response.read_and_close()
data = yield from response.read()
self.assertEqual(b'{"success": true}', data)

self.loop.run_until_complete(query())
Expand All @@ -132,7 +132,7 @@ def test_GET_with_query_string(self):
def query():
response = yield from aiohttp.request('GET', url, loop=self.loop)
self.assertEqual(200, response.status)
data = yield from response.read_and_close()
data = yield from response.read()
dct = json.loads(data.decode('utf-8'))
self.assertEqual({'success': True,
'args': ['a', 'b'],
Expand All @@ -153,7 +153,7 @@ def test_set_cookie(self):
@asyncio.coroutine
def query():
response = yield from aiohttp.request('GET', url, loop=self.loop)
yield from response.read_and_close()
yield from response.read()
self.assertEqual(200, response.status)
self.assertIn('test_cookie', response.cookies)
self.assertEqual(response.cookies['test_cookie'].value, '123')
Expand All @@ -177,7 +177,7 @@ def query():
cookies={'test_cookie': 'value'},
loop=self.loop)
self.assertEqual(200, response.status)
data = yield from response.read_and_close()
data = yield from response.read()
dct = json.loads(data.decode('utf-8'))
self.assertEqual({'success': True,
'cookie': 'value',
Expand All @@ -200,7 +200,7 @@ def query():
'GET', url, headers={'ACCEPT-ENCODING': 'deflate'},
loop=self.loop)
self.assertEqual(200, response.status)
data = yield from response.read_and_close()
data = yield from response.read()
dct = json.loads(data.decode('utf-8'))
self.assertEqual({'success': True}, dct)
headers = response.message.headers
Expand All @@ -221,7 +221,7 @@ def query():
'GET', url, headers={'ACCEPT-ENCODING': 'gzip'},
loop=self.loop)
self.assertEqual(200, response.status)
yield from response.read_and_close()
yield from response.read()
# dct = json.loads(data.decode('utf-8'))
# self.assertEqual({'success': True}, dct)
headers = response.message.headers
Expand Down

0 comments on commit 8d81eb2

Please sign in to comment.