(action, params = {}, opts = {})
| 162 | } |
| 163 | |
| 164 | async request(action, params = {}, opts = {}) { |
| 165 | const credentials = await this.credentialsProvider.getCredentials(); |
| 166 | // 1. compose params and opts |
| 167 | opts = Object.assign({ |
| 168 | headers: { |
| 169 | 'x-sdk-client': helper.DEFAULT_CLIENT, |
| 170 | 'user-agent': helper.DEFAULT_UA, |
| 171 | 'x-acs-action': action, |
| 172 | 'x-acs-version': this.apiVersion |
| 173 | } |
| 174 | }, this.opts, opts); |
| 175 | |
| 176 | // format action until formatAction is false |
| 177 | if (opts.formatAction !== false) { |
| 178 | action = firstLetterUpper(action); |
| 179 | } |
| 180 | |
| 181 | // format params until formatParams is false |
| 182 | if (opts.formatParams !== false) { |
| 183 | params = formatParams(params); |
| 184 | } |
| 185 | const defaultParams = { |
| 186 | Format: 'JSON', |
| 187 | Timestamp: timestamp(), |
| 188 | Version: this.apiVersion, |
| 189 | }; |
| 190 | if (credentials && credentials.accessKeyId && credentials.accessKeySecret) { |
| 191 | defaultParams.SignatureMethod = 'HMAC-SHA1'; |
| 192 | defaultParams.SignatureVersion = '1.0'; |
| 193 | defaultParams.SignatureNonce = kitx.makeNonce(); |
| 194 | defaultParams.AccessKeyId = credentials.accessKeyId; |
| 195 | if (credentials.securityToken) { |
| 196 | defaultParams.SecurityToken = credentials.securityToken; |
| 197 | } |
| 198 | } |
| 199 | params = Object.assign({ Action: action }, defaultParams, params); |
| 200 | |
| 201 | const method = (opts.method || 'GET').toUpperCase(); |
| 202 | const normalized = normalize(params); |
| 203 | // 2. caculate signature |
| 204 | if (credentials && credentials.accessKeyId && credentials.accessKeySecret) { |
| 205 | const canonicalized = canonicalize(normalized); |
| 206 | // 2.1 get string to sign |
| 207 | const stringToSign = `${method}&${encode('/')}&${encode(canonicalized)}`; |
| 208 | // 2.2 get signature |
| 209 | const key = credentials.accessKeySecret + '&'; |
| 210 | const signature = kitx.sha1(stringToSign, key, 'base64'); |
| 211 | // add signature |
| 212 | normalized.push(['Signature', encode(signature)]); |
| 213 | } |
| 214 | |
| 215 | // 3. generate final url |
| 216 | const url = opts.method === 'POST' ? `${this.endpoint}/` : `${this.endpoint}/?${canonicalize(normalized)}`; |
| 217 | // 4. send request |
| 218 | const entry = { |
| 219 | url: url, |
| 220 | request: null, |
| 221 | response: null |
no test coverage detected