Azure Event Grid client library for Python ========================================== Azure Event Grid is a fully-managed intelligent event routing service that allows for uniform event consumption using a publish-subscribe model. `Source code `_ | `Package (PyPI) `_ | `API reference documentation `_\ | `Product documentation `_ | `Samples `_\ | `Changelog `_ Getting started --------------- Prerequisites ^^^^^^^^^^^^^ * Python 2.7, or 3.5 or later is required to use this package. * You must have an `Azure subscription `_ and an Event Grid Topic resource to use this package. Install the package ^^^^^^^^^^^^^^^^^^^ Install the Azure Event Grid client library for Python with `pip `_\ : .. code-block:: bash pip install azure-eventgrid * An existing Event Grid topic or domain is required. You can create the resource using `Azure Portal `_ or `Azure CLI `_ If you use Azure CLI, replace ```` and ```` with your own unique names. Create an Event Grid Topic ~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: az eventgrid topic --create --location --resource-group --name Create an Event Grid Domain ~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: az eventgrid domain --create --location --resource-group --name Authenticate the client ^^^^^^^^^^^^^^^^^^^^^^^ In order to interact with the Event Grid service, you will need to create an instance of a client. An **endpoint** and **credential** are necessary to instantiate the client object. Looking up the endpoint ~~~~~~~~~~~~~~~~~~~~~~~ You can find the endpoint and the hostname on the Azure portal. Create the client with AzureKeyCredential ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To use an API key as the ``credential`` parameter, pass the key as a string into an instance of `AzureKeyCredential `_. .. code-block:: python from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient endpoint = "https://..eventgrid.azure.net" credential = AzureKeyCredential("") eg_publisher_client = EventGridPublisherClient(endpoint, credential) Key concepts ------------ Information about the key concepts on Event Grid, see `Concepts in Azure Event Grid `_ EventGridPublisherClient ^^^^^^^^^^^^^^^^^^^^^^^^ ``EventGridPublisherClient`` provides operations to send event data to topic hostname specified during client initialization. CloudEvents and EventGridEvents can be sent either as a single event or a list of respective typed objects or their equivalent dict representations. To send a custom schema, a dict representation can be used. Please have a look at the `samples `_ for detailed examples. Examples -------- The following sections provide several code snippets covering some of the most common Event Grid tasks, including: * `Send an Event Grid Event <#send-an-event-grid-event>`_ * `Send a Cloud Event <#send-a-cloud-event>`_ Send an Event Grid Event ^^^^^^^^^^^^^^^^^^^^^^^^ This example publishes an Event Grid event. .. code-block:: Python import os from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient, EventGridEvent key = os.environ["EG_ACCESS_KEY"] endpoint = os.environ["EG_TOPIC_HOSTNAME"] event = EventGridEvent( data={"team": "azure-sdk"}, subject="Door1", event_type="Azure.Sdk.Demo", data_version="2.0" ) credential = AzureKeyCredential(key) client = EventGridPublisherClient(endpoint, credential) client.send(event) Send a Cloud Event ^^^^^^^^^^^^^^^^^^ This example publishes a Cloud event. .. code-block:: Python import os from azure.core.credentials import AzureKeyCredential from azure.eventgrid import EventGridPublisherClient, CloudEvent key = os.environ["CLOUD_ACCESS_KEY"] endpoint = os.environ["CLOUD_TOPIC_HOSTNAME"] event = CloudEvent( type="Azure.Sdk.Sample", source="https://egsample.dev/sampleevent", data={"team": "azure-sdk"} ) credential = AzureKeyCredential(key) client = EventGridPublisherClient(endpoint, credential) client.send(event) Troubleshooting --------------- * Enable ``azure.eventgrid`` logger to collect traces from the library. General ^^^^^^^ Event Grid client library will raise exceptions defined in `Azure Core `_. Logging ^^^^^^^ This library uses the standard `logging `_ library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level. Optional Configuration ^^^^^^^^^^^^^^^^^^^^^^ Optional keyword arguments can be passed in at the client and per-operation level. The azure-core `reference documentation `_ describes available configurations for retries, logging, transport protocols, and more. Next steps ---------- The following section provides several code snippets illustrating common patterns used in the Event Grid Python API. More sample code ^^^^^^^^^^^^^^^^ These code samples show common champion scenario operations with the Azure Event Grid client library. * Publish Custom Events to a topic: `cs1_publish_custom_events_to_a_topic.py `_ * Publish Custom events to a domain topic: `cs2_publish_custom_events_to_a_domain_topic.py `_ * Publish a Cloud Event: `cs6_publish_events_using_cloud_events_1.0_schema.py `_ More samples can be found `here `_. Additional documentation ^^^^^^^^^^^^^^^^^^^^^^^^ For more extensive documentation on Azure Event Grid, see the `Event Grid documentation `_ on docs.microsoft.com. Contributing ------------ This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit `cla.microsoft.com `_. When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. This project has adopted the `Microsoft Open Source Code of Conduct `_. For more information see the `Code of Conduct FAQ `_ or contact `opencode@microsoft.com `_ with any additional questions or comments. .. raw:: html Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` .. toctree:: :maxdepth: 5 :glob: :caption: Developer Documentation azure.eventgrid.rst