• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. Python
  6. SMN Python SDK Demo

SMN Python SDK Demo

Creating a Topic

You can create a maximum of 3000 topics. APIs are idempotent. If a topic of the same name already exists, the status code 200 is returned. Otherwise, the status code 201 is returned.

You can create a topic using the Python OpenStack SDK based on the following code:

def operate_topic(conn):
topic_dict = {
'name': 'labj',
'display_name': 'djb',
}
tp = conn.smn.create_topic(**topic_dict)

Adding a Subscription

Add a subscription to a specified topic. If the status of the subscription is unconfirmed, a confirmation message is sent to the subscriber. After confirming the subscription, the subscriber can receive notification messages published to the topic. APIs are idempotent. If the added subscription already exists, the status code 200 is returned. Otherwise, the status code 201 is returned.

You can add a subscription using the Python OpenStack SDK based on the following code by specifying tp to topicUrn:

sub_dict = {
'protocol': 'email',
'endpoint': 'xxx@xxx.com',
'remark': 'test',
}
sub = conn.smn.subscript_topic(tp, **sub_dict)

Publishing a Message

You can publish messages to a topic. After the message ID is returned, the message has been saved and is to be pushed to the subscribers of the topic. The message format varies depending on the protocol of a subscription.

You can publish a message using the Python OpenStack SDK based on the following code:

msg_dict = {
'message': "hello world!"
}
print("publish message")
conn.smn.publish_topic(tp, **msg_dict)