Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
fixes #15 dynamodb compression issue. The HTTP client should not deco…
Browse files Browse the repository at this point in the history
…mpress responses
  • Loading branch information
lsbardel committed Nov 17, 2016
1 parent 95e0b61 commit 08131eb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ Some part of the module are taken from aiobotocore_ - `apache LICENSE <https://g
Asyncio Botocore
--------------------

The first implementation uses asyncio from the python standard libray only:
The first implementation uses asyncio from the python standard libray only and
requires an asyncio compatible HTTP client such as the pulsar one
(used by default):

.. code:: python
Expand Down
19 changes: 18 additions & 1 deletion cloud/asyncbotocore/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
import zlib

import botocore.endpoint
from botocore.endpoint import first_non_none_response, MAX_POOL_CONNECTIONS
Expand Down Expand Up @@ -27,10 +28,26 @@ async def convert_to_response_dict(http_response, operation_model):
elif operation_model.has_streaming_output:
response_dict['body'] = patch_stream(http_response.raw)
else:
response_dict['body'] = await read(http_response)
body = await read(http_response)
encoding = headers.get('Content-Encoding')
if encoding == "gzip":
body = decompress_gzip(body)
elif encoding == "deflate":
body = decompress_deflate(body)
response_dict['body'] = body
return response_dict


def decompress_deflate(body):
decompress = zlib.decompressobj()
return decompress.decompress(body)


def decompress_gzip(body):
decompress = zlib.decompressobj(16+zlib.MAX_WBITS)
return decompress.decompress(body)


async def read(http_response):
body = await http_response.raw.read()
http_response._content = body
Expand Down
2 changes: 1 addition & 1 deletion cloud/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, service_name, region_name=None,
session=None, http_session=None,
**kwargs):
if not http_session:
http_session = HttpClient(loop=loop)
http_session = HttpClient(loop=loop, decompress=False)
self._session = get_session()
self._client = self._session.create_client(
service_name, region_name=region_name,
Expand Down
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class BotocoreMixin:
@classmethod
def setUpClass(cls):
cls.green_pool = GreenPool()
cls.sessions = HttpClient(pool_size=4, close_connections=True)
cls.sessions = HttpClient(pool_size=4, close_connections=True,
decompress=False)
cls.kwargs = dict(http_session=cls.sessions,
region_name='us-east-1')
cls.test = unittest.TestCase()
Expand Down
27 changes: 13 additions & 14 deletions tests/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ async def create_table(cls, table_name=None):
{
'AttributeName': 'testKey',
'AttributeType': 'S'
},
}
],
'KeySchema': [
{
'AttributeName': 'testKey',
'KeyType': 'HASH'
},
}
],
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
},
}
}
response = await cls.client.create_table(**table_kwargs)
while not await cls.is_table_ready(table_name):
Expand All @@ -60,27 +60,26 @@ async def delete_table(cls, table_name=None):
await cls.client.delete_table(table_name or cls.table_name)

@classmethod
async def put_item(cls, key_string_value):
async def put_item(cls, key_string_value, **item):
item['testKey'] = {
'S': key_string_value
}
response = await cls.client.put_item(
TableName=cls.table_name,
Item={
'testKey': {
'S': key_string_value
}
},
Item=item
)
cls.assert_status(response)

async def test_can_get_item(self):
test_value = 'testValue'
await self.put_item(test_value)
async def test_get_item(self):
test_key = 'testValue'
await self.put_item(test_key, foo={'S': 't' * 2**13})
response = await self.client.get_item(
TableName=self.table_name,
Key={
'testKey': {
'S': test_value
'S': test_key
}
},
)
self.assert_status(response)
self.assertEqual(response['Item']['testKey'], {'S': test_value})
self.assertEqual(response['Item']['testKey'], {'S': test_key})

0 comments on commit 08131eb

Please sign in to comment.