|
| 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; |
0 commit comments