async def test_client_azure_sas_credential_async(live_eventhub):
    # This should "just work" to validate known-good.
    hostname = live_eventhub['hostname']
    producer_client = EventHubProducerClient.from_connection_string(
        live_eventhub['connection_str'],
        eventhub_name=live_eventhub['event_hub'])

    async with producer_client:
        batch = await producer_client.create_batch(partition_id='0')
        batch.add(EventData(body='A single message'))
        await producer_client.send_batch(batch)

    credential = EventHubSharedKeyCredential(live_eventhub['key_name'],
                                             live_eventhub['access_key'])
    auth_uri = "sb://{}/{}".format(hostname, live_eventhub['event_hub'])
    token = (await credential.get_token(auth_uri)).token.decode()
    producer_client = EventHubProducerClient(
        fully_qualified_namespace=hostname,
        eventhub_name=live_eventhub['event_hub'],
        credential=AzureSasCredential(token))

    async with producer_client:
        batch = await producer_client.create_batch(partition_id='0')
        batch.add(EventData(body='A single message'))
        await producer_client.send_batch(batch)
Beispiel #2
0
 def _get_client(self):
     """Get a Event Producer Client."""
     if self._conn_str_client:
         return EventHubProducerClient.from_connection_string(
             **self._client_args, **ADDITIONAL_ARGS
         )
     return EventHubProducerClient(**self._client_args, **ADDITIONAL_ARGS)
def create_async_eventhub_producer_client():
    # [START create_eventhub_producer_client_from_conn_str_async]
    import os
    from azure.eventhub.aio import EventHubProducerClient
    event_hub_connection_str = os.environ['EVENT_HUB_CONN_STR']
    event_hub = os.environ['EVENT_HUB_NAME']
    producer = EventHubProducerClient.from_connection_string(conn_str=event_hub_connection_str,
                                                             event_hub_path=event_hub)
    # [END create_eventhub_producer_client_from_conn_str_async]

    # [START create_eventhub_producer_client_async]
    import os
    from azure.eventhub import EventHubSharedKeyCredential
    from azure.eventhub.aio import EventHubProducerClient

    hostname = os.environ['EVENT_HUB_HOSTNAME']
    event_hub = os.environ['EVENT_HUB_NAME']
    shared_access_policy = os.environ['EVENT_HUB_SAS_POLICY']
    shared_access_key = os.environ['EVENT_HUB_SAS_KEY']

    producer = EventHubProducerClient(host=hostname,
                                      event_hub_path=event_hub,
                                      credential=EventHubSharedKeyCredential(shared_access_policy, shared_access_key))
    # [END create_eventhub_producer_client_async]
    return producer
def example_create_async_eventhub_producer_client():
    # [START create_eventhub_producer_client_from_conn_str_async]
    import os
    from azure.eventhub.aio import EventHubProducerClient
    event_hub_connection_str = os.environ['EVENT_HUB_CONN_STR']
    eventhub_name = os.environ['EVENT_HUB_NAME']
    producer = EventHubProducerClient.from_connection_string(
        conn_str=event_hub_connection_str,
        eventhub_name=
        eventhub_name  # EventHub name should be specified if it doesn't show up in connection string.
    )
    # [END create_eventhub_producer_client_from_conn_str_async]

    # [START create_eventhub_producer_client_async]
    import os
    from azure.eventhub.aio import EventHubProducerClient, EventHubSharedKeyCredential

    fully_qualified_namespace = os.environ['EVENT_HUB_HOSTNAME']
    eventhub_name = os.environ['EVENT_HUB_NAME']
    shared_access_policy = os.environ['EVENT_HUB_SAS_POLICY']
    shared_access_key = os.environ['EVENT_HUB_SAS_KEY']

    producer = EventHubProducerClient(
        fully_qualified_namespace=fully_qualified_namespace,
        eventhub_name=eventhub_name,
        credential=EventHubSharedKeyCredential(shared_access_policy,
                                               shared_access_key))
    # [END create_eventhub_producer_client_async]
    return producer
    async def test_client_azure_sas_credential_async(
            self, eventhub, eventhub_namespace, eventhub_namespace_key_name,
            eventhub_namespace_primary_key,
            eventhub_namespace_connection_string, **kwargs):
        # This should "just work" to validate known-good.
        hostname = "{}.servicebus.windows.net".format(eventhub_namespace.name)
        producer_client = EventHubProducerClient.from_connection_string(
            eventhub_namespace_connection_string, eventhub_name=eventhub.name)

        async with producer_client:
            batch = await producer_client.create_batch(partition_id='0')
            batch.add(EventData(body='A single message'))
            await producer_client.send_batch(batch)

        hostname = "{}.servicebus.windows.net".format(eventhub_namespace.name)
        auth_uri = "sb://{}/{}".format(hostname, eventhub.name)
        token = (await credential.get_token(auth_uri)).token
        producer_client = EventHubProducerClient(
            fully_qualified_namespace=hostname,
            eventhub_name=eventhub.name,
            credential=AzureSasCredential(token))

        async with producer_client:
            batch = await producer_client.create_batch(partition_id='0')
            batch.add(EventData(body='A single message'))
            await producer_client.send_batch(batch)
Beispiel #6
0
async def _run(params):
    """
        Runs the application.

        Args:
            params: A dictionary containing the following:
                connection_string: The event hub namespace connection string.
                name: The event hub name.
                interval: The number of seconds to wait between message sends.
                what_if: When true does not send the message to the event hub.
                    Instead it prints what would have been sent.
    """

    producer = EventHubProducerClient.from_connection_string(
        params["connection_string"], eventhub_name=params["name"])

    async with producer:
        for index in count(1000):
            event_data = _create_event_data(index)

            if params["what_if"]:
                print(event_data)
            else:
                await _send_message(producer, event_data)

                _print_send_status(event_data)

            time.sleep(params["interval"])
Beispiel #7
0
 async def accummulate(self):
     producer = EventHubProducerClient.from_connection_string(conn_str=self._config['Azure']['ConneStringEventHub'],\
                                                             eventhub_name=self._config['Azure']['eventhub_name'])
     async with producer:
         event_data_batch = await producer.create_batch()
         event_data_batch.add(EventData(self._json_data))
         await producer.send_batch(event_data_batch)
Beispiel #8
0
async def test_send_batch_pid_pk_async(invalid_hostname, partition_id, partition_key):
    # Use invalid_hostname because this is not a live test.
    client = EventHubProducerClient.from_connection_string(invalid_hostname)
    batch = EventDataBatch(partition_id=partition_id, partition_key=partition_key)
    async with client:
        with pytest.raises(TypeError):
            await client.send_batch(batch, partition_id=partition_id, partition_key=partition_key)
Beispiel #9
0
async def test_client_secret_credential_async(aad_credential, live_eventhub):
    try:
        from azure.identity.aio import EnvironmentCredential
    except ImportError:
        pytest.skip("No azure identity library")

    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(host=live_eventhub['hostname'],
                                             event_hub_path=live_eventhub['event_hub'],
                                             credential=credential,
                                             user_agent='customized information')
    consumer_client = EventHubConsumerClient(host=live_eventhub['hostname'],
                                             event_hub_path=live_eventhub['event_hub'],
                                             credential=credential,
                                             user_agent='customized information')

    async with producer_client:
        await producer_client.send(EventData(body='A single message'))

    async def event_handler(partition_context, events):
        assert partition_context.partition_id == '0'
        assert len(events) == 1
        assert list(events[0].body)[0] == 'A single message'.encode('utf-8')

    async with consumer_client:
        task = asyncio.ensure_future(
            consumer_client.receive(event_handler=event_handler, consumer_group='$default', partition_id='0'))
        await asyncio.sleep(2)
        task.cancel()
Beispiel #10
0
 def client(self) -> EventHubProducerClient:
     """Return the client."""
     return EventHubProducerClient.from_connection_string(
         conn_str=self.event_hub_connection_string,
         eventhub_name=self.event_hub_instance_name,
         **ADDITIONAL_ARGS,
     )
async def main():
    # Create an Azure blob checkpoint store to store the checkpoints.
    checkpoint_store = BlobCheckpointStore.from_connection_string(checkpoint_conn_str, checkpoint_container_name)

    # Create a consumer client for the event hub.
    client = EventHubConsumerClient.from_connection_string(conn_str, consumer_group="$Default", eventhub_name=event_hub_name, checkpoint_store=checkpoint_store)

    client_future = client.receive(on_event=on_event, starting_position=-1)
    print('client is listening')

    producer = EventHubProducerClient.from_connection_string(conn_str=conn_str, eventhub_name=event_hub_name)
    async with producer:
        # Create a batch.
        event_data_batch = await producer.create_batch()

        # Add events to the batch.
        event_data_batch.add(EventData('First event '))
        event_data_batch.add(EventData('Second event'))
        event_data_batch.add(EventData('Third event'))

        # Send the batch of events to the event hub.
        await producer.send_batch(event_data_batch)
        print('sent events')

    await client_future
async def example_eventhub_async_producer_send_and_close():
    # [START eventhub_producer_client_close_async]
    import os
    from azure.eventhub.aio import EventHubProducerClient
    from azure.eventhub import EventData

    event_hub_connection_str = os.environ['EVENT_HUB_CONN_STR']
    eventhub_name = os.environ['EVENT_HUB_NAME']

    producer = EventHubProducerClient.from_connection_string(
        conn_str=event_hub_connection_str,
        eventhub_name=
        eventhub_name  # EventHub name should be specified if it doesn't show up in connection string.
    )
    try:
        event_data_batch = await producer.create_batch()
        while True:
            try:
                event_data_batch.add(
                    EventData('Message inside EventBatchData'))
            except ValueError:
                # The EventDataBatch object reaches its max_size.
                # You can send the full EventDataBatch object and create a new one here.
                break
        await producer.send_batch(event_data_batch)
    finally:
        # Close down the producer handler.
        await producer.close()
Beispiel #13
0
async def test_send_with_invalid_policy_async(invalid_policy):
    client = EventHubProducerClient.from_connection_string(invalid_policy)
    async with client:
        with pytest.raises(ConnectError):
            batch = EventDataBatch()
            batch.add(EventData("test data"))
            await client.send_batch(batch)
Beispiel #14
0
async def run(text):
    producer = EventHubProducerClient.from_connection_string(
        conn_str=connection_string, eventhub_name=event_hub_name)
    async with producer:
        event_data_batch = await producer.create_batch()
        event_data_batch.add(EventData(text))
        await producer.send_batch(event_data_batch)
 def __init__(self, arguments):
     super().__init__(arguments)
     connection_string = self.get_from_env("AZURE_EVENTHUB_CONNECTION_STRING")
     eventhub_name = self.get_from_env("AZURE_EVENTHUB_NAME")
     self.async_producer = AsyncEventHubProducerClient.from_connection_string(connection_string, eventhub_name=eventhub_name)
     self.consumer = EventHubConsumerClient.from_connection_string(connection_string, _EventHubTest.consumer_group, eventhub_name=eventhub_name)
     self.async_consumer = AsyncEventHubConsumerClient.from_connection_string(connection_string, _EventHubTest.consumer_group, eventhub_name=eventhub_name)
Beispiel #16
0
async def test_send_connection_idle_timeout_and_reconnect_async(
        connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubProducerClient.from_connection_string(
        conn_str=connection_str, idle_timeout=10)
    async with client:
        ed = EventData('data')
        sender = client._create_producer(partition_id='0')
        async with sender:
            await sender._open_with_retry()
            time.sleep(11)
            sender._unsent_events = [ed.message]
            ed.message.on_send_complete = sender._on_outcome
            with pytest.raises(
                (uamqp.errors.ConnectionClose,
                 uamqp.errors.MessageHandlerError, OperationTimeoutError)):
                # Mac may raise OperationTimeoutError or MessageHandlerError
                await sender._send_event_data()
            await sender._send_event_data_with_retry()
    retry = 0
    while retry < 3:
        try:
            messages = receivers[0].receive_message_batch(max_batch_size=10,
                                                          timeout=10000)
            if messages:
                received_ed1 = EventData._from_message(messages[0])
                assert received_ed1.body_as_str() == 'data'
                break
        except compat.TimeoutException:
            retry += 1
Beispiel #17
0
async def run():
 parser = argparse.ArgumentParser()
 parser.add_argument('--EVENTHUB_NAME', dest='EVENTHUB_NAME')
 parser.add_argument('--EVENTHUB_CONN_STR', dest='EVENTHUB_CONN_STR')
 args = parser.parse_args()
 #print(args)
 EVENTHUB_CONN_STR=args.EVENTHUB_CONN_STR
 EVENTHUB_NAME=args.EVENTHUB_NAME
 producer = EventHubProducerClient.from_connection_string(conn_str=EVENTHUB_CONN_STR, eventhub_name=EVENTHUB_NAME)
 async with producer:
 # Create a batch.
  event_data_batch = await producer.create_batch()
  terminalvoltage = round(random.uniform(93.5, 96.5), 2)
  #print(terminalvoltage)
  batterypack_timestamp = calendar.timegm(datetime.utcnow().timetuple())
  batterypacks = ['Scooter4']
  batterypack = random.choice(batterypacks)
  payload = {
                'id'         : 1,
                'eventType'  : 'battery',
                'subject'    : 'iot/batterysensors',
                'eventTime'  : str(batterypack_timestamp),
                'data'       : { 'Timestamp' : batterypack_timestamp, 'Battery Pack': batterypack, 'Terminal Voltage': terminalvoltage,
                                 'Charging Current' : 0, 'Discharging Current' : 0, 'SoC' : 1.6, 'Charge Capacity' : 0,
                                 'Charging Power' : 0, 'Discharging Power' : 0, 'Cycle Count' : 0
                               },
                'dataVersion': '1.0'
              }
 # Add events to the batch.
  event_data_batch.add(EventData(json.dumps(payload)))
 # Send the batch of events to the event hub.
  await producer.send_batch(event_data_batch)
async def test_send_multiple_partition_with_app_prop_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    app_prop_key = "raw_prop"
    app_prop_value = "raw_value"
    app_prop = {app_prop_key: app_prop_value}
    client = EventHubProducerClient.from_connection_string(connection_str)
    async with client:
        ed0 = EventData(b"Message 0")
        ed0.properties = app_prop
        batch = await client.create_batch(partition_id="0")
        batch.add(ed0)
        await client.send_batch(batch)

        ed1 = EventData(b"Message 1")
        ed1.properties = app_prop
        batch = await client.create_batch(partition_id="1")
        batch.add(ed1)
        await client.send_batch(batch)

    partition_0 = [EventData._from_message(x) for x in receivers[0].receive_message_batch(timeout=5000)]
    assert len(partition_0) == 1
    assert partition_0[0].properties[b"raw_prop"] == b"raw_value"
    partition_1 = [EventData._from_message(x) for x in receivers[1].receive_message_batch(timeout=5000)]
    assert len(partition_1) == 1
    assert partition_1[0].properties[b"raw_prop"] == b"raw_value"
Beispiel #19
0
 async def create_producer(self):
     self.producer = \
         EventHubProducerClient.from_connection_string(
             conn_str=self.namespace_conn_string,
             eventhub_name=self.topic,
         )
     await asyncio.sleep(0.01)
Beispiel #20
0
    async def run(self):
        # Create a producer client to send messages to the event hub.
        # Specify a connection string to your event hubs namespace and
        # the event hub name.
        producer = EventHubProducerClient.from_connection_string(
            conn_str=self.CONNECTION_STRING, eventhub_name=self.EVENTHUB_NAME)
        async with producer:
            # Create a batch.
            event_data_batch = await producer.create_batch()

            # Add events to the batch.
            event_data_batch.add(
                EventData(
                    json.dumps({
                        'user': self.YOUR_NAME,
                        'timestamp': str(datetime.now()),
                        'message': 'Robert,ABP'
                    })))
            event_data_batch.add(
                EventData(
                    json.dumps({
                        'user': self.YOUR_NAME,
                        'timestamp': str(datetime.now()),
                        'message': 'Els,ABP'
                    })))

            # Send the batch of events to the event hub.
            await producer.send_batch(event_data_batch)
async def test_client_secret_credential_async(live_eventhub):
    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        credential=credential,
        user_agent='customized information')
    consumer_client = EventHubConsumerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        consumer_group='$default',
        credential=credential,
        user_agent='customized information')

    async with producer_client:
        batch = await producer_client.create_batch(partition_id='0')
        batch.add(EventData(body='A single message'))
        await producer_client.send_batch(batch)

    def on_event(partition_context, event):
        on_event.called = True
        on_event.partition_id = partition_context.partition_id
        on_event.event = event

    on_event.called = False
    async with consumer_client:
        task = asyncio.ensure_future(
            consumer_client.receive(on_event,
                                    partition_id='0',
                                    starting_position='-1'))
        await asyncio.sleep(13)
    await task
    assert on_event.called is True
    assert on_event.partition_id == "0"
    assert list(on_event.event.body)[0] == 'A single message'.encode('utf-8')
Beispiel #22
0
async def test_send_partition_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubProducerClient.from_connection_string(connection_str)

    async with client:
        batch = await client.create_batch()
        batch.add(EventData(b"Data"))
        await client.send_batch(batch)

    async with client:
        batch = await client.create_batch(partition_id="1")
        batch.add(EventData(b"Data"))
        await client.send_batch(batch)

    partition_0 = receivers[0].receive_message_batch(timeout=5000)
    partition_1 = receivers[1].receive_message_batch(timeout=5000)
    assert len(partition_0) + len(partition_1) == 2

    async with client:
        batch = await client.create_batch()
        batch.add(EventData(b"Data"))
        await client.send_batch(batch)

    async with client:
        batch = await client.create_batch(partition_id="1")
        batch.add(EventData(b"Data"))
        await client.send_batch(batch)

    partition_0 = receivers[0].receive_message_batch(timeout=5000)
    partition_1 = receivers[1].receive_message_batch(timeout=5000)
    assert len(partition_0) + len(partition_1) == 2
Beispiel #23
0
async def test_send_with_partition_key_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubProducerClient.from_connection_string(connection_str)
    async with client:
        data_val = 0
        for partition in [b"a", b"b", b"c", b"d", b"e", b"f"]:
            partition_key = b"test_partition_" + partition
            for i in range(50):
                batch = await client.create_batch(partition_key=partition_key)
                batch.add(EventData(str(data_val)))
                data_val += 1
                await client.send_batch(batch)

        await client.send_batch(await client.create_batch())

    found_partition_keys = {}
    for index, partition in enumerate(receivers):
        received = partition.receive_message_batch(timeout=5000)
        for message in received:
            try:
                event_data = EventData._from_message(message)
                existing = found_partition_keys[event_data.partition_key]
                assert existing == index
            except KeyError:
                found_partition_keys[event_data.partition_key] = index
Beispiel #24
0
async def main():
    consumer_client = EventHubConsumerClient.from_connection_string(
        conn_str=CONNECTION_STR,
        consumer_group='$Default',
        eventhub_name=EVENTHUB_NAME,
        http_proxy=HTTP_PROXY)

    producer_client = EventHubProducerClient.from_connection_string(
        conn_str=CONNECTION_STR,
        eventhub_name=EVENTHUB_NAME,
        http_proxy=HTTP_PROXY)

    async with producer_client:
        event_data_batch = await producer_client.create_batch(
            max_size_in_bytes=10000)
        while True:
            try:
                event_data_batch.add(
                    EventData('Message inside EventBatchData'))
            except ValueError:
                # EventDataBatch object reaches max_size.
                # New EventDataBatch object can be created here to send more data.
                break
        await producer_client.send_batch(event_data_batch)
        print('Finished sending.')

    async with consumer_client:
        await consumer_client.receive(on_event=on_event)
        print('Finished receiving.')
async def run():

    faker = Faker()

    eventhub_connection_STR = os.getenv('EVENT_HUB_CONN_STR')

    eventhub_name = os.getenv('EVENT_HUB_NAME')

    print(eventhub_connection_STR)
    print(eventhub_name)

    producer = EventHubProducerClient.from_connection_string(
        conn_str=eventhub_connection_STR, eventhub_name=eventhub_name)
    async with producer:
        # Create a batch.
        event_data_batch = await producer.create_batch()

        # Add events to the batch.
        event_data_batch.add(EventData(faker.name()))
        event_data_batch.add(EventData(faker.name()))
        #event_data_batch.add(EventData(faker.simple_profile()))

        # Send the batch of events to the event hub.
        while (True):
            await producer.send_batch(event_data_batch)
Beispiel #26
0
async def test_send_null_body_async(connection_str):
    client = EventHubProducerClient.from_connection_string(connection_str)
    try:
        with pytest.raises(ValueError):
            data = EventData(None)
            await client.send(data)
    finally:
        await client.close()
Beispiel #27
0
async def test_non_existing_entity_sender_async(connection_str):
    client = EventHubProducerClient.from_connection_string(
        connection_str, eventhub_name="nemo")
    async with client:
        with pytest.raises(ConnectError):
            batch = EventDataBatch()
            batch.add(EventData("test data"))
            await client.send_batch(batch)
Beispiel #28
0
async def test_send_to_invalid_partitions_async(connection_str):
    partitions = ["XYZ", "-1", "1000", "-"]
    for p in partitions:
        client = EventHubProducerClient.from_connection_string(connection_str)
        try:
            with pytest.raises(ConnectError):
                await client.send(EventData("test data"), partition_id=p)
        finally:
            await client.close()
Beispiel #29
0
async def test_send_list_partition_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubProducerClient.from_connection_string(connection_str)
    payload = "A1"
    async with client:
        await client.send_batch([EventData(payload)], partition_id="0")
        message = receivers[0].receive_message_batch(timeout=10000)[0]
        received = EventData._from_message(message)
    assert received.body_as_str() == payload
Beispiel #30
0
async def test_create_batch_with_invalid_hostname_async(invalid_hostname):
    if sys.platform.startswith('darwin'):
        pytest.skip(
            "Skipping on OSX - it keeps reporting 'Unable to set external certificates' "
            "and blocking other tests")
    client = EventHubProducerClient.from_connection_string(invalid_hostname)
    async with client:
        with pytest.raises(ConnectError):
            await client.create_batch(max_size_in_bytes=300)