C++ SDK examples

Updated at:

In this tutorial, you use the C++ SDK V2.0 to call the Cloud Monitor 2.0 (cms20240330) OpenAPI. The same approach applies to other APIs — replace the request and response types and fields as needed.

Prerequisites

  • A C++11 or later compiler

  • CMake 3.0 or later

Step 1: Install the SDK

Clone the cms-20240330 source code at the specified version and build it with CMake:

git clone --branch 10.0.0 https://github.com/alibabacloud-sdk-cpp/cms-20240330.git
cd cms-20240330
mkdir build && cd build
cmake ..
make

Source code repository: alibabacloud-sdk-cpp/cms-20240330.

Step 2: Configure credentials

Configure your AccessKey pair as environment variables to avoid hardcoding credentials in your code:

export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"

Step 3: Call the GetCmsService API

The following example calls the GetCmsService API to query the activation status of Cloud Monitor 2.0 for a specified product:

#include <alibabacloud/open_api.hpp>
#include <alibabacloud/cms20240330.hpp>

int main() {
    Alibabacloud_OpenApi::Config config;
    // Make sure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and
    // ALIBABA_CLOUD_ACCESS_KEY_SECRET are correctly set.
    config.accessKeyId     = make_shared<string>(getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
    config.accessKeySecret = make_shared<string>(getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    config.regionId        = make_shared<string>("cn-hangzhou");
    // For the Endpoint, refer to https://api.aliyun.com/product/Cms.
    config.endpoint        = make_shared<string>("metrics.cn-hangzhou.aliyuncs.com");

    Alibabacloud_Cms20240330::Client client(make_shared<Alibabacloud_OpenApi::Config>(config));

    Alibabacloud_Cms20240330::GetCmsServiceRequest request;
    request.product = "your_value";
    request.service = "your_value";

    Alibabacloud_Cms20240330::GetCmsServiceResponse response = client.getCmsService(
        make_shared<Alibabacloud_Cms20240330::GetCmsServiceRequest>(request)
    );

    printf("%s\n", response.Body.requestId.c_str());
    return 0;
}

The code works as follows:

  1. Initialize the configuration — Create an Alibabacloud_OpenApi::Config object. The Config object holds the accessKeyId, accessKeySecret, regionId, endpoint, and other settings. The endpoint in this example is metrics.cn-hangzhou.aliyuncs.com.

  2. Instantiate the client — Create an Alibabacloud_Cms20240330::Client instance with the configuration object.

  3. Create the request object — Create a GetCmsServiceRequest instance.

  4. Set the request parameters — Set the product and service request parameters.

  5. Call the API — Call getCmsService through the client to obtain a GetCmsServiceResponse object.

  6. Read the response — Access return values from response.Body. For example: string requestId = response.Body.requestId;

For more API operations and sample code, see SDK examples on the OpenAPI portal.