def publish_event():
    # authenticate client
    credential = AzureKeyCredential(key)
    client = EventGridPublisherClient(endpoint, credential)

    custom_schema_event = {
        "customSubject": "sample",
        "customEventType": "sample.event",
        "customDataVersion": "2.0",
        "customId": uuid.uuid4(),
        "customEventTime": dt.datetime.now(UTC()).isoformat(),
        "customData": "sample data"
    }

    # 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)):
            event = CustomEvent(custom_schema_event)
            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))
 def test_send_cloud_event_data_bytes(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=b"cloudevent",
                              type="Sample.Cloud.Event")
     client.send_events(cloud_event)
 def test_send_event_grid_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)
     eg_event = EventGridEvent(subject="sample",
                               data=u"eventgridevent",
                               event_type="Sample.EventGrid.Event",
                               data_version="2.0")
     client.send_events(eg_event)
 def test_send_event_grid_event_data_bytes(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)
     eg_event = EventGridEvent(subject="sample",
                               data=b"eventgridevent",
                               event_type="Sample.EventGrid.Event",
                               data_version="2.0")
     with pytest.raises(TypeError,
                        match="Data in EventGridEvent cannot be bytes*"):
         client.send_events(eg_event)
 def test_send_signature_credential(self, resource_group, eventgrid_topic,
                                    eventgrid_topic_primary_key,
                                    eventgrid_topic_endpoint):
     expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
     signature = generate_shared_access_signature(
         eventgrid_topic_endpoint, eventgrid_topic_primary_key,
         expiration_date_utc)
     credential = AzureSasCredential(signature)
     client = EventGridPublisherClient(eventgrid_topic_endpoint, credential)
     eg_event = EventGridEvent(subject="sample",
                               data={"sample": "eventgridevent"},
                               event_type="Sample.EventGrid.Event",
                               data_version="2.0")
     client.send_events(eg_event)
 def test_send_cloud_event_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_event1 = {
         "id": "1234",
         "source": "http://samplesource.dev",
         "specversion": "1.0",
         "data": "cloudevent",
         "type": "Sample.Cloud.Event"
     }
     client.send_events(cloud_event1)
 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_events([cloud_event])
     internal = cloud_event._to_generated().serialize()
     assert 'reason_code' in internal
     assert 'extension' in internal
     assert internal['reason_code'] == 204
 def test_send_custom_schema_event(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)
     custom_event = CustomEvent({
         "customSubject":
         "sample",
         "customEventType":
         "sample.event",
         "customDataVersion":
         "2.0",
         "customId":
         "1234",
         "customEventTime":
         dt.datetime.now(UTC()).isoformat(),
         "customData":
         "sample data"
     })
     client.send_events(custom_event)
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: cs5_publish_events_using_cloud_events_1.0_schema.py
DESCRIPTION:
    These samples demonstrate creating a list of CloudEvents and sending then as a list.
USAGE:
    python cs5_publish_events_using_cloud_events_1.0_schema.py
    Set the environment variables with your own values before running the sample:
    1) CLOUD_ACCESS_KEY - The access key of your eventgrid account.
    2) CLOUD_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
    "<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""
import os
from azure.eventgrid import EventGridPublisherClient, CloudEvent
from azure.core.credentials import AzureKeyCredential

topic_key = os.environ["CLOUD_ACCESS_KEY"]
endpoint = os.environ["CLOUD_TOPIC_HOSTNAME"]

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

client.send_events([
    CloudEvent(type="Contoso.Items.ItemReceived",
               source="/contoso/items",
               data={"itemSku": "Contoso Item SKU #1"},
               subject="Door1")
])
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: cs1_publish_custom_events_to_a_topic.py
DESCRIPTION:
    These samples demonstrate sending an EventGrid Event.
USAGE:
    python cs1_publish_custom_events_to_a_topic.py
    Set the environment variables with your own values before running the sample:
    1) EG_ACCESS_KEY - The access key of your eventgrid account.
    2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
    "<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""
import os
from azure.eventgrid import EventGridPublisherClient, EventGridEvent, CloudEvent
from azure.core.credentials import AzureKeyCredential

topic_key = os.environ["EG_ACCESS_KEY"]
endpoint = os.environ["EG_TOPIC_HOSTNAME"]

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

client.send_events([
    EventGridEvent(event_type="Contoso.Items.ItemReceived",
                   data={"itemSku": "Contoso Item SKU #1"},
                   subject="Door1",
                   data_version="2.0")
])
    These samples demonstrate creating a list of EventGrid Events and sending them as a list.
USAGE:
    python cs2_publish_custom_events_to_a_domain_topic.py
    Set the environment variables with your own values before running the sample:
    1) EG_ACCESS_KEY - The access key of your eventgrid account.
    2) EG_TOPIC_HOSTNAME - The topic hostname. Typically it exists in the format
    "<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net".
"""
import os
from azure.eventgrid import EventGridPublisherClient, EventGridEvent
from azure.core.credentials import AzureKeyCredential

domain_key = os.environ["EG_DOMAIN_ACCESS_KEY"]
domain_hostname = os.environ["EG_DOMAIN_TOPIC_HOSTNAME"]

credential = AzureKeyCredential(domain_key)
client = EventGridPublisherClient(domain_hostname, credential)

client.send_events([
    EventGridEvent(topic="MyCustomDomainTopic1",
                   event_type="Contoso.Items.ItemReceived",
                   data={"itemSku": "Contoso Item SKU #1"},
                   subject="Door1",
                   data_version="2.0"),
    EventGridEvent(topic="MyCustomDomainTopic2",
                   event_type="Contoso.Items.ItemReceived",
                   data={"itemSku": "Contoso Item SKU #2"},
                   subject="Door1",
                   data_version="2.0")
])