azure-servicebus/azure/servicebus/control_client/_serialization.py
def _create_message(response, service_instance):
    ''' Create message from response.

    response:
        response from Service Bus cloud server.
    service_instance:
        the Service Bus client.
    '''
    respbody = response.body
    custom_properties = {}
    broker_properties = None
    message_type = None
    message_location = None

    # gets all information from respheaders.
    for name, value in response.headers:
        if name.lower() == 'brokerproperties':
            broker_properties = json.loads(value)
        elif name.lower() == 'content-type':
            message_type = value
        elif name.lower() == 'location':
            message_location = value
        # Exclude common HTTP headers to avoid noise. List
        # is not exhaustive. At worst, custom properties will contains
        # an unexpected content generated by the webserver and not the customer.
        elif name.lower() not in ['transfer-encoding',
                                  'server',
                                  'date',
                                  'strict-transport-security']:
            # Follow the spec:
            # https://docs.microsoft.com/rest/api/servicebus/message-headers-and-properties
            if '"' in value:
                value = value[1:-1].replace('\\"', '"')
                try:
                    custom_properties[name] = datetime.strptime(
                        value, '%a, %d %b %Y %H:%M:%S GMT')
                except ValueError:
                    custom_properties[name] = value
            elif value.lower() == 'true':
                custom_properties[name] = True
            elif value.lower() == 'false':
                custom_properties[name] = False
            else:  # in theory, only int or float
                try:
                    # int('3.1') doesn't work so need to get float('3.14') first
                    float_value = float(value)
                    if str(int(float_value)) == value:
                        custom_properties[name] = int(value)
                    else:
                        custom_properties[name] = float_value
                except ValueError:
                    # If we are here, this header does not respect the spec.
                    # Could be an unexpected HTTP header or an invalid
                    # header value. In both case we ignore without failing.
                    pass

    if message_type is None:
        message = Message(
            respbody, service_instance, message_location, custom_properties,
            'application/atom+xml;type=entry;charset=utf-8', broker_properties)
    else:
        message = Message(respbody, service_instance, message_location,
                          custom_properties, message_type, broker_properties)
    return message
def _create_message(response, service_instance):
    ''' Create message from response.

    response:
        response from Service Bus cloud server.
    service_instance:
        the Service Bus client.
    '''
    respbody = response.body
    custom_properties = {}
    broker_properties = None
    message_type = None
    message_location = None

    # gets all information from respheaders.
    for name, value in response.headers:
        if name.lower() == 'brokerproperties':
            broker_properties = json.loads(value)
        elif name.lower() == 'content-type':
            message_type = value
        elif name.lower() == 'location':
            message_location = value
        # Exclude common HTTP headers to avoid noise. List
        # is not exhaustive. At worst, custom properties will contains
        # an unexpected content generated by the webserver and not the customer.
        elif name.lower() not in ['transfer-encoding',
                                  'server',
                                  'date',
                                  'strict-transport-security']:
            # Follow the spec:
            # https://docs.microsoft.com/rest/api/servicebus/message-headers-and-properties
            if '"' in value:
                value = value[1:-1].replace('\\"', '"')
                try:
                    custom_properties[name] = datetime.strptime(
                        value, '%a, %d %b %Y %H:%M:%S GMT')
                except ValueError:
                    custom_properties[name] = value
            elif value.lower() == 'true':
                custom_properties[name] = True
            elif value.lower() == 'false':
                custom_properties[name] = False
            else:  # in theory, only int or float
                try:
                    # int('3.1') doesn't work so need to get float('3.14') first
                    float_value = float(value)
                    if str(int(float_value)) == value:
                        custom_properties[name] = int(value)
                    else:
                        custom_properties[name] = float_value
                except ValueError:
                    # If we are here, this header does not respect the spec.
                    # Could be an unexpected HTTP header or an invalid
                    # header value. In both case we ignore without failing.
                    pass

    if message_type is None:
        message = Message(
            respbody, service_instance, message_location, custom_properties,
            'application/atom+xml;type=entry;charset=utf-8', broker_properties)
    else:
        message = Message(respbody, service_instance, message_location,
                          custom_properties, message_type, broker_properties)
    return message
Create message from response.

    response:
        response from Service Bus cloud server.
    service_instance:
        the Service Bus client.