This topic describes how to use the SDK for C++ to publish one or more events.

The following code provides an example on how to use the SDK for C++ to publish one or more events:

#include <alibabacloud/event_bridge.hpp>
#include <alibabacloud/sample.hpp>
#include <darabonba/console.hpp>
#include <darabonba/core.hpp>
#include <darabonba/util.hpp>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

using namespace Alibabacloud_Sample;

Alibabacloud_EventBridge::Client Alibabacloud_Sample::Client::createClient() {
  shared_ptr<Alibabacloud_EventBridge::Config> config =
      make_shared<Alibabacloud_EventBridge::Config>();
  // The AccessKey ID. 
  config->accessKeyId = make_shared<string>("<accessKeyId>");
  // The AccessKey secret. 
  config->accessKeySecret = make_shared<string>("<accessKeySecret>");
  // The endpoint. 
  config->endpoint = make_shared<string>("<endpoint>");
  return Alibabacloud_EventBridge::Client(config);
}

void Alibabacloud_Sample::Client::PutEvents(
    shared_ptr<Alibabacloud_EventBridge::Client> client) {
  shared_ptr<Alibabacloud_EventBridge::CloudEvent> event =
      make_shared<Alibabacloud_EventBridge::CloudEvent>();
  event->datacontenttype = make_shared<string>("application/json");
  event->data = make_shared<vector<uint8_t>>(
      Darabonba_Util::Client::toBytes(make_shared<string>("test")));
  event->id = make_shared<string>("a5074581-7e74-4e4c-868f-47e7afdf8445");
  event->source = make_shared<string>("acs.oss");
  event->specversion = make_shared<string>("1.0");
  event->type = make_shared<string>("oss:ObjectCreated:PostObject");
  event->time = make_shared<string>("2020-08-24T13:54:05.965Asia/Shanghai");
  event->subject = make_shared<string>("1.0");
  event->type = make_shared<string>(
      "acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg");
  event->extensions = make_shared<map<string, string>>(
      map<string, string>({{"aliyuneventbusname", "demo-bus"}}));
  try {
    shared_ptr<Alibabacloud_EventBridge::PutEventsResponse> resp =
        make_shared<Alibabacloud_EventBridge::PutEventsResponse>(
            client->putEvents(
                make_shared<vector<Alibabacloud_EventBridge::CloudEvent>>(
                    vector<Alibabacloud_EventBridge::CloudEvent>({event}))));
    Darabonba_Console::Client::log(
        make_shared<string>("--------------------Publish event to the aliyun "
                            "EventBus--------------------"));
    Darabonba_Console::Client::log(
        make_shared<string>(Darabonba_Util::Client::toJSONString(
            make_shared<map<string, boost::any>>(resp->toMap()))));
  } catch (std::exception &error) {
    Darabonba_Console::Client::log(error.message);
  }
}

void Alibabacloud_Sample::Client::main(shared_ptr<vector<string>> args) {
  shared_ptr<Alibabacloud_EventBridge::Client> client =
      make_shared<Alibabacloud_EventBridge::Client>(Client::createClient());
  Client::PutEvents(client);
}