Пример #1
0
 def _build_message(self, body):
     if isinstance(body, list) and body:  # TODO: This only works for a list of bytes/strings
         self.message = uamqp.Message(body[0], properties=self.properties, header=self.header)
         for more in body[1:]:
             self.message._body.append(more)  # pylint: disable=protected-access
     elif body is None:
         raise ValueError("Message body cannot be None.")
     else:
         self.message = uamqp.Message(body, properties=self.properties, header=self.header)
Пример #2
0
async def test_event_hubs_async_sender_sync(live_eventhub_config):
    annotations={b"x-opt-partition-key": b"PartitionKeyInfo"}
    msg_content = b"hello world"

    message = uamqp.Message(msg_content, annotations=annotations)
    uri = "sb://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    sas_auth = authentication.SASTokenAsync.from_shared_access_key(uri, live_eventhub_config['key_name'], live_eventhub_config['access_key'])

    target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    send_client = uamqp.SendClientAsync(target, auth=sas_auth, debug=False)
   
    for _ in range(10):
        message = uamqp.Message(msg_content, application_properties=annotations, annotations=annotations)
        send_client.send_message(message)
    send_client.close()
Пример #3
0
    def _message_received(self, message):
        """Callback run on receipt of every message. If there is
        a user-defined callback, this will be called.
        Additionally if the client is retrieving messages for a batch
        or iterator, the message will be added to an internal queue.

        :param message: c_uamqp.Message
        """
        # pylint: disable=protected-access
        message_number = self._receiver.last_received_message_number()
        if self._settle_mode == constants.ReceiverSettleMode.ReceiveAndDelete:
            settler = None
        else:
            settler = functools.partial(self._settle_message, message_number)
        try:
            wrapped_message = uamqp.Message(
                message=message,
                encoding=self.encoding,
                settler=settler,
                delivery_no=message_number)
            self.on_message_received(wrapped_message)
        except RuntimeError:
            condition = b"amqp:unknown-error"
            self._error = errors._process_link_error(self.error_policy, condition, None, None)
            _logger.info("Unable to settle message no %r. Disconnecting.\nLink: %r\nConnection: %r",
                         message_number,
                         self.name,
                         self._session._connection.container_id)
        except KeyboardInterrupt:
            _logger.error("Received shutdown signal while processing message no %r\nRejecting message.", message_number)
            self._receiver.settle_modified_message(message_number, True, True, None)
            self._error = errors.AMQPClientShutdown()
        except Exception as e:  # pylint: disable=broad-except
            _logger.error("Error processing message no %r: %r\nRejecting message.", message_number, e)
            self._receiver.settle_modified_message(message_number, True, True, None)
def test_sys_properties():
    properties = uamqp.message.MessageProperties()
    properties.message_id = "message_id"
    properties.user_id = "user_id"
    properties.to = "to"
    properties.subject = "subject"
    properties.reply_to = "reply_to"
    properties.correlation_id = "correlation_id"
    properties.content_type = "content_type"
    properties.content_encoding = "content_encoding"
    properties.absolute_expiry_time = 1
    properties.creation_time = 1
    properties.group_id = "group_id"
    properties.group_sequence = 1
    properties.reply_to_group_id = "reply_to_group_id"
    message = uamqp.Message(properties=properties)
    message.annotations = {_common.PROP_OFFSET: "@latest"}
    ed = EventData._from_message(message)  # type: EventData

    assert ed.system_properties[_common.PROP_OFFSET] == "@latest"
    assert ed.system_properties[_common.PROP_CORRELATION_ID] == properties.correlation_id
    assert ed.system_properties[_common.PROP_MESSAGE_ID] == properties.message_id
    assert ed.system_properties[_common.PROP_CONTENT_ENCODING] == properties.content_encoding
    assert ed.system_properties[_common.PROP_CONTENT_TYPE] == properties.content_type
    assert ed.system_properties[_common.PROP_USER_ID] == properties.user_id
    assert ed.system_properties[_common.PROP_TO] == properties.to
    assert ed.system_properties[_common.PROP_SUBJECT] == properties.subject
    assert ed.system_properties[_common.PROP_REPLY_TO] == properties.reply_to
    assert ed.system_properties[_common.PROP_ABSOLUTE_EXPIRY_TIME] == properties.absolute_expiry_time
    assert ed.system_properties[_common.PROP_CREATION_TIME] == properties.creation_time
    assert ed.system_properties[_common.PROP_GROUP_ID] == properties.group_id
    assert ed.system_properties[_common.PROP_GROUP_SEQUENCE] == properties.group_sequence
    assert ed.system_properties[_common.PROP_REPLY_TO_GROUP_ID] == properties.reply_to_group_id
Пример #5
0
async def send_async():
    TOPIC_NAME = config['topic_name']
    SERVICE_NAMESPACE = config['service_namespace']
    KEY_NAME = config['key_name']
    KEY_VALUE = config['key_value']
    HOSTNAME = '{}.servicebus.windows.net'.format(SERVICE_NAMESPACE)

    uri = "sb://{}/{}".format(HOSTNAME, TOPIC_NAME)
    sas_auth = authentication.SASTokenAsync.from_shared_access_key(
        uri, KEY_NAME, KEY_VALUE)

    target = "amqps://{}/{}".format(HOSTNAME, TOPIC_NAME)
    send_client = uamqp.SendClientAsync(target, auth=sas_auth, debug=False)
    start_time = time.time()
    count = 0
    time_printed = 0

    while True:
        count += 1
        end_time = time.time()
        period = end_time - start_time
        if time_printed < int(period):
            time_printed = int(period)
            rate = count / (end_time - start_time)
            print("".join([str(count), " messages in ", str(period),
                           " secs ; rate=" + str(rate) + " msgs/sec"]))
        msg_content = b"hello world"
        message = uamqp.Message(msg_content)
        await send_client.send_message_async(message)
    await send_client.close_async()
Пример #6
0
def test_event_hubs_client_send_sync(live_eventhub_config):
    annotations = {b"x-opt-partition-key": b"PartitionKeyInfo"}
    uri = "sb://{}/{}".format(live_eventhub_config['hostname'],
                              live_eventhub_config['event_hub'])
    sas_auth = authentication.SASTokenAuth.from_shared_access_key(
        uri, live_eventhub_config['key_name'],
        live_eventhub_config['access_key'])

    target = "amqps://{}/{}".format(live_eventhub_config['hostname'],
                                    live_eventhub_config['event_hub'])
    send_client = uamqp.SendClient(target, auth=sas_auth, debug=False)
    for _ in range(10):
        header = uamqp.message.MessageHeader()
        header.durable = True
        assert header.get_header_obj()
        props = uamqp.message.MessageProperties(message_id=b"message id",
                                                subject="test_subject")
        assert props.get_properties_obj()
        msg_content = b"hello world"
        message = uamqp.Message(msg_content,
                                properties=props,
                                header=header,
                                application_properties=annotations,
                                annotations=annotations)
        assert message.get_message_encoded_size() == 151
        assert message.encode_message(
        ) == b'\x00Sp\xc0\x06\x05A@@@C\x00Sr\xc1(\x02\xa1\x13x-opt-partition-key\xa1\x10PartitionKeyInfo\x00Ss\xc0\x1d\x04\xa1\nmessage id@@\xa1\x0ctest_subject\x00St\xc1(\x02\xa1\x13x-opt-partition-key\xa1\x10PartitionKeyInfo\x00Su\xa0\x0bhello world'
        send_client.queue_message(message)
    results = send_client.send_all_messages(close_on_done=False)
    assert not [
        m for m in results if m == uamqp.constants.MessageState.SendFailed
    ]
    send_client.close()
Пример #7
0
    async def _mgmt_request_response(
            self, mgmt_operation, message, callback, keep_alive_associated_link=True, **kwargs
    ):
        await self._open()

        application_properties = {}
        # Some mgmt calls do not support an associated link name (such as list_sessions).  Most do, so on by default.
        if keep_alive_associated_link:
            try:
                application_properties = {ASSOCIATEDLINKPROPERTYNAME:self._handler.message_handler.name}
            except AttributeError:
                pass

        mgmt_msg = uamqp.Message(
            body=message,
            properties=MessageProperties(
                reply_to=self._mgmt_target,
                encoding=self._config.encoding,
                **kwargs),
            application_properties=application_properties)
        try:
            return await self._handler.mgmt_request_async(
                mgmt_msg,
                mgmt_operation,
                op_type=MGMT_REQUEST_OP_TYPE_ENTITY_MGMT,
                node=self._mgmt_target.encode(self._config.encoding),
                timeout=5000,
                callback=callback)
        except Exception as exp:  # pylint: disable=broad-except
            raise ServiceBusError("Management request failed: {}".format(exp), exp)
Пример #8
0
def send_c2d_message(target, device_id, data, properties=None,
                     correlation_id=None, ack=None):
    app_props = {}
    if properties:
        app_props.update(properties)

    app_props['iothub-ack'] = (ack if ack else 'none')

    msg_props = uamqp.message.MessageProperties()
    msg_props.to = '/devices/{}/messages/devicebound'.format(device_id)
    msg_id = str(uuid4())
    msg_props.message_id = msg_id

    if correlation_id:
        msg_props.correlation_id = correlation_id

    msg_content = str.encode(data)

    message = uamqp.Message(msg_content, properties=msg_props, application_properties=app_props)

    operation = '/messages/devicebound'
    endpoint = _build_iothub_amqp_endpoint_from_target(target)
    endpoint = endpoint + operation
    client = uamqp.SendClient('amqps://' + endpoint, debug=DEBUG)
    client.queue_message(message)
    result = client.send_all_messages()
    errors = [m for m in result if m == uamqp.constants.MessageState.SendFailed]
    return msg_id, errors
    async def _mgmt_request_response(
        self,
        mgmt_operation,
        message,
        callback,
        keep_alive_associated_link=True,
        timeout=None,
        **kwargs
    ):
        # type: (bytes, uamqp.Message, Callable, bool, Optional[float], Any) -> uamqp.Message
        """
        Execute an amqp management operation.

        :param bytes mgmt_operation: The type of operation to be performed. This value will
         be service-specific, but common values include READ, CREATE and UPDATE.
         This value will be added as an application property on the message.
        :param message: The message to send in the management request.
        :paramtype message: ~uamqp.message.Message
        :param callback: The callback which is used to parse the returning message.
        :paramtype callback: Callable[int, ~uamqp.message.Message, str]
        :param keep_alive_associated_link: A boolean flag for keeping associated amqp sender/receiver link alive when
         executing operation on mgmt links.
        :param timeout: timeout in seconds for executing the mgmt operation.
        :rtype: None
        """
        await self._open()

        application_properties = {}
        # Some mgmt calls do not support an associated link name (such as list_sessions).  Most do, so on by default.
        if keep_alive_associated_link:
            try:
                application_properties = {
                    ASSOCIATEDLINKPROPERTYNAME: self._handler.message_handler.name
                }
            except AttributeError:
                pass

        mgmt_msg = uamqp.Message(
            body=message,
            properties=MessageProperties(
                reply_to=self._mgmt_target, encoding=self._config.encoding, **kwargs
            ),
            application_properties=application_properties,
        )
        try:
            return await self._handler.mgmt_request_async(
                mgmt_msg,
                mgmt_operation,
                op_type=MGMT_REQUEST_OP_TYPE_ENTITY_MGMT,
                node=self._mgmt_target.encode(self._config.encoding),
                timeout=timeout * 1000 if timeout else None,
                callback=callback,
            )
        except Exception as exp:  # pylint: disable=broad-except
            if isinstance(exp, compat.TimeoutException):
                raise OperationTimeoutError(error=exp)
            raise
async def send_async(uri, key_name, key_value, target):
    sas_auth = authentication.SASTokenAsync.from_shared_access_key(
        uri, key_name, key_value)

    send_client = uamqp.SendClientAsync(target, auth=sas_auth, debug=False)

    while True:
        msg_content = b"hello world"
        message = uamqp.Message(msg_content)
        await send_client.send_message_async(message)
    await send_client.close_async()
 async def send_to_device(self, device_id, message):
     msg_content = json.dumps(message)
     app_properties = {}
     msg_props = uamqp.message.MessageProperties()
     msg_props.to = "/devices/{}/messages/devicebound".format(device_id)
     msg_props.message_id = str(uuid4())
     amqp_message = uamqp.Message(
         msg_content, properties=msg_props, application_properties=app_properties
     )
     await self.send_client.send_message_async(amqp_message)
     adapter_config.logger("AMQP service client sent: {}".format(message))
Пример #12
0
async def test_event_hubs_single_send_async(live_eventhub_config):
    annotations={b"x-opt-partition-key": b"PartitionKeyInfo"}
    msg_content = b"hello world"

    message = uamqp.Message(msg_content, annotations=annotations)
    uri = "sb://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    sas_auth = a_uamqp.SASTokenAsync.from_shared_access_key(uri, live_eventhub_config['key_name'], live_eventhub_config['access_key'])

    target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    send_client = a_uamqp.SendClientAsync(target, auth=sas_auth, debug=False)
    await send_client.send_message_async(message, close_on_done=True)
Пример #13
0
async def test_event_hubs_client_send_async(live_eventhub_config):
    properties = {b"SendData": b"Property_String_Value_1"}
    msg_content = b"hello world"

    message = uamqp.Message(msg_content, application_properties=properties)
    plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key'])
    target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    send_client = a_uamqp.SendClientAsync(target, auth=plain_auth, debug=True)
    send_client.queue_message(message)
    results = await send_client.send_all_messages_async()
    assert not [m for m in results if m == uamqp.constants.MessageState.Failed]
def test_rabbitmq_client_send_sync(rabbit_mq_config):
    pytest.skip("Not working yet")
    uri = "amqp://{}/{}/".format(rabbit_mq_config['hostname'], rabbit_mq_config['path'])
    sas_auth = uamqp.authentication.SASLAnonymous(hostname=rabbit_mq_config['hostname'], port=5672)
    send_client = uamqp.SendClient(uri, auth=sas_auth, debug=True)
    try:
        message = uamqp.Message("content")
        send_client.queue_message(message)
        results = send_client.send_all_messages(close_on_done=False)
        assert not [m for m in results if m == uamqp.constants.MessageState.SendFailed]
    finally:
        send_client.close()
Пример #15
0
def test_event_hubs_client_send_multiple_sync(live_eventhub_config):
    annotations = {b"x-opt-partition-key": b"PartitionKeyInfo"}
    uri = "sb://{}/{}".format(live_eventhub_config['hostname'],
                              live_eventhub_config['event_hub'])
    sas_auth = authentication.SASTokenAuth.from_shared_access_key(
        uri, live_eventhub_config['key_name'],
        live_eventhub_config['access_key'])
    assert not sas_auth.consumed

    target = "amqps://{}/{}".format(live_eventhub_config['hostname'],
                                    live_eventhub_config['event_hub'])
    send_client = uamqp.SendClient(target, auth=sas_auth, debug=False)
    messages = []
    for _ in range(10):
        header = uamqp.message.MessageHeader()
        header.durable = True
        header.delivery_count = 5
        header.time_to_live = 500000
        header.first_acquirer = True
        header.priority = 3
        assert header.get_header_obj()
        props = uamqp.message.MessageProperties(
            message_id=b"message id",
            user_id="user_id",
            to="to",
            subject="test",
            reply_to="reply_to",
            correlation_id="correlation_id",
            content_type="content_type",
            content_encoding="content_encoding",
            creation_time=12345,
            absolute_expiry_time=12345,
            group_id="group_id",
            group_sequence=1234,
            reply_to_group_id="reply_to_group_id")
        assert props.get_properties_obj()
        msg_content = b"hello world"
        message = uamqp.Message(msg_content,
                                properties=props,
                                header=header,
                                application_properties=annotations,
                                annotations=annotations)
        messages.append(message)
    send_client.queue_message(*messages)
    assert len(send_client.pending_messages) == 10
    results = send_client.send_all_messages()
    assert len(results) == 10
    assert not [
        m for m in results if m == uamqp.constants.MessageState.SendFailed
    ]
    assert sas_auth.consumed
    assert send_client.pending_messages == []
Пример #16
0
def test_event_data_from_message():
    message = uamqp.Message('A')
    event = EventData._from_message(message)
    assert event.content_type is None
    assert event.correlation_id is None
    assert event.message_id is None

    event.content_type = 'content_type'
    event.correlation_id = 'correlation_id'
    event.message_id = 'message_id'
    assert event.content_type == 'content_type'
    assert event.correlation_id == 'correlation_id'
    assert event.message_id == 'message_id'
 def send_to_device(self, device_id, message):
     msg_content = message
     app_properties = {}
     msg_props = uamqp.message.MessageProperties()
     msg_props.to = "/devices/{}/messages/devicebound".format(device_id)
     msg_props.message_id = str(uuid4())
     message = uamqp.Message(
         msg_content, properties=msg_props, application_properties=app_properties
     )
     self.send_client.queue_message(message)
     results = self.send_client.send_all_messages()
     assert not [m for m in results if m == uamqp.constants.MessageState.SendFailed]
     logger.info("Message sent.")
Пример #18
0
 def _message_received(self, message):
     """Callback run on receipt of every message. If there is
     a user-defined callback, this will be called.
     Additionally if the client is retrieving messages for a batch
     or iterator, the message will be added to an internal queue.
     :param message: c_uamqp.Message
     """
     self._was_message_received = True
     wrapped_message = uamqp.Message(message=message, encoding=self._encoding)
     if self._message_received_callback:
         wrapped_message = self._message_received_callback(wrapped_message) or wrapped_message
     if self._received_messages:
         self._received_messages.put(wrapped_message)
Пример #19
0
async def test_event_hubs_mgmt_op_async(live_eventhub_config):

    plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key'])
    target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    async with uamqp.AMQPClientAsync(target, auth=plain_auth, debug=True) as send_client:
        mgmt_msg = uamqp.Message(application_properties={'name': live_eventhub_config['event_hub']})
        response = await send_client.mgmt_request_async(
            mgmt_msg,
            b'READ',
            op_type=b'com.microsoft:eventhub',
            status_code_field=b'status-code',
            description_fields=b'status-description',
            timeout=3000)
        output = response.get_data()
        assert output[b'partition_ids'] == [b"0", b"1"]
Пример #20
0
 def send_to_device(self, device_id, message):
     msg_content = message
     app_properties = {}
     msg_props = uamqp.message.MessageProperties()
     msg_props.to = "/devices/{}/messages/devicebound".format(device_id)
     msg_props.message_id = str(uuid4())
     message = uamqp.Message(msg_content,
                             properties=msg_props,
                             application_properties=app_properties)
     self.send_client.queue_message(message)
     results = self.send_client.send_all_messages(close_on_done=False)
     assert not [
         m for m in results if m == uamqp.constants.MessageState.SendFailed
     ]
     adapter_config.logger("AMQP service client sent: {}".format(message))
def test_event_hubs_mgmt_op(live_eventhub_config):
    uri = "sb://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    sas_auth = authentication.SASTokenAuth.from_shared_access_key(
        uri, live_eventhub_config['key_name'], live_eventhub_config['access_key'])

    target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    with uamqp.AMQPClient(target, auth=sas_auth, debug=False) as send_client:
        mgmt_msg = uamqp.Message(application_properties={'name': live_eventhub_config['event_hub']})
        response = send_client.mgmt_request(
            mgmt_msg,
            b'READ',
            op_type=b'com.microsoft:eventhub',
            status_code_field=b'status-code',
            description_fields=b'status-description')
        output = response.get_data()
        assert output[b'partition_ids'] == [b"0", b"1"]
async def test_iot_hub_send_async(live_iothub_config):
    msg_content = b"hello world"
    msg_props = uamqp.message.MessageProperties()
    msg_props.to = '/devices/{}/messages/devicebound'.format(live_iothub_config['device'])
    msg_props.message_id = str(uuid4())
    message = uamqp.Message(msg_content, properties=msg_props)

    operation = '/messages/devicebound'
    endpoint = _build_iothub_amqp_endpoint_from_target(live_iothub_config)

    target = 'amqps://' + endpoint + operation
    log.info("Target: {}".format(target))

    send_client = uamqp.SendClientAsync(target, debug=False)
    send_client.queue_message(message)
    results = await send_client.send_all_messages_async()
    assert not [m for m in results if m == uamqp.constants.MessageState.SendFailed]
Пример #23
0
async def test_event_hubs_client_send_multiple_async(live_eventhub_config):
    properties = {b"SendData": b"Property_String_Value_1"}
    msg_content = b"hello world"
    plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key'])
    assert not plain_auth.consumed

    target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    send_client = uamqp.SendClientAsync(target, auth=plain_auth, debug=False)
    messages = []
    for i in range(10):
        messages.append(uamqp.Message(msg_content, application_properties=properties))
    send_client.queue_message(*messages)
    assert len(send_client.pending_messages) == 10
    results = await send_client.send_all_messages_async()
    assert len(results) == 10
    assert not [m for m in results if m == uamqp.constants.MessageState.SendFailed]
    assert plain_auth.consumed
    assert send_client.pending_messages == []
 def run(self):
     while self.running:
         if not self.use_batch:
             msg_content = b"Hello world"
             msg = uamqp.Message(msg_content)
             self.send_client.queue_message(msg)
             results = self.send_client.send_all_messages(
                 close_on_done=False)
             assert not [m for m in results if (
                 m == uamqp.constants.MessageState.SendFailed)]
             self.rate.increment()
         else:
             message_batch = uamqp.message.BatchMessage(
                 self.data_generator())
             self.send_client.queue_message(message_batch)
             results = self.send_client.send_all_messages(
                 close_on_done=False)
             assert not [m for m in results if (
                 m == uamqp.constants.MessageState.SendFailed)]
Пример #25
0
async def query_meta_data(endpoint, path, auth):
    source = uamqp.address.Source(endpoint)
    receive_client = uamqp.ReceiveClientAsync(source, auth=auth, timeout=30000, debug=DEBUG)
    try:
        await receive_client.open_async()
        message = uamqp.Message(application_properties={'name': path})

        response = await receive_client.mgmt_request_async(
            message,
            b'READ',
            op_type=b'com.microsoft:eventhub',
            status_code_field=b'status-code',
            description_fields=b'status-description',
            timeout=30000
        )
        test = response.get_data()
        return test
    finally:
        await receive_client.close_async()
Пример #26
0
def test_iot_hub_send(live_iothub_config):
    msg_content = b"hello world"
    app_properties = {"test_prop_1": "value", "test_prop_2": "X"}
    msg_props = uamqp.message.MessageProperties()
    msg_props.to = '/devices/{}/messages/devicebound'.format(live_iothub_config['device'])
    msg_props.message_id = str(uuid4())
    message = uamqp.Message(msg_content, properties=msg_props, application_properties=app_properties)

    operation = '/messages/devicebound'
    endpoint = _build_iothub_amqp_endpoint_from_target(live_iothub_config)

    target = 'amqps://' + endpoint + operation
    log.info("Target: {}".format(target))

    send_client = uamqp.SendClient(target, debug=True)
    send_client.queue_message(message)
    results = send_client.send_all_messages()
    assert not [m for m in results if m == uamqp.constants.MessageState.SendFailed]
    log.info("Message sent.")
    def send_message_to_device(self, device_id, message):
        """Send a message to the specified deivce.

        :param str device_id: The name (Id) of the device.
        :param str message: The message that is to be delivered to the device.

        :raises: Exception if the Send command is not able to send the message
        """
        msg_content = message
        app_properties = {}
        msg_props = uamqp.message.MessageProperties()
        msg_props.to = "/devices/{}/messages/devicebound".format(device_id)
        msg_props.message_id = str(uuid4())
        message = uamqp.Message(msg_content,
                                properties=msg_props,
                                application_properties=app_properties)
        self.amqp_client.queue_message(message)
        results = self.amqp_client.send_all_messages(close_on_done=False)
        if uamqp.constants.MessageState.SendFailed in results:
            raise Exception("C2D message sned failure")
    def send_message_to_device(self, device_id, message, app_props):
        """Send a message to the specified deivce.

        :param str device_id: The name (Id) of the device.
        :param str message: The message that is to be delivered to the device.
        :param dict app_props: Application and system properties for the message

        :raises: Exception if the Send command is not able to send the message
        """
        msg_content = message
        msg_props = uamqp.message.MessageProperties()
        msg_props.message_id = str(uuid4())
        msg_props.to = "/devices/{}/messages/devicebound".format(device_id)

        app_properties = {}

        # loop through all properties and pull out the custom
        # properties
        for prop_key, prop_value in app_props.items():
            if prop_key == "contentType":
                msg_props.content_type = prop_value
            elif prop_key == "contentEncoding":
                msg_props.content_encoding = prop_value
            elif prop_key == "correlationId":
                msg_props.correlation_id = prop_value
            elif prop_key == "expiryTimeUtc":
                msg_props.absolute_expiry_time = prop_value
            elif prop_key == "messageId":
                msg_props.message_id = prop_value
            elif prop_key == "userId":
                msg_props.user_id = prop_value
            else:
                app_properties[prop_key] = prop_value

        message = uamqp.Message(msg_content,
                                properties=msg_props,
                                application_properties=app_properties)
        self.amqp_client.queue_message(message)
        results = self.amqp_client.send_all_messages(close_on_done=False)
        if uamqp.constants.MessageState.SendFailed in results:
            raise Exception("C2D message send failure")
    async def _mgmt_request_response(self, mgmt_operation, message, callback, **kwargs):
        await self._open()
        if not self._running:
            raise InvalidHandlerState("Client connection is closed.")

        mgmt_msg = uamqp.Message(
            body=message,
            properties=MessageProperties(
                reply_to=self._mgmt_target,
                encoding=self._config.encoding,
                **kwargs))
        try:
            return await self._handler.mgmt_request_async(
                mgmt_msg,
                mgmt_operation,
                op_type=MGMT_REQUEST_OP_TYPE_ENTITY_MGMT,
                node=self._mgmt_target.encode(self._config.encoding),
                timeout=5000,
                callback=callback)
        except Exception as exp:  # pylint: disable=broad-except
            raise ServiceBusError("Management request failed: {}".format(exp), exp)
Пример #30
0
async def query_meta_data(address, path, auth):
    source = uamqp.address.Source(address)
    receive_client = uamqp.ReceiveClientAsync(source,
                                              auth=auth,
                                              timeout=30000,
                                              debug=DEBUG)
    try:
        await receive_client.open_async()
        message = uamqp.Message(application_properties={"name": path})

        response = await receive_client.mgmt_request_async(
            message,
            b"READ",
            op_type=b"com.microsoft:eventhub",
            status_code_field=b"status-code",
            description_fields=b"status-description",
            timeout=30000,
        )
        test = response.get_data()
        return test
    finally:
        await receive_client.close_async()