Ejemplo n.º 1
0
def convert_to_response_dict(http_response, operation_model):
    response_dict = {
        # botocore converts keys to str, so make sure that they are in
        # the expected case. See detailed discussion here:
        # https://github.com/aio-libs/aiobotocore/pull/116
        # aiohttp's CIMultiDict camel cases the headers :(
        'headers':
        CaseInsensitiveDict({
            k.decode('utf-8').lower(): v.decode('utf-8')
            for k, v in http_response.raw_headers
        }),
        'status_code':
        http_response.status_code,
    }

    if response_dict['status_code'] >= 300:
        body = yield from http_response.read()
        response_dict['body'] = body
    elif operation_model.has_streaming_output:
        response_dict['body'] = http_response.raw
    else:
        body = yield from http_response.read()
        response_dict['body'] = body

    if response_dict['headers'].get('content-encoding', '').lower() == 'gzip' \
            and isinstance(response_dict['body'], (bytes, bytearray)):
        response_dict['body'] = zlib.decompress(response_dict['body'],
                                                16 + zlib.MAX_WBITS)

    return response_dict
Ejemplo n.º 2
0
async def convert_to_response_dict(http_response, operation_model):
    response_dict = {
        # botocore converts keys to str, so make sure that they are in
        # the expected case. See detailed discussion here:
        # https://github.com/aio-libs/aiobotocore/pull/116
        # aiohttp's CIMultiDict camel cases the headers :(
        'headers':
        CaseInsensitiveDict({
            k.decode('utf-8').lower(): v.decode('utf-8')
            for k, v in http_response.raw_headers
        }),
        'status_code':
        http_response.status_code,
        'context': {
            'operation_name': operation_model.name,
        }
    }

    if response_dict['status_code'] >= 300:
        response_dict['body'] = await http_response.read()
    elif operation_model.has_event_stream_output:
        response_dict['body'] = http_response.raw
    elif operation_model.has_streaming_output:
        length = response_dict['headers'].get('content-length')
        response_dict['body'] = StreamingBody(http_response.raw, length)
    else:
        response_dict['body'] = await http_response.read()
    return response_dict
Ejemplo n.º 3
0
 def test_head_object(self):
     http_response = Mock()
     http_response.encoding = 'utf-8'
     http_response.headers = CaseInsensitiveDict({
         'Date':
         'Thu, 22 Aug 2013 02:11:57 GMT',
         'Content-Length':
         '265',
         'x-amz-request-id':
         '2B74ECB010FF029E',
         'ETag':
         '"40d06eb6194712ac1c915783004ef730"',
         'Server':
         'AmazonS3',
         'content-type':
         'binary/octet-stream',
         'Content-Type':
         'binary/octet-stream',
         'accept-ranges':
         'bytes',
         'Last-Modified':
         'Tue, 20 Aug 2013 18:33:25 GMT',
         'x-amz-server-side-encryption':
         'AES256',
         'x-amz-meta-mykey1':
         'value1',
         'x-amz-meta-mykey2':
         'value2',
     })
     http_response.content = ''
     http_response.request.method = 'HEAD'
     put_object = self.s3.get_operation('HeadObject')
     expected = {
         "AcceptRanges": "bytes",
         "ContentType": "binary/octet-stream",
         "LastModified": "Tue, 20 Aug 2013 18:33:25 GMT",
         "ContentLength": "265",
         "ETag": '"40d06eb6194712ac1c915783004ef730"',
         "ServerSideEncryption": "AES256",
         "Metadata": {
             'mykey1': 'value1',
             'mykey2': 'value2',
         }
     }
     response_data = get_response(self.session, put_object,
                                  http_response)[1]
     self.assertEqual(response_data, expected)
Ejemplo n.º 4
0
 def test_put_object(self):
     http_response = Mock()
     http_response.encoding = 'utf-8'
     http_response.headers = CaseInsensitiveDict({
         'Date': 'Thu, 22 Aug 2013 02:11:57 GMT',
         'Content-Length': '0',
         'x-amz-request-id': '2B74ECB010FF029E',
         'ETag': '"b081e66e7e0c314285c655cafb4d1e71"',
         'x-amz-id-2':
         'bKECRRBFttBRVbJPIVBLQwwipI0i+s9HMvNFdttR17ouR0pvQSKEJUR+1c6cW1nQ',
         'Server': 'AmazonS3',
         'content-type': 'text/xml'
     })
     http_response.content = ''
     put_object = self.s3.get_operation('PutObject')
     expected = {"ETag": '"b081e66e7e0c314285c655cafb4d1e71"'}
     response_data = get_response(self.session, put_object,
                                  http_response)[1]
     self.assertEqual(response_data, expected)
Ejemplo n.º 5
0
 def test_list_objects_with_invalid_content_length(self):
     http_response = Mock()
     http_response.encoding = 'utf-8'
     http_response.headers = CaseInsensitiveDict({
         'Date':
         'Thu, 22 Aug 2013 02:11:57 GMT',
         # We say we have 265 bytes but we're returning 0,
         # this should raise an exception because this is not
         # a HEAD request.
         'Content-Length':
         '265',
         'x-amz-request-id':
         '2B74ECB010FF029E',
         'ETag':
         '"40d06eb6194712ac1c915783004ef730"',
         'Server':
         'AmazonS3',
         'content-type':
         'binary/octet-stream',
         'Content-Type':
         'binary/octet-stream',
         'accept-ranges':
         'bytes',
         'Last-Modified':
         'Tue, 20 Aug 2013 18:33:25 GMT',
         'x-amz-server-side-encryption':
         'AES256'
     })
     http_response.content = ''
     http_response.request.method = 'GET'
     list_objects = self.s3.get_operation('ListObjects')
     expected = {
         "AcceptRanges": "bytes",
         "ContentType": "binary/octet-stream",
         "LastModified": "Tue, 20 Aug 2013 18:33:25 GMT",
         "ContentLength": "265",
         "ETag": '"40d06eb6194712ac1c915783004ef730"',
         "ServerSideEncryption": "AES256"
     }
     with self.assertRaises(IncompleteReadError):
         response_data = get_response(self.session, list_objects,
                                      http_response)[1]
Ejemplo n.º 6
0
def convert_to_response_dict(http_response, operation_model):
    response_dict = {
        # botocore converts keys to str, so make sure that they are in
        # the expected case. See detailed discussion here:
        # https://github.com/aio-libs/aiobotocore/pull/116
        # aiohttp's CIMultiDict camel cases the headers :(
        'headers': CaseInsensitiveDict(
            {k.decode('utf-8').lower(): v.decode('utf-8')
             for k, v in http_response.raw_headers}),
        'status_code': http_response.status_code,
    }

    if response_dict['status_code'] >= 300:
        body = yield from http_response.read()
        response_dict['body'] = body
    elif operation_model.has_streaming_output:
        response_dict['body'] = http_response.raw
    else:
        body = yield from http_response.read()
        response_dict['body'] = body
    return response_dict
Ejemplo n.º 7
0
 def test_head_object_with_json(self):
     http_response = Mock()
     http_response.encoding = 'utf-8'
     http_response.headers = CaseInsensitiveDict({
         'Date':
         'Thu, 22 Aug 2013 02:11:57 GMT',
         'Content-Length':
         '0',
         'x-amz-request-id':
         '2B74ECB010FF029E',
         'ETag':
         '"40d06eb6194712ac1c915783004ef730"',
         'Server':
         'AmazonS3',
         'content-type':
         'application/json',
         'Content-Type':
         'application/json',
         'accept-ranges':
         'bytes',
         'Last-Modified':
         'Tue, 20 Aug 2013 18:33:25 GMT',
         'x-amz-server-side-encryption':
         'AES256'
     })
     http_response.content = ''
     put_object = self.s3.get_operation('HeadObject')
     expected = {
         "AcceptRanges": "bytes",
         "ContentType": "application/json",
         "LastModified": "Tue, 20 Aug 2013 18:33:25 GMT",
         "ContentLength": "0",
         "ETag": '"40d06eb6194712ac1c915783004ef730"',
         "ServerSideEncryption": "AES256"
     }
     response_data = get_response(self.session, put_object,
                                  http_response)[1]
     self.assertEqual(response_data, expected)