All Products
Search
Document Center

EventBridge:SDK for C++

Last Updated:Nov 07, 2023

This topic describes how to install the SDK for C++ provided by EventBridge and how to use the SDK for C++ to publish events. Sample code is also provided in this topic.

Overview

EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code that is used by different types of SDKs varies.

  • 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

  • A compiler that supports C++ 11 or later is installed.

  • CMake 3.0 or later is installed. For more information, visit the download page of CMake.

  • A memory of 4 GB or above is available for the operating system.

  • The required dependency libraries are installed.

    Before you install the SDK for C++, you must install the required dependency libraries, including Boost and OpenSSL libraries. You must also install the C++ REST SDK. You can use one of the following methods based on your operating system to install the dependency libraries:

    • If your operating system is macOS, we recommend that you use Homebrew to install the dependency libraries.

      brew install boost cpprestsdk openssl
    • If your operating system is Linux, we recommend that you use YUM or apt-get to install the dependency libraries.

      • yum install boost-devel openssl-devel
        Note

        You cannot install the C++ REST SDK by using YUM.

      • # install boost
        sudo add-apt-repository ppa:mhier/libboost-latest -y
        sudo apt-get update
        sudo apt-get install libboost-all-dev
        
        sudo apt-get install libcpprest-dev libcurl4-openssl-dev libssl-dev
    • If your operating system is Windows, we recommend that you use vcpkg to install the dependency libraries.

      vcpkg install boost openssl-windows cpprestsdk

Management API SDKs

Install the SDK for C++

Linux

  1. Run the following command to clone the source code from GitHub:

    git clone https://github.com/alibabacloud-sdk-cpp/dara-openapi.git
  2. Run the following command to go to the eventbridge directory and install the SDK:

    cd dara-openapi && sh scripts/install.sh

Windows

  1. Run the following command to clone the source code from GitHub:

    git clone https://github.com/alibabacloud-sdk-cpp/dara-openapi.git
  2. Create an empty directory cmake_build in the root directory of the project.

  3. Perform the following steps in CMake:

    1. Click Browse Source and select the alibabacloud_event_bridge directory as the source code directory.

    2. Click Browse Build and select the cmake_build directory as the build directory.

    3. Click Configure.

    4. Click Generate to build a Visual Studio solution.

  4. In the cmake_build directory, use Visual Studio to open the solution darabonba_core.sln.

  5. Select Release, select Install in the Configuration Manager dialog box, and then choose Build > Build Solution.

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 C++

Linux

  1. Run the following command to clone the source code from GitHub:

    git clone https://github.com/alibabacloud-sdk-cpp/eventbridge.git
  2. Run the following command to go to the eventbridge directory and install the SDK:

    cd eventbridge && sh scripts/install.sh

Windows

  1. Run the following command to clone the source code from GitHub:

    git clone https://github.com/alibabacloud-sdk-cpp/eventbridge.git
  2. Create an empty directory cmake_build in the root directory of the project.

  3. Perform the following steps in CMake:

    1. Click Browse Source and select the alibabacloud_event_bridge directory as the source code directory.

    2. Click Browse Build and select the cmake_build directory as the build directory.

    3. Click Configure.

    4. Click Generate to build a Visual Studio solution.

  4. In the cmake_build directory, use Visual Studio to open the solution darabonba_core.sln.

  5. Select Release, select Install in the Configuration Manager dialog box, and then choose Build > Build Solution.

Sample code

Data API SDKs support only the PutEvents API operation. If you want to use the SDK for C++ to publish one or more events, refer to the following sample code:

#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>();
      SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
      SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
      SetEndpoint("<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);
}