def serialize(self, event): """Serializes a monitor event to the CSM format :type event: BaseMonitorEvent :param event: The event to serialize to bytes :rtype: bytes :returns: The CSM serialized form of the event """ event_dict = self._get_base_event_dict(event) event_type = self._get_event_type(event) event_dict['Type'] = event_type for attr in self._SERIALIZEABLE_EVENT_PROPERTIES: value = getattr(event, attr, None) if value is not None: getattr(self, '_serialize_' + attr)(value, event_dict, event_type=event_type) return ensure_bytes(json.dumps(event_dict, separators=(',', ':')))
def convert_body_to_file_like_object(params, **kwargs): if 'Body' in params: if isinstance(params['Body'], six.string_types): params['Body'] = six.BytesIO(ensure_bytes(params['Body'])) elif isinstance(params['Body'], six.binary_type): params['Body'] = six.BytesIO(params['Body'])
def _chunked(self, headers): transfer_encoding = headers.get('Transfer-Encoding', b'') transfer_encoding = ensure_bytes(transfer_encoding) return transfer_encoding.lower() == b'chunked'
def test_non_string_or_bytes_raises_error(self): value = 500 with self.assertRaises(ValueError): ensure_bytes(value)
def test_non_ascii(self): value = u'\u2713' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'\xe2\x9c\x93')
def test_unicode(self): value = u'baz' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'baz')
def test_binary(self): value = b'bar' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'bar')
def test_string(self): value = 'foo' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'foo')