This topic describes how to install the SDK for Python provided by EventBridge and how to use the SDK for Python to publish events. Sample code is provided in this topic.
Overview
EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code and dependencies that are used by different types of SDKs vary.
Management API SDKs: the SDKs that are used to perform operations on the EventBridge console.
Data API SDKs: the channel for event data. Only the PutEvents API operation is called by using this type of SDK.
Before you start
Perform the following operations:
Environment preparations
Environment requirements
Python 3.4 or later is installed. For more information, visit the downloads page of Python.
NoteBy default, the pip tool is included in Python 3.4 or later.
Check the Python version
Run the
python -V
command to check the Python version.
Management API SDKs
Install the SDK for Python
Run the following command to install the SDK for Python:
pip install alibabacloud_eventbridge20200401==2.0.1
Run the following command to install the output library for Python:
pip install alibabacloud_tea_console
Sample code
You can call the corresponding operation in OpenAPI Explorer to automatically generate the sample code. For more information, see Automatic generation of SDK examples.
Data API SDKs
Install the SDK for Python
Run the following command to install the SDK for Python:
pip install alibabacloud_eventbridge
Run the following command to install the output library for Python:
pip install alibabacloud_tea_console
Sample code
Data API SDKs support only the PutEvents API operation. If you want to use the SDK for Python to publish one or more events, refer to the following sample code:
# -*- coding: utf-8 -*-
from alibabacloud_eventbridge.client import Client as EventBridgeClient
from alibabacloud_eventbridge import models as event_bridge_models
from alibabacloud_tea_console.client import Client as ConsoleClient
from alibabacloud_tea_util.client import Client as UtilClient
class put_events(object):
def __init__(self):
pass
@staticmethod
def create_client():
"""
Use the create_client() function to initialize common request parameters.
"""
config = event_bridge_models.Config(
)
config.access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
config.access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
# Your endpoint.
config.endpoint = "<endpoint>"
return EventBridgeClient(config)
@staticmethod
def put_events(client):
"""
PutEvents
"""
event = event_bridge_models.CloudEvent(
)
event.datacontenttype = "application/json"
event.data = UtilClient.to_bytes("test")
event.id = "a5074581-7e74-4e4c-868f-47e7afdf****"
event.source = "acs.oss"
event.specversion = "1.0"
event.type = "oss:ObjectCreated:PostObject"
event.time = "2020-08-24T13:54:05.965Asia/Shanghai"
event.subject = "acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg"
event.extensions = {
"aliyuneventbusname": "demo-bus"
}
try:
resp = client.put_events([
event
])
ConsoleClient.log("--------------------Publish event to the aliyun EventBus--------------------")
ConsoleClient.log(UtilClient.to_jsonstring(resp.to_map()))
except Exception as error:
ConsoleClient.log(error.message)
@staticmethod
def main(args):
client = put_events.create_client()
put_events.put_events(client)
put_events.main("")