Librerie della Griglia di eventi per PythonEvent Grid libraries for Python
Griglia di eventi di Azure è un servizio di routing di eventi intelligente completamente gestito che consente un uso degli eventi uniforme tramite un modello di pubblicazione-sottoscrizione.Azure Event Grid is a fully-managed intelligent event routing service that allows for uniform event consumption using a publish-subscribe model.
Altre informazioni su Griglia di eventi di Azure e introduzione all'esercitazione sugli eventi di archiviazione BLOB di Azure.Learn more about Azure Event Grid and get started with the Azure Blob storage event tutorial.
SDK di pubblicazionePublish SDK
Eseguire l'autenticazione, creare, gestire e pubblicare eventi negli argomenti usando l'SDK di pubblicazione di Griglia di eventi di Azure.Authenticate, create, handle, and publish events to topics using the Azure Event Grid publish SDK.
InstallazioneInstallation
Installare il pacchetto usando pip:Install the package with pip:
pip install azure-eventgrid
EsempioExample
Il codice seguente pubblica un evento in un argomento.The following code publishes an event to a topic. È possibile recuperare la chiave e l'endpoint dell'argomento dal portale di Azure o tramite l'interfaccia della riga di comando di Azure:You can retrieve the topic key and endpoint through the Azure Portal or through the Azure CLI:
endpoint=$(az eventgrid topic show --name <topic_name> -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name <topic_name> -g gridResourceGroup --query "key1" --output tsv)
from datetime import datetime
from azure.eventgrid import EventGridClient
from msrest.authentication import TopicCredentials
def publish_event(self):
credentials = TopicCredentials(
self.settings.EVENT_GRID_KEY
)
event_grid_client = EventGridClient(credentials)
event_grid_client.publish_events(
"your-endpoint-here",
events=[{
'id' : "dbf93d79-3859-4cac-8055-51e3b6b54bea",
'subject' : "Sample subject",
'data': {
'key': 'Sample Data'
},
'event_type': 'SampleEventType',
'event_time': datetime(2018, 5, 2),
'data_version': 1
}]
)
SDK di gestioneManagement SDK
Creare, aggiornare o eliminare istanze, argomenti e sottoscrizioni di Griglia di eventi con l'SDK di gestione.Create, update, or delete Event Grid instances, topics, and subscriptions with the management SDK.
InstallazioneInstallation
Installare il pacchetto usando pip:Install the package with pip:
pip install azure-mgmt-eventgrid
EsempioExample
L'esempio seguente crea un argomento personalizzato e iscrive un endpoint all'argomento.The following creates a custom topic and subscribes an endpoint to the topic. Il codice invia quindi un evento all'argomento tramite HTTPS.The code then sends an event to the topic through HTTPS. RequestBin è uno strumento open source di terze parti che consente di creare un endpoint e visualizza le richieste che gli vengono inviate.RequestBin is an open source, third-party tool that enables you to create an endpoint, and view requests that are sent to it. Passare a RequestBin e fare clic su Create a RequestBin (Crea un RequestBin).Go to RequestBin, and click Create a RequestBin. Copiare l'URL del contenitore, necessario per sottoscrivere l'argomento.Copy the bin URL, because you need it when subscribing to the topic.
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.eventgrid import EventGridManagementClient
import requests
RESOURCE_GROUP_NAME = 'gridResourceGroup'
TOPIC_NAME = 'gridTopic1234'
LOCATION = 'westus2'
SUBSCRIPTION_ID = 'YOUR_SUBSCRIPTION_ID'
SUBSCRIPTION_NAME = 'gridSubscription'
REQUEST_BIN_URL = 'YOUR_REQUEST_BIN_URL'
# create resource group
resource_client = ResourceManagementClient(credentials, SUBSCRIPTION_ID)
resource_client.resource_groups.create_or_update(
RESOURCE_GROUP_NAME,
{
'location': LOCATION
}
)
event_client = EventGridManagementClient(credentials, SUBSCRIPTION_ID)
# create a custom topic
event_client.topics.create_or_update(RESOURCE_GROUP_NAME, TOPIC_NAME, LOCATION)
# subscribe to a topic
scope = '/subscriptions/'+SUBSCRIPTION_ID+'/resourceGroups/'+RESOURCE_GROUP_NAME+'/providers/Microsoft.EventGrid/topics/'+TOPIC_NAME
event_client.event_subscriptions.create(scope, SUBSCRIPTION_NAME,
{
'destination': {
'endpoint_url': REQUEST_BIN_URL
}
}
)
# send an event to topic
# get endpoint url
url = event_client.event_subscriptions.get_full_url(scope, SUBSCRIPTION_NAME).endpoint_url
# get key
key = event_client.topics.list_shared_access_keys(RESOURCE_GROUP_NAME,TOPIC_NAME).key1
headers = {'aeg-sas-key': key}
s = requests.get('https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/event-grid/customevent.json')
r = requests.post(url, data=s, headers=headers)
print(r.status_code)
print(r.content)
Passare all'URL di RequestBin creato in precedenza per visualizzare l'evento appena inviato.Browse to the RequestBin URL created earlier to see the event just sent.
Pulire le risorseClean up resources
az group delete --name gridResourceGroup
Altre informazioniLearn more
Receive events using the Event Grid SDK (Ricevere eventi tramite l'SDK di Griglia di eventi)Receive events using the Event Grid SDK