SDK 客户端的初始化和请求方式

本节主要介绍升级版 SDK 对于客户端的初始化以及 OpenAPI 请求方式。

SDK 客户端初始化

升级版 SDK 中因为具备完整的参数处理、返回值处理以及请求逻辑,所以可以给不同云产品的 SDK 生成不同的 Client 并包含其产品下所有的 OpenAPI 请求方法,可以使不同云产品之间的身份及鉴权信息隔离,使用上更加安全,下面是产品 ECS 的升级版 SDK 初始化示例:

TypeScript 版本:

import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';

export default class Client {
  static async main(): Promise<void> {
    let config = new $OpenApi.Config({
      // 您的AccessKey ID
      accessKeyId: 'AccessKeyId',
      // 您的AccessKey Secret
      accessKeySecret: 'AccessKeySecret',
      // 访问的区域
      regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
  }
}

JavaScript 版本:

const { default: Ecs20140526 } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');

async function main() {
  let config = new Config({
    // 您的AccessKey ID
    accessKeyId: 'AccessKeyId',
    // 您的AccessKey Secret
    accessKeySecret: 'AccessKeySecret',
    // 访问的区域
    regionId: 'RegionId'
  });
  const client = new Ecs20140526(config);
}

OpenAPI 请求方式

升级版 SDK 中每个产品的 Client 包含了所有的 OpenAPI,所以可以指定 OpenAPI 请求方法来请求,其方法名对应 OpenAPI 首字母小写,一般有三个:

  • 可以不用传入运行参数的请求方法,TypeScript 版:

import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';

export default class Client {
  static async main(): Promise<void> {
    const config = new $OpenApi.Config({
      // 您的AccessKey ID
      accessKeyId: 'AccessKeyId',
      // 您的AccessKey Secret
      accessKeySecret: 'AccessKeySecret',
      // 访问的区域
      regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
    const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
      resourceOwnerAccount: "test",
      resourceOwnerId: 1,
      ownerAccount: "test",
      regionId: "test",
    });
    const resp = await client.modifySecurityGroupRule(request);
    console.log(resp.headers);
    console.log(resp.body);
  }
}

JavaScript 版:

const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');

async function main() {
     const config = new Config({
       // 您的AccessKey ID
       accessKeyId: 'AccessKeyId',
       // 您的AccessKey Secret
       accessKeySecret: 'AccessKeySecret',
       // 访问的区域
       regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
    const request = new ModifySecurityGroupRuleRequest({
      resourceOwnerAccount: "test",
      resourceOwnerId: 1,
      ownerAccount: "test",
      regionId: "test",
    });
    const resp = await client.modifySecurityGroupRule(request);
    console.log(resp.headers);
    console.log(resp.body);
}
  • 需要传入运行参数的请求方法,TypeScript 版:

import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';
import * as $Util from '@alicloud/tea-util';

export default class Client {
  static async main(): Promise<void> {
    const config = new $OpenApi.Config({
      // 您的AccessKey ID
      accessKeyId: 'AccessKeyId',
      // 您的AccessKey Secret
      accessKeySecret: 'AccessKeySecret',
      // 访问的区域
      regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
    const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
      resourceOwnerAccount: "test",
      resourceOwnerId: 1,
      ownerAccount: "test",
      regionId: "test",
    });
    // 创建RuntimeObject实例并设置运行参数。
    const runtime = new $Util.RuntimeOptions({});
    const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
    console.log(resp.headers);
    console.log(resp.body);
  }
}

JavaScript 版:

const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');
const { RuntimeOptions } = require('@alicloud/tea-util');

async function main() {
  const config = new Config({
    // 您的AccessKey ID
    accessKeyId: 'AccessKeyId',
    // 您的AccessKey Secret
    accessKeySecret: 'AccessKeySecret',
    // 访问的区域
    regionId: 'RegionId'
  });
  const client = new Ecs20140526(config);
  const request = new ModifySecurityGroupRuleRequest({
    resourceOwnerAccount: "test",
    resourceOwnerId: 1,
    ownerAccount: "test",
    regionId: "test",
  });
  // 创建RuntimeObject实例并设置运行参数。
  const runtime = new RuntimeOptions({});
  const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
  console.log(resp.headers);
  console.log(resp.body);
}
  • 需要进行文件上传的请求方法,该方法默认需要传入运行时参数,这种方法只有需要用到文件上传的 OpenAPI 才会具备,TypeScript 版:

import Facebody20191230, * as $Facebody20191230 from '@alicloud/facebody20191230';
import * as $OpenApi from '@alicloud/openapi-client';
import * as $Util from '@alicloud/tea-util';
import { createReadStream } from 'fs';

export default class Client {
  static async main(): Promise<void> {
    const config = new $OpenApi.Config({
      // 您的AccessKey ID
      accessKeyId: 'AccessKeyId',
      // 您的AccessKey Secret
      accessKeySecret: 'AccessKeySecret',
      // 访问的区域
      regionId: 'RegionId'
    });
    const client = new Facebody20191230(config);
    const request = new $Facebody20191230.DetectBodyCountAdvanceRequest({
      imageURLObject: createReadStream("本地文件路径"),
    });
    // 创建RuntimeObject实例并设置运行参数。
    const runtime = new $Util.RuntimeOptions({});
    const resp = await client.DetectBodyCountAdvance(request, runtime);
    console.log(resp.headers);
    console.log(resp.body);
  }
}

JavaScript 版:

const { default: Facebody20191230, DetectBodyCountAdvanceRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');
const { RuntimeOptions } = require('@alicloud/tea-util');
const { createReadStream } = require('fs');

async function main() {
  const config = new Config({
    // 您的AccessKey ID
    accessKeyId: 'AccessKeyId',
    // 您的AccessKey Secret
    accessKeySecret: 'AccessKeySecret',
    // 访问的区域
    regionId: 'RegionId'
  });
  const client = new Facebody20191230(config);
  const request = new DetectBodyCountAdvanceRequest({
    imageURLObject: createReadStream("本地文件路径"),
  });
  // 创建RuntimeObject实例并设置运行参数。
  const runtime = new RuntimeOptions({});
  const resp = await client.DetectBodyCountAdvance(request, runtime);
  console.log(resp.headers);
  console.log(resp.body);
}
说明

每个接口 API 都有独立的 Request ,命名方式为 ${API}${Request} ,例如 DescribeRegionRequest。Response 亦如此。