async def publish(): credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(endpoint, credential) await client.send([ EventGridEvent(event_type="Contoso.Items.ItemReceived", data={"itemSku": "Contoso Item SKU #1"}, subject="Door1", data_version="2.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)
async 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="eventgridevent", event_type="Sample.EventGrid.Event", data_version="2.0" ) await client.send(eg_event)
async def test_send_token_credential(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint): credential = self.get_credential(EventGridPublisherClient) client = EventGridPublisherClient(eventgrid_topic_endpoint, credential) eg_event = EventGridEvent(subject="sample", data={"sample": "eventgridevent"}, event_type="Sample.EventGrid.Event", data_version="2.0") await client.send(eg_event)
async 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*"): await client.send(eg_event)
async def test_raise_on_bad_resource(self, variables, eventgrid_topic_key): akc_credential = AzureKeyCredential(eventgrid_topic_key) client = EventGridPublisherClient( "https://bad-resource.westus-1.eventgrid.azure.net/api/events", akc_credential) eg_event = EventGridEvent(subject="sample", data={"sample": "eventgridevent"}, event_type="Sample.EventGrid.Event", data_version="2.0") with pytest.raises(HttpResponseError): await client.send(eg_event)
async 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" } await client.send(cloud_event1)
async def test_send_cloud_event_data_none(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=None, type="Sample.Cloud.Event") await client.send(cloud_event)
async def test_send_event_grid_event_fails_without_full_url(self, variables, eventgrid_topic_key, eventgrid_topic_endpoint): akc_credential = AzureKeyCredential(eventgrid_topic_key) parsed_url = urlparse(eventgrid_topic_endpoint) client = EventGridPublisherClient(parsed_url.netloc, akc_credential) eg_event = EventGridEvent( subject="sample", data={"sample": "eventgridevent"}, event_type="Sample.EventGrid.Event", data_version="2.0" ) with pytest.raises(ValueError): await client.send(eg_event)
async 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 = { "customSubject": "sample", "customEventType": "sample.event", "customDataVersion": "2.0", "customId": "1234", "customEventTime": dt.datetime.now(UTC()).isoformat(), "customData": "sample data" } await client.send(custom_event)
async 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 = EventGridSharedAccessSignatureCredential(signature) client = EventGridPublisherClient(eventgrid_topic_endpoint, credential) eg_event = EventGridEvent( subject="sample", data={"sample": "eventgridevent"}, event_type="Sample.EventGrid.Event", data_version="2.0" ) await client.send(eg_event)
async def test_send_signature_credential(self, variables, eventgrid_topic_key, eventgrid_topic_endpoint): expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1) signature = generate_sas(eventgrid_topic_endpoint, eventgrid_topic_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" ) await client.send(eg_event)
async def test_send_cloud_event_data_NULL(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 = NULL, type="Sample.Cloud.Event" ) def callback(request): req = json.loads(request.http_request.body) assert req[0].get("data") is None await client.send(cloud_event, raw_request_hook=callback)
async def test_raise_on_auth_error(self, variables, eventgrid_topic_endpoint): akc_credential = AzureKeyCredential("bad credential") client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential) eg_event = EventGridEvent(subject="sample", data={"sample": "eventgridevent"}, event_type="Sample.EventGrid.Event", data_version="2.0") with pytest.raises( ClientAuthenticationError, match="The request authorization key is not authorized for*"): await client.send(eg_event)
async def test_raise_on_bad_resource(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint): akc_credential = AzureKeyCredential(eventgrid_topic_primary_key) client = EventGridPublisherClient( "https://bad-resource.westus-1.eventgrid.azure.net/api/events", akc_credential) eg_event = EventGridEvent(subject="sample", data={"sample": "eventgridevent"}, event_type="Sample.EventGrid.Event", data_version="2.0") with pytest.raises(ServiceRequestError): await client.send(eg_event)
async def test_send_event_grid_event_dict_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 = { "subject":"sample", "data":b"eventgridevent", "eventType":"Sample.EventGrid.Event", "dataVersion":"2.0", "id": "123-ddf-133-324255ffd", "eventTime": dt.datetime.utcnow() } with pytest.raises(TypeError, match="Data in EventGridEvent cannot be bytes*"): await client.send(eg_event)
async def test_send_cloud_event_data_none(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) attributes = { "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = None cloud_event = CloudEvent(attributes, data) await client.send(cloud_event)
async def publish(): credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(endpoint, credential) await client.send([ CloudEvent( type="Contoso.Items.ItemReceived", source="/contoso/items", data={ "itemSku": "Contoso Item SKU #1" }, subject="Door1" ) ])
async def publish(): credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(endpoint, credential) await client.send([ CloudEvent( attributes={ "type": "cloudevent", "source": "/cncf/cloud/event/1.0", "subject": "testingcncfevent" }, data=b'This is a cncf cloud event.', ) ])
async def publish(): credential = AzureKeyCredential(domain_key) client = EventGridPublisherClient(domain_hostname, credential) await client.send([ 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") ])
async def publish(): credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(endpoint, credential) # [START publish_cloud_event_dict_async] async with client: await client.send([{ "type": "Contoso.Items.ItemReceived", "source": "/contoso/items", "data": { "itemSku": "Contoso Item SKU #1" }, "subject": "Door1", "specversion": "1.0", "id": "randomclouduuid11" }])
async 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={ 'reasoncode':204, 'extension':'hello' } ) await client.send([cloud_event]) internal = _cloud_event_to_generated(cloud_event).serialize() assert 'reasoncode' in internal assert 'extension' in internal assert internal['reasoncode'] == 204
async def publish_event(): # authenticate client # [START publish_custom_schema_async] 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" } async with client: # publish list of events await client.send(custom_schema_event)
async def test_raise_on_large_payload(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) path = os.path.abspath( os.path.join(os.path.abspath(__file__), "..", "./large_data.json")) with open(path) as json_file: data = json.load(json_file) eg_event = EventGridEvent(subject="sample", data=data, event_type="Sample.EventGrid.Event", data_version="2.0") with pytest.raises(HttpResponseError) as err: await client.send(eg_event) assert "The maximum size (1536000) has been exceeded." in err.value.message
async def test_send_cloud_event_data_base64_using_data( 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) attributes = { "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = b'hello world' cloud_event = CloudEvent(attributes, data) 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 await client.send(cloud_event, raw_request_hook=callback)
async def test_send_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) attributes = { "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} cloud_event = CloudEvent(attributes, data) def callback(request): req = json.loads(request.http_request.body) assert req[0].get("data") is not None assert isinstance(req[0], dict) assert req[0].get("type") == "com.example.sampletype1" assert req[0].get("source") == "https://example.com/event-producer" await client.send(cloud_event, raw_request_hook=callback)
def test_send_NONE_credential_async(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint): with pytest.raises(ValueError, match="Parameter 'self._credential' must not be None."): client = EventGridPublisherClient(eventgrid_topic_endpoint, None)
def test_send_NONE_credential_async(self, variables, eventgrid_topic_endpoint): with pytest.raises(ValueError, match="Parameter 'self._credential' must not be None."): client = EventGridPublisherClient(eventgrid_topic_endpoint, None)
Set the environment variables with your own values before running the sample: 1) EVENTGRID_TOPIC_KEY - The access key of your eventgrid account. 2) EVENTGRID_TOPIC_ENDPOINT - The topic hostname. Typically it exists in the format "https://<YOUR-TOPIC-NAME>.<REGION-NAME>.eventgrid.azure.net/api/events". 3) EVENTGRID_SAS - The shared access signature that is to be used to authenticate the client. """ # [START client_auth_with_key_cred_async] import os from azure.eventgrid.aio import EventGridPublisherClient from azure.core.credentials import AzureKeyCredential topic_key = os.environ["EVENTGRID_TOPIC_KEY"] endpoint = os.environ["EVENTGRID_TOPIC_ENDPOINT"] credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(endpoint, credential) # [END client_auth_with_key_cred_async] # [START client_auth_with_sas_cred_async] import os from azure.eventgrid.aio import EventGridPublisherClient from azure.core.credentials import AzureSasCredential signature = os.environ["EVENTGRID_SAS"] endpoint = os.environ["EVENTGRID_TOPIC_ENDPOINT"] credential = AzureSasCredential(signature) client = EventGridPublisherClient(endpoint, credential) # [END client_auth_with_sas_cred_async] # [START client_auth_with_token_cred_async]