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
PutEventsoperation to publish events.
Prerequisites
You have completed the following:
Setup
-
Install a compiler that supports C++11 or later:
-
Windows: Visual Studio 2015 or later. For more information, see Install Visual Studio.
-
Linux: GCC 4.9 or later. For more information, see Download GCC.
-
-
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
yumorapt-getfor installation.yum install boost-devel openssl-develNoteYou cannot use
yumto 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
vcpkgfor installation.vcpkg install boost openssl-windows cpprestsdk
-
Management API SDK
Installation
Linux
-
Run the following command to clone the source code from GitHub.
git clone https://github.com/alibabacloud-sdk-cpp/dara-openapi.git -
Run the following command to change to the eventbridge directory and install the SDK.
cd dara-openapi && sh scripts/install.sh
Windows
-
Run the following command to clone the source code from GitHub.
git clone https://github.com/alibabacloud-sdk-cpp/dara-openapi.git -
In the project root directory, create an empty folder named cmake_build.
-
Perform the following operations in CMake.
-
For
Browse Source, select the source code directory alibabacloud_event_bridge. -
For Browse build, set the build directory to cmake_build.
-
Click Configure.
-
Click generate to build the Visual Studio solution.
-
-
In the cmake_build directory, use Visual Studio to open the solution darabonba_core.sln.
-
Select the Release build output, select INSTALL in the Configuration Manager, and then select .
Example
OpenAPI Explorer can automatically generate SDK examples for API operations. For more information, see Automatically generate SDK examples.
Data API SDK
Installation
Linux
-
Run the following command to clone the source code from GitHub.
git clone https://github.com/alibabacloud-sdk-cpp/eventbridge.git -
Run the following command to go to the eventbridge directory and install the SDK.
cd eventbridge && sh scripts/install.sh
Windows
-
Run the following command to clone the source code from GitHub.
git clone https://github.com/alibabacloud-sdk-cpp/eventbridge.git -
Create an empty folder cmake_build in the project root directory.
-
Perform the following operations in CMake.
-
For Browse Source, select the source code directory alibabacloud_event_bridge.
-
For Browse build, select the build directory cmake_build.
-
Click Configure.
-
Click generate to build the VS solution.
-
-
In the cmake_build directory, use Visual Studio to open the darabonba_core.sln solution.
-
Select the Release build output, in Configuration Manager, select INSTALL, and then select .
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);
}