add examples and build with as many cpus as possible
This commit is contained in:
14
build_sdk.sh
Executable file
14
build_sdk.sh
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
MAKE=make
|
||||
if command -v python > /dev/null ; then
|
||||
MAKE="make -j $(python -c 'import multiprocessing as mp; print(int(mp.cpu_count()))')"
|
||||
fi
|
||||
|
||||
echo $MAKE
|
||||
|
||||
rm -rf sdk_build
|
||||
mkdir sdk_build
|
||||
cd sdk_build
|
||||
cmake -DBUILD_FUNCTION_TESTS=OFF -DBUILD_UNIT_TESTS=OFF -DENABLE_COVERAGE=OFF ..
|
||||
$MAKE
|
||||
@@ -15,7 +15,7 @@
|
||||
#
|
||||
|
||||
# coverage option
|
||||
OPTION (ENABLE_COVERAGE "Use gcov" ON)
|
||||
OPTION (ENABLE_COVERAGE "Use gcov" OFF)
|
||||
MESSAGE(STATUS ENABLE_COVERAGE=${ENABLE_COVERAGE})
|
||||
IF(ENABLE_COVERAGE)
|
||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
|
||||
27
examples/CMakeLists.txt
Normal file
27
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,27 @@
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
|
||||
cmake_policy(SET CMP0048 NEW)
|
||||
|
||||
project(alibabacloud-sdk-examples)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
message("include dir: " ${CMAKE_SOURCE_DIR}/../core/include)
|
||||
|
||||
include_directories("${CMAKE_SOURCE_DIR}/../core/include/")
|
||||
include_directories("${CMAKE_SOURCE_DIR}/../cdn/include/")
|
||||
include_directories("${CMAKE_SOURCE_DIR}/../ecs/include/")
|
||||
include_directories("${CMAKE_SOURCE_DIR}/../rds/include/")
|
||||
include_directories("${CMAKE_SOURCE_DIR}/../slb/include/")
|
||||
include_directories("${CMAKE_SOURCE_DIR}/../vpc/include/")
|
||||
|
||||
|
||||
link_directories(${CMAKE_SOURCE_DIR}/../sdk_build/lib)
|
||||
|
||||
add_subdirectory(imagesearch)
|
||||
add_subdirectory(nlp)
|
||||
add_subdirectory(rds)
|
||||
add_subdirectory(ecs)
|
||||
add_subdirectory(vpc)
|
||||
add_subdirectory(slb)
|
||||
add_subdirectory(cdn)
|
||||
37
examples/README.md
Normal file
37
examples/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# how to use examples
|
||||
|
||||
## 1. install sdk
|
||||
|
||||
```bash
|
||||
cd path/to/aliyun-openapi-cpp-sdk
|
||||
./build_sdk.sh
|
||||
```
|
||||
|
||||
## 2. build examples
|
||||
|
||||
```bash
|
||||
cd path/to/aliyun-openapi-cpp-sdk
|
||||
cd examples
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
make -j
|
||||
```
|
||||
|
||||
## 3. run examples
|
||||
|
||||
configure `accessKeyId` and `accessKeySecret` via env.
|
||||
|
||||
```bash
|
||||
export ENV_AccessKeyId="your-accessKeyId"
|
||||
export ENV_AccessKeySecret="your-accessKeySecret"
|
||||
```
|
||||
|
||||
```bash
|
||||
cd path/to/aliyun-openapi-cpp-sdk
|
||||
cd examples/build/bin
|
||||
|
||||
# run ecs_demo
|
||||
./ecs_demo
|
||||
...
|
||||
```
|
||||
17
examples/cdn/CMakeLists.txt
Normal file
17
examples/cdn/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(cdn_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(cdn_demo cdn.cc)
|
||||
target_link_libraries(cdn_demo alibabacloud-sdk-core alibabacloud-sdk-cdn)
|
||||
|
||||
target_link_libraries(cdn_demo )
|
||||
|
||||
set_target_properties(cdn_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}cdn_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
27
examples/cdn/cdn.cc
Normal file
27
examples/cdn/cdn.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/cdn/CdnClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
using namespace AlibabaCloud::Cdn;
|
||||
|
||||
int main() {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
CdnClient client(key, secret, configuration);
|
||||
Model::DescribeCdnServiceRequest request;
|
||||
auto out = client.describeCdnService(request);
|
||||
cout << endl << "cdn describeCdnService result:" << endl;
|
||||
cout << "error code: " << out.error().errorCode() << endl;
|
||||
cout << "changingChargeType: " << out.result().getChangingChargeType() << endl;
|
||||
cout << "internetChargeType: " << out.result().getInternetChargeType() << endl;
|
||||
cout << "instanceId: " << out.result().getInstanceId() << endl;
|
||||
cout << "openingTime: " << out.result().getOpeningTime() << endl;
|
||||
cout << "changingAffectTime: " << out.result().getChangingAffectTime() << endl << endl;
|
||||
ShutdownSdk();
|
||||
}
|
||||
17
examples/ecs/CMakeLists.txt
Normal file
17
examples/ecs/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(ecs_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(ecs_demo ecs.cc)
|
||||
target_link_libraries(ecs_demo alibabacloud-sdk-core alibabacloud-sdk-ecs)
|
||||
|
||||
target_link_libraries(ecs_demo )
|
||||
|
||||
set_target_properties(ecs_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}ecs_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
25
examples/ecs/ecs.cc
Normal file
25
examples/ecs/ecs.cc
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/ecs/EcsClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
using namespace AlibabaCloud::Ecs;
|
||||
|
||||
int main() {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
EcsClient client(key, secret, configuration);
|
||||
Model::DescribeInstancesRequest request;
|
||||
request.setPageSize(10);
|
||||
auto outcome = client.describeInstances(request);
|
||||
cout << endl << "ecs describeInstances returned:" << endl;
|
||||
cout << "error code: " << outcome.error().errorCode() << endl;
|
||||
cout << "totalCount: " << outcome.result().getTotalCount() << endl << endl;
|
||||
|
||||
ShutdownSdk();
|
||||
}
|
||||
BIN
examples/imagesearch/1.jpg
Normal file
BIN
examples/imagesearch/1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 179 KiB |
BIN
examples/imagesearch/2.jpg
Normal file
BIN
examples/imagesearch/2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 230 KiB |
18
examples/imagesearch/CMakeLists.txt
Normal file
18
examples/imagesearch/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(imagesearch_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(imagesearch_demo imagesearch.cc)
|
||||
target_link_libraries(imagesearch_demo alibabacloud-sdk-core)
|
||||
|
||||
target_link_libraries(imagesearch_demo )
|
||||
|
||||
set_target_properties(imagesearch_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}imagesearch_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
file(COPY 1.jpg 2.jpg DESTINATION ${CMAKE_BINARY_DIR}/bin)
|
||||
194
examples/imagesearch/imagesearch.cc
Normal file
194
examples/imagesearch/imagesearch.cc
Normal file
@@ -0,0 +1,194 @@
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/core/CommonClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
typedef unsigned char u8_t;
|
||||
|
||||
|
||||
const string catId = "88888888"; // ref: https://help.aliyun.com/document_detail/66623.html
|
||||
const string itemId = "1234";
|
||||
string custContent = "{\"key\":\"value\"}";
|
||||
|
||||
map<string, string> picList;
|
||||
map<int, string> mapStudent;
|
||||
|
||||
const string jpg1 = "1.jpg";
|
||||
const string jpg2 = "2.jpg";
|
||||
|
||||
|
||||
|
||||
static const u8_t base64_table[65] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
/**
|
||||
* base64_encode - Base64 encode
|
||||
* @src: Data to be encoded
|
||||
* @len: Length of the data to be encoded
|
||||
* @out_len: Pointer to output length variable, or %NULL if not used
|
||||
* Returns: Allocated buffer of out_len bytes of encoded data,
|
||||
* or empty string on failure
|
||||
*/
|
||||
string base64_encode(const unsigned char *src, size_t len) {
|
||||
unsigned char *out, *pos;
|
||||
const unsigned char *end, *in;
|
||||
size_t olen;
|
||||
|
||||
olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
|
||||
|
||||
if (olen < len)
|
||||
return std::string(); /* integer overflow */
|
||||
|
||||
std::string outStr;
|
||||
outStr.resize(olen);
|
||||
out = (unsigned char*)&outStr[0];
|
||||
|
||||
end = src + len;
|
||||
in = src;
|
||||
pos = out;
|
||||
while (end - in >= 3) {
|
||||
*pos++ = base64_table[in[0] >> 2];
|
||||
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
|
||||
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
|
||||
*pos++ = base64_table[in[2] & 0x3f];
|
||||
in += 3;
|
||||
}
|
||||
|
||||
if (end - in) {
|
||||
*pos++ = base64_table[in[0] >> 2];
|
||||
if (end - in == 1) {
|
||||
*pos++ = base64_table[(in[0] & 0x03) << 4];
|
||||
*pos++ = '=';
|
||||
}
|
||||
else {
|
||||
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
|
||||
*pos++ = base64_table[(in[1] & 0x0f) << 2];
|
||||
}
|
||||
*pos++ = '=';
|
||||
}
|
||||
return outStr;
|
||||
}
|
||||
|
||||
string imageBase64(string file) {
|
||||
|
||||
FILE* img = fopen(file.c_str(), "rb");
|
||||
if (!img) {
|
||||
perror("open file error");
|
||||
return "";
|
||||
}
|
||||
|
||||
fseek(img, 0L, SEEK_END);
|
||||
int size = ftell(img);
|
||||
fseek(img, 0L, SEEK_SET);
|
||||
u8_t* buf = (u8_t*)malloc(size);
|
||||
int bytes = fread(buf, 1, size, img);
|
||||
fclose(img);
|
||||
string encoded = base64_encode(buf, bytes);
|
||||
free(buf);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
string buildContent(map<string, string> params) {
|
||||
string meta = "";
|
||||
string body = "";
|
||||
int start = 0;
|
||||
|
||||
for(auto item = params.begin(); item != params.end(); item++) {
|
||||
auto k = item->first;
|
||||
auto v = item->second;
|
||||
|
||||
if (meta.size() > 0) {
|
||||
meta += "#";
|
||||
}
|
||||
|
||||
std::stringstream ss1, ss2;
|
||||
ss1 << start;
|
||||
ss2 << start + v.size();
|
||||
|
||||
meta += k + "," + ss1.str() + "," + ss2.str();
|
||||
body += v;
|
||||
start += v.size();
|
||||
|
||||
}
|
||||
return meta + "^" + body;
|
||||
}
|
||||
|
||||
string buildAddContent(map<string, string> picList) {
|
||||
if (itemId.empty() || catId.empty() ) {
|
||||
return "";
|
||||
}
|
||||
if (custContent.empty()) {
|
||||
custContent = "";
|
||||
}
|
||||
|
||||
map<string, string> params;
|
||||
params["item_id"] = itemId;
|
||||
params["cat_id"] = catId;
|
||||
params["cust_content"] = custContent;
|
||||
|
||||
string picListStr = "";
|
||||
for (auto item = picList.begin(); item != picList.end(); item++) {
|
||||
auto v = item->second;
|
||||
auto k = item->first;
|
||||
|
||||
if (k.empty()) {
|
||||
continue;
|
||||
}
|
||||
picListStr += k + ",";
|
||||
params[k] = v;
|
||||
}
|
||||
|
||||
params["pic_list"] = picListStr.substr(0, picListStr.size() - 1);
|
||||
return buildContent(params);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-shanghai");
|
||||
CommonClient client(key, secret, configuration);
|
||||
|
||||
CommonRequest request(CommonRequest::RoaPattern);
|
||||
request.setScheme("http");
|
||||
request.setDomain("imagesearch.cn-shanghai.aliyuncs.com");
|
||||
request.setResourcePath("/item/add");
|
||||
|
||||
request.setHeaderParameter("instanceName", "sdkimagetest1");
|
||||
request.setHttpMethod(HttpRequest::Post);
|
||||
|
||||
char dir[1024];
|
||||
utils.get_dir_exec(dir, nullptr);
|
||||
string image1_path = string(dir) + "1.jpg";
|
||||
string image2_path = string(dir) + "2.jpg";
|
||||
picList[base64_encode((u8_t*)jpg1.c_str(), jpg1.size())] = imageBase64(image1_path);
|
||||
picList[base64_encode((u8_t*)jpg2.c_str(), jpg2.size())] = imageBase64(image2_path);
|
||||
|
||||
string cc = buildAddContent(picList);
|
||||
const char* data = cc.c_str();
|
||||
request.setContent(cc.c_str(), cc.size());
|
||||
request.setHeaderParameter("Content-Type", "application/octet-stream;charset=utf-8");
|
||||
request.setHeaderParameter("Accept", "application/json");
|
||||
request.setVersion("2018-01-20");
|
||||
|
||||
auto out = client.commonResponse(request);
|
||||
if (!out.isSuccess()) {
|
||||
cout << "error code: " << out.error().errorCode() << endl;
|
||||
cout << "error message: " << out.error().errorMessage() << endl;
|
||||
cout << "error host: " << out.error().host() << endl;
|
||||
cout << "error requestId: " << out.error().requestId() << endl;
|
||||
cout << "error detail: " << out.error().detail() << endl;
|
||||
ShutdownSdk();
|
||||
return -1;
|
||||
}
|
||||
cout << endl << "add item returns: " << out.result().payload() << endl << endl;
|
||||
ShutdownSdk();
|
||||
return 0;
|
||||
}
|
||||
17
examples/nlp/CMakeLists.txt
Normal file
17
examples/nlp/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(nlp_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(nlp_demo nlp.cc)
|
||||
target_link_libraries(nlp_demo alibabacloud-sdk-core)
|
||||
|
||||
target_link_libraries(nlp_demo )
|
||||
|
||||
set_target_properties(nlp_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}nlp_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
46
examples/nlp/nlp.cc
Normal file
46
examples/nlp/nlp.cc
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/core/CommonClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
|
||||
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-shanghai");
|
||||
CommonClient client(key, secret, configuration);
|
||||
|
||||
CommonRequest request(CommonRequest::RoaPattern);
|
||||
request.setScheme("http");
|
||||
request.setDomain("nlp.cn-shanghai.aliyuncs.com");
|
||||
request.setResourcePath("/nlp/api/wordsegment/general");
|
||||
request.setHttpMethod(HttpRequest::Post);
|
||||
const char * data = "{\"lang\":\"ZH\",\"text\":\"Iphone is a bad choice 专用数据线\"}";
|
||||
|
||||
request.setContent(data, strlen(data));
|
||||
request.setHeaderParameter("Content-Type", "application/json;chrset=utf-8");
|
||||
request.setHeaderParameter("Accept", "application/json");
|
||||
request.setVersion("2018-04-08");
|
||||
|
||||
auto out = client.commonResponse(request);
|
||||
if (!out.isSuccess()) {
|
||||
cout << "error code: " << out.error().errorCode() << endl;
|
||||
cout << "error message: " << out.error().errorMessage() << endl;
|
||||
cout << "error host: " << out.error().host() << endl;
|
||||
cout << "error requestId: " << out.error().requestId() << endl;
|
||||
cout << "error detail: " << out.error().detail() << endl;
|
||||
ShutdownSdk();
|
||||
return -1;
|
||||
}
|
||||
cout << endl << "wordsegment retruns: " << out.result().payload() << std::endl << endl;
|
||||
ShutdownSdk();
|
||||
return 0;
|
||||
}
|
||||
17
examples/rds/CMakeLists.txt
Normal file
17
examples/rds/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(rds_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(rds_demo rds.cc)
|
||||
target_link_libraries(rds_demo alibabacloud-sdk-core alibabacloud-sdk-rds)
|
||||
|
||||
target_link_libraries(rds_demo )
|
||||
|
||||
set_target_properties(rds_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}rds_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
24
examples/rds/rds.cc
Normal file
24
examples/rds/rds.cc
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/rds/RdsClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
using namespace AlibabaCloud::Rds;
|
||||
|
||||
int main() {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
RdsClient client(key, secret, configuration);
|
||||
Model::DescribeDBInstancesRequest request;
|
||||
request.setPageSize(10);
|
||||
auto outcome = client.describeDBInstances(request);
|
||||
cout << endl << "rds describeDBInstances result" << endl;
|
||||
cout << "error code: " << outcome.error().errorCode() << endl;
|
||||
cout << "getTotalRecordCount: " << outcome.result().getTotalRecordCount() << endl << endl;
|
||||
ShutdownSdk();
|
||||
}
|
||||
17
examples/slb/CMakeLists.txt
Normal file
17
examples/slb/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(slb_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(slb_demo slb.cc)
|
||||
target_link_libraries(slb_demo alibabacloud-sdk-core alibabacloud-sdk-slb)
|
||||
|
||||
target_link_libraries(slb_demo )
|
||||
|
||||
set_target_properties(slb_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}slb_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
24
examples/slb/slb.cc
Normal file
24
examples/slb/slb.cc
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/slb/SlbClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
using namespace AlibabaCloud::Slb;
|
||||
|
||||
int main() {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
SlbClient client(key, secret, configuration);
|
||||
Model::DescribeLoadBalancersRequest request;
|
||||
request.setPageSize(10);
|
||||
auto outcome = client.describeLoadBalancers(request);
|
||||
cout << endl << "slb describeLoadBalancers result" << endl;
|
||||
cout << "error code: " << outcome.error().errorCode() << endl;
|
||||
cout << "totalCount: " << outcome.result().getTotalCount() << endl << endl;
|
||||
ShutdownSdk();
|
||||
}
|
||||
38
examples/utils.h
Normal file
38
examples/utils.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class utUtils {
|
||||
public:
|
||||
void get_dir_exec(char* dir, char* exec) {
|
||||
char* filename = nullptr;
|
||||
if (readlink("/proc/self/exe", dir, 1024) < 0) {
|
||||
dir[0] = '\0';
|
||||
return;
|
||||
}
|
||||
filename = strrchr(dir, '/');
|
||||
if (filename == nullptr) {
|
||||
dir[0] = '\0';
|
||||
return;
|
||||
}
|
||||
++filename;
|
||||
if (exec) {
|
||||
sprintf(exec, "%s", filename);
|
||||
}
|
||||
|
||||
*filename = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
std::string get_env(const std::string env) {
|
||||
char* value = getenv(env.c_str());
|
||||
if (value == nullptr) {
|
||||
return std::string();
|
||||
}
|
||||
return std::string(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
17
examples/vpc/CMakeLists.txt
Normal file
17
examples/vpc/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(vpc_demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
add_executable(vpc_demo vpc.cc)
|
||||
target_link_libraries(vpc_demo alibabacloud-sdk-core alibabacloud-sdk-vpc)
|
||||
|
||||
target_link_libraries(vpc_demo )
|
||||
|
||||
set_target_properties(vpc_demo
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}vpc_demo
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
24
examples/vpc/vpc.cc
Normal file
24
examples/vpc/vpc.cc
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/vpc/VpcClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
using namespace AlibabaCloud::Vpc;
|
||||
|
||||
int main() {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
VpcClient client(key, secret, configuration);
|
||||
Model::DescribeNatGatewaysRequest request;
|
||||
request.setPageSize(10);
|
||||
auto outcome = client.describeNatGateways(request);
|
||||
cout << endl << "vpc describeNatGateways result" << endl;
|
||||
cout << "error code: " << outcome.error().errorCode() << endl;
|
||||
cout << "totalCount: " << outcome.result().getTotalCount() << endl << endl;
|
||||
ShutdownSdk();
|
||||
}
|
||||
@@ -3,12 +3,19 @@
|
||||
cd `dirname $0`
|
||||
echo '-------build function test----------'
|
||||
|
||||
MAKE=make
|
||||
if command -v python > /dev/null ; then
|
||||
MAKE="make -j $(python -c 'import multiprocessing as mp; print(int(mp.cpu_count()))')"
|
||||
fi
|
||||
|
||||
echo $MAKE
|
||||
|
||||
FT_BUILD_DIR=ft_build
|
||||
rm -rf $FT_BUILD_DIR
|
||||
mkdir $FT_BUILD_DIR
|
||||
cd $FT_BUILD_DIR
|
||||
cmake -DBUILD_FUNCTION_TESTS=ON -DBUILD_UNIT_TESTS=OFF ..
|
||||
make cdn core ecs rds slb vpc cdn_ft core_ft ecs_ft nlp_ft rds_ft slb_ft vpc_ft
|
||||
$MAKE cdn core ecs rds slb vpc cdn_ft core_ft ecs_ft nlp_ft rds_ft slb_ft vpc_ft
|
||||
|
||||
echo '------- run function test -----------'
|
||||
|
||||
|
||||
@@ -59,7 +59,6 @@ namespace {
|
||||
|
||||
auto outcome = client.commonResponse(request);
|
||||
const string error = "{\"errorCode\":10007,\"errorMsg\":\"body json format invalid\"}";
|
||||
cout << "error: " << outcome.error().detail() << endl;
|
||||
EXPECT_TRUE(outcome.error().detail() == error);
|
||||
AlibabaCloud::ShutdownSdk();
|
||||
}
|
||||
|
||||
13
unit_test.sh
13
unit_test.sh
@@ -3,15 +3,20 @@
|
||||
cd `dirname $0`
|
||||
echo '-------build unit test----------'
|
||||
|
||||
MAKE=make
|
||||
if command -v python > /dev/null ; then
|
||||
MAKE="make -j $(python -c 'import multiprocessing as mp; print(int(mp.cpu_count()))')"
|
||||
fi
|
||||
|
||||
echo $MAKE
|
||||
|
||||
UT_BUILD_DIR=ut_build
|
||||
rm -rf $UT_BUILD_DIR
|
||||
mkdir $UT_BUILD_DIR
|
||||
cd $UT_BUILD_DIR
|
||||
cmake -DBUILD_FUNCTION_TESTS=OFF -DBUILD_UNIT_TESTS=ON ..
|
||||
make core_ut
|
||||
make httpserver_for_ut
|
||||
cmake -DBUILD_FUNCTION_TESTS=OFF -DBUILD_UNIT_TESTS=ON -DENABLE_COVERAGE=ON ..
|
||||
$MAKE core_ut httpserver_for_ut
|
||||
|
||||
echo '------- run unit test -----------'
|
||||
|
||||
ctest --verbose
|
||||
|
||||
|
||||
Reference in New Issue
Block a user