in alibabacloud-gateway-oss/python2/alibabacloud_gateway_oss/client.py [0:0]
def modify_response(self, context, attribute_map):
request = context.request
response = context.response
body_str = None
if UtilClient.is_4xx(response.status_code) or UtilClient.is_5xx(response.status_code):
body_str = UtilClient.read_as_string(response.body)
if not UtilClient.empty(body_str):
resp_map = XMLClient.parse_xml(body_str, None)
err = UtilClient.assert_as_map(resp_map.get('Error'))
raise TeaException({
'code': err.get('Code'),
'message': err.get('Message'),
'data': {
'statusCode': response.status_code,
'requestId': err.get('RequestId'),
'ecCode': err.get('EC'),
'Recommend': err.get('RecommendDoc'),
'hostId': err.get('HostId')
}
})
else:
headers = response.headers
request_id = headers.get('x-oss-request-id')
ec_code = headers.get('x-oss-ec-code')
raise TeaException({
'code': response.status_code,
'message': None,
'data': {
'statusCode': response.status_code,
'requestId': '%s' % TeaConverter.to_unicode(request_id),
'ecCode': ec_code
}
})
ctx = attribute_map.key
if not UtilClient.is_unset(ctx):
if not UtilClient.is_unset(ctx.get('crc')) and not UtilClient.is_unset(response.headers.get('x-oss-hash-crc64ecma')) and not StringClient.equals(ctx.get('crc'), response.headers.get('x-oss-hash-crc64ecma')):
raise TeaException({
'code': 'CrcNotMatched',
'data': {
'clientCrc': ctx.get('crc'),
'serverCrc': response.headers.get('x-oss-hash-crc64ecma')
}
})
if not UtilClient.is_unset(ctx.get('md5')) and not UtilClient.is_unset(response.headers.get('content-md5')) and not StringClient.equals(ctx.get('md5'), response.headers.get('content-md5')):
raise TeaException({
'code': 'MD5NotMatched',
'data': {
'clientMD5': ctx.get('md5'),
'serverMD5': response.headers.get('content-md5')
}
})
if not UtilClient.is_unset(response.body):
if UtilClient.equal_number(response.status_code, 204):
UtilClient.read_as_string(response.body)
elif StringClient.equals(request.body_type, 'xml'):
body_str = UtilClient.read_as_string(response.body)
response.deserialized_body = body_str
if not UtilClient.empty(body_str):
result = XMLClient.parse_xml(body_str, None)
list = MapClient.key_set(result)
if UtilClient.equal_number(ArrayClient.size(list), 1):
tmp = list[0]
try:
response.deserialized_body = UtilClient.assert_as_map(result.get(tmp))
except Exception as error:
response.deserialized_body = result
elif UtilClient.equal_string(request.body_type, 'binary'):
response.deserialized_body = response.body
elif UtilClient.equal_string(request.body_type, 'byte'):
byt = UtilClient.read_as_bytes(response.body)
response.deserialized_body = byt
elif UtilClient.equal_string(request.body_type, 'string'):
response.deserialized_body = UtilClient.read_as_string(response.body)
elif UtilClient.equal_string(request.body_type, 'json'):
obj = UtilClient.read_as_json(response.body)
res = UtilClient.assert_as_map(obj)
response.deserialized_body = res
elif UtilClient.equal_string(request.body_type, 'array'):
response.deserialized_body = UtilClient.read_as_json(response.body)
else:
response.deserialized_body = UtilClient.read_as_string(response.body)