|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { default: OpenApiUtil } = require('@alicloud/openapi-util'); |
| 4 | +const { default: Util } = require('@alicloud/tea-util'); |
| 5 | +const $Credential = require('@alicloud/credentials'); |
| 6 | +const $tea = require('@alicloud/tea-typescript'); |
| 7 | +const { default: Credential } = require('@alicloud/credentials'); |
| 8 | + |
| 9 | +export class OpenAPIClient { |
| 10 | + private _credential: any; |
| 11 | + private _endpoint: any; |
| 12 | + private _endpointType: any; |
| 13 | + private _protocol: any; |
| 14 | + private _regionId: any; |
| 15 | + private _userAgent: any; |
| 16 | + private _readTimeout: any; |
| 17 | + private _connectTimeout: any; |
| 18 | + private _httpsProxy: any; |
| 19 | + private _httpProxy: any; |
| 20 | + private _noProxy: any; |
| 21 | + private _socks5Proxy: any; |
| 22 | + private _socks5NetWork: any; |
| 23 | + private _maxIdleConns: any; |
| 24 | + private _signatureAlgorithm: any; |
| 25 | + constructor(config) { |
| 26 | + if (Util.isUnset($tea.toMap(config))) { |
| 27 | + throw $tea.newError({ |
| 28 | + code: 'ParameterMissing', |
| 29 | + message: "'config' can not be unset", |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + if (!Util.isUnset(config.credential)) { |
| 34 | + this._credential = config.credential; |
| 35 | + } else if (!Util.empty(config.accessKeyId) && !Util.empty(config.accessKeySecret)) { |
| 36 | + if (!Util.empty(config.securityToken)) { |
| 37 | + config.type = 'sts'; |
| 38 | + } else { |
| 39 | + config.type = 'access_key'; |
| 40 | + } |
| 41 | + |
| 42 | + let credentialConfig = new $Credential.Config({ |
| 43 | + accessKeyId: config.accessKeyId, |
| 44 | + type: config.type, |
| 45 | + accessKeySecret: config.accessKeySecret, |
| 46 | + securityToken: config.securityToken, |
| 47 | + }); |
| 48 | + this._credential = new Credential(credentialConfig); |
| 49 | + } |
| 50 | + |
| 51 | + this._endpoint = config.endpoint; |
| 52 | + this._endpointType = config.endpointType; |
| 53 | + this._protocol = config.protocol; |
| 54 | + this._regionId = config.regionId; |
| 55 | + this._userAgent = config.userAgent; |
| 56 | + this._readTimeout = config.readTimeout; |
| 57 | + this._connectTimeout = config.connectTimeout; |
| 58 | + this._httpProxy = config.httpProxy; |
| 59 | + this._httpsProxy = config.httpsProxy; |
| 60 | + this._noProxy = config.noProxy; |
| 61 | + this._socks5Proxy = config.socks5Proxy; |
| 62 | + this._socks5NetWork = config.socks5NetWork; |
| 63 | + this._maxIdleConns = config.maxIdleConns; |
| 64 | + this._signatureAlgorithm = config.signatureAlgorithm; |
| 65 | + } |
| 66 | + |
| 67 | + async getAccessKeyId() { |
| 68 | + if (Util.isUnset(this._credential)) { |
| 69 | + return ''; |
| 70 | + } |
| 71 | + |
| 72 | + let accessKeyId = await this._credential.getAccessKeyId(); |
| 73 | + return accessKeyId; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get accesskey secret by using credential |
| 78 | + * @return accesskey secret |
| 79 | + */ |
| 80 | + async getAccessKeySecret() { |
| 81 | + if (Util.isUnset(this._credential)) { |
| 82 | + return ''; |
| 83 | + } |
| 84 | + |
| 85 | + let secret = await this._credential.getAccessKeySecret(); |
| 86 | + return secret; |
| 87 | + } |
| 88 | + |
| 89 | + async getSecurityToken() { |
| 90 | + if (Util.isUnset(this._credential)) { |
| 91 | + return ''; |
| 92 | + } |
| 93 | + |
| 94 | + let token = await this._credential.getSecurityToken(); |
| 95 | + return token; |
| 96 | + } |
| 97 | + |
| 98 | + defaultAny(inputValue, defaultValue) { |
| 99 | + if (Util.isUnset(inputValue)) { |
| 100 | + return defaultValue; |
| 101 | + } |
| 102 | + |
| 103 | + return inputValue; |
| 104 | + } |
| 105 | + |
| 106 | + async doRequest(params, request, runtime) { |
| 107 | + let _runtime = { |
| 108 | + timeouted: 'retry', |
| 109 | + readTimeout: Util.defaultNumber(runtime.readTimeout, this._readTimeout), |
| 110 | + connectTimeout: Util.defaultNumber(runtime.connectTimeout, this._connectTimeout), |
| 111 | + httpProxy: Util.defaultString(runtime.httpProxy, this._httpProxy), |
| 112 | + httpsProxy: Util.defaultString(runtime.httpsProxy, this._httpsProxy), |
| 113 | + noProxy: Util.defaultString(runtime.noProxy, this._noProxy), |
| 114 | + maxIdleConns: Util.defaultNumber(runtime.maxIdleConns, this._maxIdleConns), |
| 115 | + retry: { |
| 116 | + retryable: runtime.autoretry, |
| 117 | + maxAttempts: Util.defaultNumber(runtime.maxAttempts, 3), |
| 118 | + }, |
| 119 | + backoff: { |
| 120 | + policy: Util.defaultString(runtime.backoffPolicy, 'no'), |
| 121 | + period: Util.defaultNumber(runtime.backoffPeriod, 1), |
| 122 | + }, |
| 123 | + ignoreSSL: runtime.ignoreSSL, |
| 124 | + }; |
| 125 | + |
| 126 | + let _lastRequest = null; |
| 127 | + let _now = Date.now(); |
| 128 | + let _retryTimes = 0; |
| 129 | + while ($tea.allowRetry(_runtime['retry'], _retryTimes, _now)) { |
| 130 | + if (_retryTimes > 0) { |
| 131 | + let _backoffTime = $tea.getBackoffTime(_runtime['backoff'], _retryTimes); |
| 132 | + if (_backoffTime > 0) { |
| 133 | + await $tea.sleep(_backoffTime); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + _retryTimes = _retryTimes + 1; |
| 138 | + let entry; |
| 139 | + try { |
| 140 | + let request_ = new $tea.Request(); |
| 141 | + request_.protocol = Util.defaultString(this._protocol, params.protocol); |
| 142 | + request_.method = params.method; |
| 143 | + request_.pathname = OpenApiUtil.getEncodePath(params.pathname); |
| 144 | + request_.query = OpenApiUtil.query(request.query); |
| 145 | + // endpoint is setted in product client |
| 146 | + request_.headers = { |
| 147 | + host: this._endpoint, |
| 148 | + 'x-acs-version': params.version, |
| 149 | + 'x-acs-action': params.action, |
| 150 | + 'user-agent': 'Alibaba Cloud API Toolkit', |
| 151 | + 'x-acs-date': OpenApiUtil.getTimestamp(), |
| 152 | + 'x-acs-signature-nonce': Util.getNonce(), |
| 153 | + accept: 'application/json', |
| 154 | + ...request.headers, |
| 155 | + }; |
| 156 | + if (request.headers && request.headers['Host']) { |
| 157 | + delete request_.headers['Host']; |
| 158 | + request_.headers['host'] = request.headers['Host']; |
| 159 | + } |
| 160 | + let signatureAlgorithm = Util.defaultString(this._signatureAlgorithm, 'ACS3-HMAC-SHA256'); |
| 161 | + let hashedRequestPayload = OpenApiUtil.hexEncode(OpenApiUtil.hash(Util.toBytes(''), signatureAlgorithm)); |
| 162 | + if (!Util.isUnset(request.stream)) { |
| 163 | + let tmp = await Util.readAsBytes(request.stream); |
| 164 | + hashedRequestPayload = OpenApiUtil.hexEncode(OpenApiUtil.hash(tmp, signatureAlgorithm)); |
| 165 | + request_.body = new $tea.BytesReadable(tmp); |
| 166 | + request_.headers['content-type'] = 'application/octet-stream'; |
| 167 | + } |
| 168 | + else { |
| 169 | + if (!Util.isUnset(request.body)) { |
| 170 | + if (Util.equalString(params.reqBodyType, 'json')) { |
| 171 | + let jsonObj = Util.toJSONString(request.body); |
| 172 | + hashedRequestPayload = OpenApiUtil.hexEncode(OpenApiUtil.hash(Util.toBytes(jsonObj), signatureAlgorithm)); |
| 173 | + request_.body = new $tea.BytesReadable(jsonObj); |
| 174 | + request_.headers['content-type'] = 'application/json; charset=utf-8'; |
| 175 | + } |
| 176 | + else { |
| 177 | + let m = Util.assertAsMap(request.body); |
| 178 | + let formObj = OpenApiUtil.toForm(m); |
| 179 | + hashedRequestPayload = OpenApiUtil.hexEncode(OpenApiUtil.hash(Util.toBytes(formObj), signatureAlgorithm)); |
| 180 | + request_.body = new $tea.BytesReadable(formObj); |
| 181 | + request_.headers['content-type'] = 'application/x-www-form-urlencoded'; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + request_.headers['x-acs-content-sha256'] = hashedRequestPayload; |
| 187 | + if (!Util.equalString(params.authType, 'Anonymous')) { |
| 188 | + let accessKeyId = await this.getAccessKeyId(); |
| 189 | + let accessKeySecret = await this.getAccessKeySecret(); |
| 190 | + let securityToken = await this.getSecurityToken(); |
| 191 | + if (!Util.empty(securityToken)) { |
| 192 | + request_.headers['x-acs-accesskey-id'] = accessKeyId; |
| 193 | + request_.headers['x-acs-security-token'] = securityToken; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + request_.headers['Authorization'] = OpenApiUtil.getAuthorization(request_, signatureAlgorithm, hashedRequestPayload, accessKeyId, accessKeySecret); |
| 198 | + } |
| 199 | + request_.headers['host'] = this._endpoint; |
| 200 | + if (request.headers && request.headers['Host']) { |
| 201 | + request_.headers['Host'] = request.headers['Host']; |
| 202 | + } |
| 203 | + |
| 204 | + _lastRequest = request_; |
| 205 | + console.log("_lastRequest",_lastRequest) |
| 206 | + |
| 207 | + let response_ = await $tea.doAction(request_, _runtime); |
| 208 | + entry = { |
| 209 | + request: { |
| 210 | + headers: request_.headers |
| 211 | + }, |
| 212 | + response: { |
| 213 | + statusCode: response_.statusCode, |
| 214 | + headers: response_.headers, |
| 215 | + } |
| 216 | + }; |
| 217 | + |
| 218 | + if (Util.is4xx(response_.statusCode) || Util.is5xx(response_.statusCode)) { |
| 219 | + let _res = await Util.readAsJSON(response_.body); |
| 220 | + let err = Util.assertAsMap(_res); |
| 221 | + return { |
| 222 | + format: 'json', |
| 223 | + result: err, |
| 224 | + entry |
| 225 | + }; |
| 226 | + } |
| 227 | + |
| 228 | + if (response_.statusCode === 204 && (Util.isUnset(response_.headers['content-type']) || Util.isUnset(response_.headers['content-length']))) { |
| 229 | + let str = await Util.readAsString(response_.body); |
| 230 | + return { |
| 231 | + format: 'string', |
| 232 | + result: str, |
| 233 | + entry |
| 234 | + }; |
| 235 | + } |
| 236 | + |
| 237 | + if (Util.equalString(params.bodyType, 'binary')) { |
| 238 | + return { |
| 239 | + format: 'binary', |
| 240 | + result: response_.body, |
| 241 | + entry |
| 242 | + }; |
| 243 | + } else if (Util.equalString(params.bodyType, 'byte')) { |
| 244 | + let byt = await Util.readAsBytes(response_.body); |
| 245 | + return { |
| 246 | + format: 'byte', |
| 247 | + result: byt, |
| 248 | + entry |
| 249 | + }; |
| 250 | + } else if (Util.equalString(params.bodyType, 'string')) { |
| 251 | + let str = await Util.readAsString(response_.body); |
| 252 | + return { |
| 253 | + format: 'string', |
| 254 | + result: str, |
| 255 | + entry |
| 256 | + }; |
| 257 | + } else if (Util.equalString(params.bodyType, 'json')) { |
| 258 | + let obj = await Util.readAsJSON(response_.body); |
| 259 | + let res = Util.assertAsMap(obj); |
| 260 | + return { |
| 261 | + format: 'json', |
| 262 | + result: res, |
| 263 | + entry |
| 264 | + }; |
| 265 | + } else if (Util.equalString(params.bodyType, 'array')) { |
| 266 | + let arr = await Util.readAsJSON(response_.body); |
| 267 | + return { |
| 268 | + format: 'json', |
| 269 | + result: arr, |
| 270 | + entry |
| 271 | + }; |
| 272 | + } |
| 273 | + let str = await Util.readAsString(response_.body); |
| 274 | + return { |
| 275 | + format: 'string', |
| 276 | + result: str, |
| 277 | + entry |
| 278 | + }; |
| 279 | + |
| 280 | + |
| 281 | + } catch (ex) { |
| 282 | + if ($tea.isRetryable(ex)) { |
| 283 | + continue; |
| 284 | + } |
| 285 | + ex.entry = entry; |
| 286 | + throw ex; |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + throw $tea.newUnretryableError(_lastRequest); |
| 291 | + } |
| 292 | +} |
| 293 | + |
| 294 | + |
| 295 | +exports.Client = OpenAPIClient; |
| 296 | +export default OpenAPIClient; |
0 commit comments