Skip to content

Commit 541e78a

Browse files
committed
remove tencentcloud sdk
1 parent c41d4fe commit 541e78a

File tree

16 files changed

+1301
-49
lines changed

16 files changed

+1301
-49
lines changed

library/common/abstract_client.js

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
const Credential = require("./credential");
2+
const sdkVersion = require("./sdk_version");
3+
const ClientProfile = require("./profile/client_profile");
4+
const Sign = require("./sign");
5+
const HttpConnection = require("./http/http_connection");
6+
const TencentCloudSDKHttpException = require("./exception/tencent_cloud_sdk_exception");
7+
8+
/**
9+
* @inner
10+
*/
11+
class AbstractClient {
12+
13+
/**
14+
* 实例化client对象
15+
* @param {string} endpoint 接入点域名
16+
* @param {string} version 产品版本
17+
* @param {Credential} credential 认证信息实例
18+
* @param {string} region 产品地域
19+
* @param {ClientProfile} profile 可选配置实例
20+
*/
21+
constructor(endpoint, version, credential, region, profile) {
22+
this.path = "/";
23+
24+
/**
25+
* 认证信息实例
26+
* @type {Credential || null}
27+
*/
28+
this.credential = credential || null;
29+
30+
/**
31+
* 产品地域
32+
* @type {string || null}
33+
*/
34+
this.region = region || null;
35+
this.sdkVersion = "SDK_NODEJS_" + sdkVersion;
36+
this.apiVersion = version;
37+
this.endpoint = endpoint;
38+
39+
/**
40+
* 可选配置实例
41+
* @type {ClientProfile}
42+
*/
43+
this.profile = profile || new ClientProfile();
44+
}
45+
46+
/**
47+
* @inner
48+
*/
49+
getEndpoint() {
50+
return this.profile.httpProfile.endpoint || this.endpoint;
51+
}
52+
53+
/**
54+
* @inner
55+
*/
56+
succRequest(resp, cb, data) {
57+
resp.deserialize(data);
58+
cb(null, resp);
59+
}
60+
61+
/**
62+
* @inner
63+
*/
64+
failRequest(err, cb) {
65+
cb(err, null);
66+
}
67+
68+
/**
69+
* @inner
70+
*/
71+
request(action, req, resp, cb) {
72+
this.doRequest(action, req).then(data => this.succRequest(resp, cb, data), error => this.failRequest(error, cb));
73+
}
74+
75+
/**
76+
* @inner
77+
*/
78+
doRequest(action, req) {
79+
let params = this.mergeData(req);
80+
params = this.formatRequestData(action, params);
81+
let optional = {
82+
timeout: this.profile.httpProfile.reqTimeout * 1000
83+
};
84+
return new Promise(
85+
(resolve, reject) => {
86+
HttpConnection.doRequest(this.profile.httpProfile.reqMethod,
87+
this.profile.httpProfile.protocol + this.getEndpoint() + this.path,
88+
params, (error, response, data) => {
89+
if (error) {
90+
reject(new TencentCloudSDKHttpException(error.message));
91+
} else if (response.statusCode !== 200) {
92+
const tcError = new TencentCloudSDKHttpException(response.statusMessage)
93+
tcError.httpCode = response.statusCode
94+
reject(tcError);
95+
} else {
96+
data = JSON.parse(data);
97+
if (data.Response.Error) {
98+
const tcError = new TencentCloudSDKHttpException(data.Response.Error.Message, data.Response.RequestId)
99+
tcError.code = data.Response.Error.Code
100+
reject(tcError);
101+
} else {
102+
resolve(data.Response);
103+
}
104+
}
105+
}, // callback
106+
optional) // doRequest
107+
;})
108+
}
109+
110+
/**
111+
* @inner
112+
*/
113+
mergeData(data, prefix="") {
114+
let ret = {};
115+
for (let k in data) {
116+
if (data[k] === null) {
117+
continue;
118+
}
119+
if (data[k] instanceof Array || data[k] instanceof Object) {
120+
Object.assign(ret, this.mergeData(data[k], prefix + k + "."));
121+
} else {
122+
ret[prefix + k] = data[k];
123+
}
124+
}
125+
return ret;
126+
}
127+
128+
/**
129+
* @inner
130+
*/
131+
formatRequestData(action, params) {
132+
params.Action = action;
133+
params.RequestClient = this.sdkVersion;
134+
params.Nonce= Math.round(Math.random() * 65535);
135+
params.Timestamp = Math.round(Date.now() / 1000);
136+
params.Version = this.apiVersion;
137+
138+
if (this.credential.secretId) {
139+
params.SecretId = this.credential.secretId;
140+
}
141+
142+
if (this.region) {
143+
params.Region = this.region;
144+
}
145+
146+
if (this.credential.token) {
147+
params.Token = this.credential.token;
148+
}
149+
150+
if (this.profile.signMethod) {
151+
params.SignatureMethod = this.profile.signMethod;
152+
}
153+
let signStr = this.formatSignString(params);
154+
155+
params.Signature = Sign.sign(this.credential.secretKey, signStr, this.profile.signMethod);
156+
return params;
157+
}
158+
159+
/**
160+
* @inner
161+
*/
162+
formatSignString (params) {
163+
let strParam = "";
164+
let keys = Object.keys(params);
165+
keys.sort();
166+
for (let k in keys) {
167+
//k = k.replace(/_/g, '.');
168+
strParam += ("&" + keys[k] + "=" + params[keys[k]]);
169+
}
170+
let strSign = this.profile.httpProfile.reqMethod.toLocaleUpperCase() + this.getEndpoint() +
171+
this.path + "?" + strParam.slice(1);
172+
return strSign;
173+
}
174+
175+
}
176+
module.exports = AbstractClient;

library/common/abstract_model.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @inner
3+
*/
4+
class AbstractModel {
5+
constructor() {
6+
7+
}
8+
9+
/**
10+
* @inner
11+
*/
12+
deserialize (params) {
13+
}
14+
15+
/**
16+
* 将object转化为json格式的string
17+
* @return {string}
18+
*/
19+
to_json_string() {
20+
return JSON.stringify(this);
21+
}
22+
23+
/**
24+
* 将json格式的string转化为object
25+
* @param {string} dataString
26+
*/
27+
from_json_string(dataString) {
28+
let params = JSON.parse(dataString);
29+
this.deserialize(params);
30+
}
31+
}
32+
module.exports = AbstractModel;

library/common/credential.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 认证信息类
3+
* @class
4+
*/
5+
class Credential {
6+
/**
7+
* @param {string} secretId
8+
* @param {string} secretKey
9+
* @param {string=} token
10+
*/
11+
constructor(secretId, secretKey, token) {
12+
/**
13+
* secretId,可在控制台获取
14+
* @type {string || null}
15+
*/
16+
this.secretId = secretId || null;
17+
18+
/**
19+
* secretKey,可在控制台获取
20+
* @type {string || null}
21+
*/
22+
this.secretKey = secretKey || null;
23+
24+
/**
25+
* token
26+
* @type {string || null}
27+
*/
28+
this.token = token || null
29+
}
30+
}
31+
module.exports = Credential;
32+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @inner
3+
*/
4+
class TencentCloudSDKHttpException extends Error {
5+
constructor(error, requestId="") {
6+
super(error);
7+
this.requestId = requestId || '';
8+
}
9+
10+
getMessage() {
11+
return this.message;
12+
}
13+
14+
getRequestId() {
15+
return this.requestId;
16+
}
17+
18+
toString() {
19+
return "[TencentCloudSDKException]" + "message:" + this.getMessage() + " requestId:" + this.getRequestId();
20+
}
21+
22+
toLocaleString() {
23+
return "[TencentCloudSDKException]" + "message:" + this.getMessage() + " requestId:" + this.getRequestId();
24+
}
25+
}
26+
module.exports = TencentCloudSDKHttpException
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const request = require('request');
2+
const QueryString = require("querystring");
3+
4+
/**
5+
* @inner
6+
*/
7+
class HttpConnection {
8+
static doRequest(method, url, data, callback, opt={}) {
9+
let req = {
10+
method: method,
11+
url: url,
12+
};
13+
if (method === "GET") {
14+
req.url += "?" + QueryString.stringify(data);
15+
} else {
16+
req.form = data;
17+
}
18+
Object.assign(req, opt);
19+
request(req, function (error, response, body) {
20+
/**
21+
* `.request` 的请求回调
22+
* @callback requestCallback
23+
* @param {Error} error 请求错误
24+
* @param {Object} response 请求响应
25+
* @param {String} body API 请求结果
26+
*/
27+
28+
callback(error, response, body);
29+
})
30+
}
31+
}
32+
module.exports = HttpConnection;

library/common/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
HttpConnection: require("./http/http_connection"),
3+
ClientProfile: require("./profile/client_profile"),
4+
HttpProfile: require("./profile/http_profile"),
5+
Credential: require("./credential"),
6+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const HttpProfile = require("./http_profile");
2+
3+
/**
4+
* 可选参数类
5+
* @class
6+
*/
7+
class ClientProfile {
8+
9+
/**
10+
* @param {string} signMethod 签名方法,当前支持(HmacSHA1 HmacSHA256)
11+
* @param {HttpProfile} httpProfile http相关选项实例
12+
*/
13+
constructor(signMethod, httpProfile) {
14+
/**
15+
* 签名方法,当前支持(HmacSHA1 HmacSHA256)
16+
* @type {string}
17+
*/
18+
this.signMethod = signMethod || "HmacSHA256";
19+
20+
/**
21+
* http相关选项实例
22+
* @type {httpProfile}
23+
*/
24+
this.httpProfile = httpProfile || new HttpProfile();
25+
}
26+
}
27+
module.exports = ClientProfile
28+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* http可选参数类
3+
* @class
4+
*/
5+
class HttpProfile {
6+
7+
/**
8+
* @param {string} protocol 协议,目前支持(https://)
9+
* @param {string} endpoint 接入点域名,形如(cvm.ap-shanghai.tencentcloud.com)
10+
* @param {string} reqMethod 请求方法,目前支持(POST GET)
11+
* @param {number} reqTimeout 请求超时时间,默认60s
12+
*/
13+
constructor(protocol, endpoint, reqMethod, reqTimeout) {
14+
/**
15+
* 请求方法,目前支持(POST GET)
16+
* @type {string}
17+
*/
18+
this.reqMethod = reqMethod || "POST";
19+
20+
/**
21+
* 接入点域名,形如(cvm.ap-shanghai.tencentcloud.com)
22+
* @type {string || null}
23+
*/
24+
this.endpoint = endpoint || null;
25+
26+
/**
27+
* 协议,目前支持(https://)
28+
* @type {string}
29+
*/
30+
this.protocol = protocol || "https://";
31+
32+
/**
33+
* 请求超时时间,默认60s
34+
* @type {number}
35+
*/
36+
this.reqTimeout = reqTimeout || 60;
37+
}
38+
}
39+
module.exports = HttpProfile;

library/common/sdk_version.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const sdkVersion = "3.0.92";
2+
module.exports = sdkVersion

0 commit comments

Comments
 (0)