C++ SDK

更新时间:
复制 MD 格式

This topic covers how to install the SDK for C++, use it to publish events, and includes sample code.

Overview

EventBridge provides two types of SDKs: a management API SDK and a data API SDK. The sample code for each type is different.

  • management API SDK: Use this SDK to perform operations that are available on the console.

  • data API SDK: Use this SDK to handle event data. Currently, it only supports the PutEvents operation to publish events.

Prerequisites

You have completed the following:

Setup

  • Install a compiler that supports C++11 or later:

  • Install CMake 3.0 or later. For more information, see Install CMake.

  • Ensure that your operating system has at least 4 GB of memory.

  • Install the following required dependency libraries.

    Before installing the SDK for C++, you must install its dependencies: Boost, OpenSSL, and C++ REST SDK (cpprestsdk). Installation methods vary by operating system:

    • On macOS, use Homebrew for installation.

      brew install boost cpprestsdk openssl
    • On Linux, use yum or apt-get for installation.

      • yum install boost-devel openssl-devel
        Note

        You cannot use yum to install cpprestsdk.

      • # 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
    • On Windows, use vcpkg for installation.

      vcpkg install boost openssl-windows cpprestsdk

Management API SDK

Installation

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 change 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. In the project root directory, create an empty folder named cmake_build.

  3. Perform the following operations in CMake.

    1. For Browse Source, select the source code directory alibabacloud_event_bridge.

    2. For Browse build, set the build directory to cmake_build.

    3. Click Configure.

    4. Click generate to build the Visual Studio solution.

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

  5. Select the Release build output, select INSTALL in the Configuration Manager, and then select Data Lake Formation (DLF) > Build Solution.

Example

OpenAPI Explorer can automatically generate SDK examples for API operations. For more information, see Automatically generate SDK examples.

Data API SDK

Installation

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 folder cmake_build in the project root directory.

  3. Perform the following operations in CMake.

    1. For Browse Source, select the source code directory alibabacloud_event_bridge.

    2. For Browse build, select the build directory cmake_build.

    3. Click Configure.

    4. Click generate to build the VS solution.

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

  5. Select the Release build output, in Configuration Manager, select INSTALL, and then select Data Lake Formation (DLF) > Build Solution.

Example

The data API SDK supports only the PutEvents API operation. The following example shows 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>();
      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);
}