1 #include <iostream>
2 #include "alibabacloud/core/AlibabaCloud.h"
3 #include "alibabacloud/core/CommonClient.h"
4 #include "ucloud_ai_common.h"
5 #include <ulog/ulog.h>
6 
7 extern "C" {
8 using namespace std;
9 using namespace AlibabaCloud;
10 
11 static string accessKey;
12 static string accessSecret;
13 
14 #define USE_HTTPCLIENT
15 #define TAG "common"
16 
17 #ifdef USE_HTTPCLIENT
getResponseBodyByUrl(const char * url,const char ** buffer)18 int getResponseBodyByUrl(const char *url, const char **buffer)
19 {
20     int ret;
21     int recv_len;
22 
23     ret = ucloud_ai_connect((char *)url);
24     if (ret < 0) {
25         LOGE(TAG, "ucloud_ai_connect failed, ret: %d\n", ret);
26         return -1;
27     }
28 
29     recv_len = ucloud_ai_get_stream((char *)url, (char **)buffer);
30     ucloud_ai_disconnect();
31     if (!recv_len) {
32         LOGE(TAG, "recv_len is %d\n", recv_len);
33         return -1;
34     }
35 
36     return recv_len;
37 }
38 #else
getResponseBodyByUrl(const char * url,const char ** buffer)39 int getResponseBodyByUrl(const char *url, const char **buffer)
40 {
41     CurlHttpClient client;
42     HttpRequest request;
43 
44     Url tmpUrl;
45     tmpUrl.setHost(url);
46     tmpUrl.setScheme("http");
47 
48     request.setMethod(HttpRequest::Method::Get);
49     request.setUrl(tmpUrl);
50     request.setHeader("Accept", "*/*");
51     HttpClient::HttpResponseOutcome out = client.makeRequest(request);
52     *buffer = out.result().body();
53     return out.result().bodySize();
54 }
55 #endif
setAccesskeyAndSecret(char * key,char * secret)56 void setAccesskeyAndSecret(char *key, char *secret)
57 {
58     accessKey = key;
59     accessSecret = secret;
60 }
61 
getAccessKey(void)62 const char *getAccessKey(void)
63 {
64     return accessKey.c_str();
65 }
66 
getAccessSecret(void)67 const char *getAccessSecret(void)
68 {
69     return accessSecret.c_str();
70 }
71 
getTokenId(const char * domain,const char * regionId)72 char *getTokenId(const char *domain, const char *regionId)
73 {
74     InitializeSdk();
75     string key = getAccessKey();
76     string secret = getAccessSecret();
77     string regionIdStr = regionId;
78     string domainStr = domain;
79     ClientConfiguration configuration;
80     configuration.setRegionId(regionIdStr);
81     configuration.setEndpoint(domainStr);
82     CommonClient client(key, secret, configuration);
83     CommonRequest request(CommonRequest::RpcPattern);
84     char *response = NULL;
85 
86     request.setScheme("http");
87     request.setDomain(domainStr);
88     request.setHttpMethod(HttpRequest::Post);
89     request.setQueryParameter("Action", "CreateToken");
90     request.setVersion("2019-02-28");
91 
92     auto outcome = client.commonResponse(request);
93     if (!outcome.isSuccess()) {
94         cout << "error code:      " << outcome.error().errorCode() << endl;
95         cout << "error message:   " << outcome.error().errorMessage() << endl;
96         cout << "error host:      " << outcome.error().host() << endl;
97         cout << "error requestId: " << outcome.error().requestId() << endl;
98         cout << "error detail:    " << outcome.error().detail() << endl;
99         ShutdownSdk();
100         return NULL;
101     }
102     cout << endl << "add item returns: " << outcome.result().payload() << endl << endl;
103     response = strdup(outcome.result().payload().c_str());
104     ShutdownSdk();
105 
106     return response;
107 }
108 
109 }
110