This topic describes how to use the SDK for C++ to create one or more event targets for an event rule.

The following code provides an example on how to use the SDK for C++ to create one or more event targets for an event rule:

#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::createTargetsSample(
    shared_ptr<Alibabacloud_EventBridge::Client> client) {
  try {
    shared_ptr<Alibabacloud_EventBridge::CreateTargetsRequest>
        createTargetsRequest =
            make_shared<Alibabacloud_EventBridge::CreateTargetsRequest>();
    createTargetsRequest->eventBusName = make_shared<string>("demo-bus");
    createTargetsRequest->ruleName = make_shared<string>("myRule");
    shared_ptr<Alibabacloud_EventBridge::TargetEntry> targetEntry =
        make_shared<Alibabacloud_EventBridge::TargetEntry>();
    targetEntry->id = make_shared<string>("1234");
    targetEntry->endpoint = make_shared<string>("http://www.baidu.com");
    shared_ptr<vector<Alibabacloud_EventBridge::TargetEntry>> list =
        make_shared<vector<Alibabacloud_EventBridge::TargetEntry>>(
            vector<Alibabacloud_EventBridge::TargetEntry>({targetEntry}));
    createTargetsRequest->targets = list;
    shared_ptr<Alibabacloud_EventBridge::CreateTargetsResponse> response =
        make_shared<Alibabacloud_EventBridge::CreateTargetsResponse>(
            client->createTargets(createTargetsRequest));
    Darabonba_Console::Client::log(make_shared<string>(
        "--------------------Create targets success--------------------"));
    Darabonba_Console::Client::log(
        make_shared<string>(Darabonba_Util::Client::toJSONString(
            make_shared<map<string, boost::any>>(response->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::createTargetsSample(client);
}