def test_cloud_event_serialization_extension_bytes(self, **kwargs):
        data = b"cloudevent"
        cloud_event = CloudEvent(
                source="http://samplesource.dev",
                data=data,
                type="Sample.Cloud.Event",
                foo="bar",
                extensions={'e1':1, 'e2':2}
                )
        
        cloud_event.subject = "subject" # to test explicit setting of prop
        encoded = base64.b64encode(data).decode('utf-8')
        internal = cloud_event._to_generated()

        assert internal.additional_properties is not None
        assert 'foo' not in internal.additional_properties
        assert 'e1' in internal.additional_properties

        json  = internal.serialize()

        expected = {
            'source':'http://samplesource.dev',
            'data_base64': encoded,
            'type':'Sample.Cloud.Event',
            'reason_code':204,
            'e1':1,
            'e2':2
        }

        self._assert_cloud_event_serialized(expected, json)
        assert expected['data_base64'] == json['data_base64']
 def test_send_cloud_event_data_with_extensions(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
     akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
     client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
     cloud_event = CloudEvent(
             source = "http://samplesource.dev",
             data = "cloudevent",
             type="Sample.Cloud.Event",
             extensions={
                 'reason_code':204,
                 'extension':'hello'
                 }
             )
     client.send([cloud_event])
     internal = cloud_event._to_generated().serialize()
     assert 'reason_code' in internal
     assert 'extension' in internal
     assert internal['reason_code'] == 204
Пример #3
0
 def test_send_cloud_event_fails_on_providing_data_and_b64(self):
     with pytest.raises(
             ValueError,
             match=
             "data and data_base64 cannot be provided at the same time*"):
         cloud_event = CloudEvent(source="http://samplesource.dev",
                                  data_base64=b'cloudevent',
                                  data="random data",
                                  type="Sample.Cloud.Event")
Пример #4
0
 async def test_send_cloud_event_data_str(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
     akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
     client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
     cloud_event = CloudEvent(
             source = "http://samplesource.dev",
             data = "cloudevent",
             type="Sample.Cloud.Event"
             )
     await client.send(cloud_event)
Пример #5
0
 def test_eg_publisher_client_publish_cloud_event_data_dict(
         self, resource_group, eventgrid_topic, eventgrid_topic_primary_key,
         eventgrid_topic_endpoint):
     akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
     client = EventGridPublisherClient(eventgrid_topic_endpoint,
                                       akc_credential)
     cloud_event = CloudEvent(source="http://samplesource.dev",
                              data={"sample": "cloudevent"},
                              type="Sample.Cloud.Event")
     client.send([cloud_event])
Пример #6
0
async def publish():
    credential = AzureKeyCredential(topic_key)
    client = EventGridPublisherClient(endpoint, credential)

    client.send([
        CloudEvent(type="Contoso.Items.ItemReceived",
                   source="/contoso/items",
                   data={"itemSku": "Contoso Item SKU #1"},
                   subject="Door1")
    ])
Пример #7
0
 async def test_send_and_close_async_session(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
     akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
     client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
     async with client: # this throws if client can't close
         cloud_event = CloudEvent(
                 source = "http://samplesource.dev",
                 data = "cloudevent",
                 type="Sample.Cloud.Event"
                 )
         await client.send(cloud_event)
Пример #8
0
    def test_send_cloud_event_bytes_using_data_base64(
            self, resource_group, eventgrid_topic, eventgrid_topic_primary_key,
            eventgrid_topic_endpoint):
        akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
        client = EventGridPublisherClient(eventgrid_topic_endpoint,
                                          akc_credential)
        cloud_event = CloudEvent(source="http://samplesource.dev",
                                 data_base64=b'cloudevent',
                                 type="Sample.Cloud.Event")

        def callback(request):
            req = json.loads(request.http_request.body)
            assert req[0].get("data_base64") is not None
            assert req[0].get("data") is None

        client.send(cloud_event, raw_response_hook=callback)
def publish_event():
    # publish events
    for _ in range(3):

        event_list = []  # list of events to publish
        # create events and append to list
        for j in range(randint(1, 3)):
            sample_members = sample(services, k=randint(
                1, 4))  # select random subset of team members
            event = CloudEvent(type="Azure.Sdk.Demo",
                               source="https://egdemo.dev/demowithsignature",
                               data={"team": sample_members})
            event_list.append(event)

        # publish list of events
        client.send(event_list)
        print("Batch of size {} published".format(len(event_list)))
        time.sleep(randint(1, 5))
def publish_event():
    # publish events
    for _ in range(10):
        event_list = []  # list of events to publish
        # create events and append to list
        for j in range(randint(1, 1)):
            sample_members = sample(team_members, k=randint(
                1, 9))  # select random subset of team members
            data_dict = {"team": sample_members}
            event = CloudEvent(type="Azure.Sdk.Sample",
                               source="https://egsample.dev/sampleevent",
                               data={"team": sample_members})
            event_list.append(event)

        # publish list of events
        client.send(event_list)
        print("Batch of size {} published".format(len(event_list)))
        time.sleep(randint(1, 5))
def publish_event():
    # publish events
    for _ in range(3):

        event_list = []  # list of events to publish
        services = ["EventGrid", "ServiceBus", "EventHubs",
                    "Storage"]  # possible values for data field

        # create events and append to list
        for j in range(randint(1, 3)):
            sample_members = sample(services, k=randint(
                1, 4))  # select random subset of team members
            event = CloudEvent(type="Azure.Sdk.Demo",
                               source=domain_name,
                               data={"team": sample_members})
            event_list.append(event)

        # publish list of events
        client.send(event_list)
        print("Batch of size {} published".format(len(event_list)))
        time.sleep(randint(1, 5))
def publish_event():
    # publish events
    for _ in range(3):

        event_list = []  # list of events to publish
        team_members = [
            "Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren",
            "Srikanta", "Swathi"
        ]  # possible values for data field

        # create events and append to list
        for j in range(randint(1, 3)):
            sample_members = sample(team_members, k=randint(
                1, 9))  # select random subset of team members
            event = CloudEvent(type="Azure.Sdk.Demo",
                               source=domain_name,
                               data={"team": sample_members})
            event_list.append(event)

        # publish list of events
        client.send_events(event_list)
        print("Batch of size {} published".format(len(event_list)))
        time.sleep(randint(1, 5))
Пример #13
0
from azure.core.credentials import AzureKeyCredential
from azure.eventgrid import EventGridPublisherClient, CloudEvent

key = os.environ.get("CLOUD_ACCESS_KEY")
topic_hostname = os.environ["CLOUD_TOPIC_HOSTNAME"]

# authenticate client
credential = AzureKeyCredential(key)
client = EventGridPublisherClient(topic_hostname, credential)

team_members = ["Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta", "Swathi"]    # possible values for data field

# publish events
while True:
    event_list = []     # list of events to publish
    # create events and append to list
    for j in range(randint(1, 1)):
        sample_members = sample(team_members, k=randint(1, 9))      # select random subset of team members
        data_dict = {"team": sample_members}
        event = CloudEvent(
                type="Azure.Sdk.Sample",
                source="https://egsample.dev/sampleevent",
                data={"team": sample_members}
                )
        event_list.append(event)

    # publish list of events
    client.send(event_list)
    print("Batch of size {} published".format(len(event_list)))
    time.sleep(randint(1, 5))
FILE: consume_cloud_events_from_storage_queue.py
DESCRIPTION:
    These samples demonstrate receiving events from a Storage Queue.
USAGE:
    python consume_cloud_events_from_storage_queue.py
    Set the environment variables with your own values before running the sample:
    1) STORAGE_QUEUE_CONN_STR: The connection string to the Storage account
    3) STORAGE_QUEUE_NAME: The name of the storage queue.
"""

from azure.eventgrid import CloudEvent
from azure.storage.queue import QueueServiceClient, BinaryBase64DecodePolicy
import os
import json

# all types of CloudEvents below produce same DeserializedEvent
connection_str = os.environ['STORAGE_QUEUE_CONN_STR']
queue_name = os.environ['STORAGE_QUEUE_NAME']

with QueueServiceClient.from_connection_string(connection_str) as qsc:
    payload =  qsc.get_queue_client(
        queue=queue_name,
        message_decode_policy=BinaryBase64DecodePolicy()
        ).peek_messages()

    ## deserialize payload into a lost of typed Events
    events = [CloudEvent.from_dict(json.loads(msg.content)) for msg in payload]

    for event in events:
        print(type(event)) ## CloudEvent
Пример #15
0
domain_key = os.environ["DOMAIN_ACCESS_KEY"]
domain_topic_hostname = os.environ["DOMAIN_TOPIC_HOSTNAME"]
domain_name = os.environ["DOMAIN_NAME"]

# authenticate client
credential = AzureKeyCredential(domain_key)
client = EventGridPublisherClient(domain_topic_hostname, credential)

# publish events
while True:

    event_list = []  # list of events to publish
    team_members = [
        "Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren",
        "Srikanta", "Swathi"
    ]  # possible values for data field

    # create events and append to list
    for j in range(randint(1, 3)):
        sample_members = sample(team_members, k=randint(
            1, 9))  # select random subset of team members
        event = CloudEvent(type="Azure.Sdk.Demo",
                           source=domain_name,
                           data={"team": sample_members})
        event_list.append(event)

    # publish list of events
    client.send(event_list)
    print("Batch of size {} published".format(len(event_list)))
    time.sleep(randint(1, 5))
Пример #16
0
from azure.eventgrid import EventGridPublisherClient, CloudEvent
from azure.core.credentials import AzureKeyCredential

topic_hostname = "<YOUR-TOPIC-NAME>.<REGION-NAME>-1.eventgrid.azure.net"
topic_key = "<YOUR-TOPIC-KEY>"

credential = AzureKeyCredential(topic_key)
client = EventGridPublisherClient(topic_hostname, credential)

client.send([
    CloudEvent(type="Contoso.Items.ItemReceived",
               source="/contoso/items",
               data={"itemSku": "Contoso Item SKU #1"},
               subject="Door1")
])
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
# for details
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

# Simple console exporter
exporter = ConsoleSpanExporter()

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(exporter))

# Example with Eventgrid SDKs
import os
from azure.eventgrid import EventGridPublisherClient, CloudEvent
from azure.core.credentials import AzureKeyCredential

hostname = os.environ['CLOUD_TOPIC_HOSTNAME']
key = AzureKeyCredential(os.environ['CLOUD_ACCESS_KEY'])
cloud_event = CloudEvent(source='demo',
                         type='sdk.demo',
                         data={'test': 'hello'},
                         extensions={'test': 'maybe'})
with tracer.start_as_current_span(name="MyApplication"):
    client = EventGridPublisherClient(hostname, key)
    client.send(cloud_event)
# --------------------------------------------------------------------------
"""
FILE: sample_consume_custom_payload.py
DESCRIPTION:
    These samples demonstrate consuming a raw json with a list of jsons and
    deserializing them into typed objects of CloudEvents.
USAGE:
    python sample_consume_custom_payload.py
"""

from azure.eventgrid import CloudEvent
import json

# all types of CloudEvents below produce same DeserializedEvent
cloud_custom_dict = """[{
    "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
    "source":"https://egtest.dev/cloudcustomevent",
    "data":{
        "team": "event grid squad"
    },
    "type":"Azure.Sdk.Sample",
    "time":"2020-08-07T02:06:08.11969Z",
    "specversion":"1.0"
}]"""

deserialized_dict_events = [CloudEvent(**msg) for msg in json.loads(cloud_custom_dict)]

for event in deserialized_dict_events:
    print(event.data)
    print(type(event))
from azure.eventgrid import CloudEvent
import json

# all types of CloudEvents below produce same DeserializedEvent
cloud_custom_dict = """[{
    "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
    "source":"https://egtest.dev/cloudcustomevent",
    "data":{
        "team": "event grid squad"
    },
    "type":"Azure.Sdk.Sample",
    "time":"2020-08-07T02:06:08.11969Z",
    "specversion":"1.0"
}]"""

deserialized_dict_events = [
    CloudEvent(**msg) for msg in json.loads(cloud_custom_dict)
]

for event in deserialized_dict_events:
    print(event.data)
    print(type(event))
Пример #20
0
signature = generate_shared_access_signature(topic_hostname, key,
                                             expiration_date_utc)

# authenticate client
credential = EventGridSharedAccessSignatureCredential(signature)
client = EventGridPublisherClient(topic_hostname, credential)

team_members = [
    "Josh", "Kerri", "Kieran", "Laurent", "Lily", "Matt", "Soren", "Srikanta",
    "Swathi"
]  # possible values for data field

# publish events
while True:

    event_list = []  # list of events to publish
    # create events and append to list
    for j in range(randint(1, 3)):
        sample_members = sample(team_members, k=randint(
            1, 9))  # select random subset of team members
        event = CloudEvent(type="Azure.Sdk.Demo",
                           source="https://egdemo.dev/demowithsignature",
                           data={"team": sample_members})
        event_list.append(event)

    # publish list of events
    client.send(event_list)
    print("Batch of size {} published".format(len(event_list)))
    time.sleep(randint(1, 5))