export async function request()

in src/util/http.ts [95:147]


export async function request(host: string, params: { [key: string]: any } = {}, opts: { [key: string]: any } = {}, accessKeySecret?: string): Promise<any> {
  // 1. compose params and opts
  let options: { [key: string]: any } = {
    headers: {
      'x-sdk-client': helper.DEFAULT_CLIENT,
      'user-agent': helper.DEFAULT_UA
    },
    ...opts
  };

  // format params until formatParams is false
  if (options.formatParams !== false) {
    params = formatParams(params);
  }
  params = {
    ..._buildParams(),
    ...params
  };

  // 2. calculate signature
  const method = (opts.method || 'GET').toUpperCase();
  const normalized = normalize(params);
  if (!options.anonymous) {
    const canonicalized = canonicalize(normalized);
    // 2.1 get string to sign
    const stringToSign = `${method}&${encode('/')}&${encode(canonicalized)}`;
    // 2.2 get signature
    const key = accessKeySecret + '&';
    const signature = kitx.sha1(stringToSign, key, 'base64') as string;
    // add signature
    normalized.push(['Signature', encode(signature)]);
  }
  // 3. generate final url
  const url = opts.method === 'POST' ? `${host}/` : `${host}/?${canonicalize(normalized)}`;
  // 4. send request
  if (opts.method === 'POST') {
    opts.headers = opts.headers || {};
    opts.headers['content-type'] = 'application/x-www-form-urlencoded';
    opts.data = canonicalize(normalized);
  }
  const response = await httpx.request(url, opts);
  const buffer = await httpx.read(response, 'utf8');
  const json = JSON.parse(buffer as string);
  if (json.Code && !STATUS_CODE.has(json.Code)) {
    const err = new Error(`${json.Message}`) as any;
    err.name = json.Code + 'Error';
    err.data = json;
    err.code = json.Code;
    err.url = url;
    throw err;
  }
  return json;
}