Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support base64 encoding of bytes input for JSON blob types. #397

Merged
merged 2 commits into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions botocore/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ def _serialize_type_timestamp(self, serialized, value, shape, key):
serialized[key] = self._convert_timestamp_to_str(value)

def _serialize_type_blob(self, serialized, value, shape, key):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are now no longer consistently handling blob types across protocols, as this logic is only in the JSONSerializer, and this is now duplicated from the query protocol. I think we should refactor this so that all the protocols can share a similar blob serializer. There's even a TODO note in the RestXML serializer specifically about this point.

b64_encoded = base64.b64encode(
value.encode(self.DEFAULT_ENCODING)).strip().decode(
if isinstance(value, six.text_type):
value = value.encode(self.DEFAULT_ENCODING)
b64_encoded = base64.b64encode(value).strip().decode(
self.DEFAULT_ENCODING)
serialized[key] = b64_encoded

Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ def test_blob_handles_unicode_chars(self):
request, blob_bytes=body.encode('utf-8'))


class TestBinaryTypesJSON(unittest.TestCase):
def setUp(self):
self.model = {
'metadata': {'protocol': 'json', 'apiVersion': '2014-01-01',
'jsonVersion': '1.1', 'targetPrefix': 'foo'},
'documentation': '',
'operations': {
'TestOperation': {
'name': 'TestOperation',
'http': {
'method': 'POST',
'requestUri': '/',
},
'input': {'shape': 'InputShape'},
}
},
'shapes': {
'InputShape': {
'type': 'structure',
'members': {
'Blob': {'shape': 'BlobType'},
}
},
'BlobType': {
'type': 'blob',
}
}
}
self.service_model = ServiceModel(self.model)

def serialize_to_request(self, input_params):
request_serializer = serialize.create_serializer(
self.service_model.metadata['protocol'])
return request_serializer.serialize_to_request(
input_params, self.service_model.operation_model('TestOperation'))

def test_blob_accepts_bytes_type(self):
body = b'bytes body'
self.serialize_to_request(input_params={'Blob': body})


class TestTimestamps(unittest.TestCase):
def setUp(self):
self.model = {
Expand Down