

# 为 API Gateway 中的 REST API 生成 SDK
<a name="how-to-generate-sdk"></a>

要以特定于平台或特定于语言的方式调用您的 REST API，您必须为 API 生成特定于平台或语言的软件开发工具包。在创建、测试 API 并将其部署到某个阶段后，您将生成 SDK。目前，API Gateway 支持使用 Java、JavaScript、Java for Android，以及 Objective-C for iOS 或 Swift for iOS 以及 Ruby 为 API 生成开发工具包。

本节介绍如何生成 API Gateway API 的开发工具包。其中还演示如何在 Java 应用程序、Java for Android 应用程序、Objective-C for iOS 应用程序、Swift for iOS 应用程序以及 JavaScript 应用程序中使用生成的开发工具包。

为了便于讨论，我们将使用此 API Gateway [API](simple-calc-lambda-api.md)，它公开了这个[简单计算器](simple-calc-nodejs-lambda-function.md) Lambda 函数。

继续操作之前，请在 API Gateway 中创建或导入 API 并至少部署一次。有关说明，请参阅 [在 API Gateway 中部署 REST API。](how-to-deploy-api.md)。

**Topics**
+ [

# 简单的计算器 Lambda 函数
](simple-calc-nodejs-lambda-function.md)
+ [

# API Gateway 中的简单计算器 API
](simple-calc-lambda-api.md)
+ [

# 简单计算器 API OpenAPI 定义
](simple-calc-lambda-api-swagger-definition.md)
+ [

# 在 API Gateway 中生成 API 的 Java SDK
](generate-java-sdk-of-an-api.md)
+ [

# 在 API Gateway 中生成 API 的 Android SDK
](generate-android-sdk-of-an-api.md)
+ [

# 在 API Gateway 中生成 API 的 iOS SDK
](generate-ios-sdk-of-an-api.md)
+ [

# 在 API Gateway 中生成 REST API 的 JavaScript SDK
](generate-javascript-sdk-of-an-api.md)
+ [

# 在 API Gateway 中生成 API 的 Ruby SDK
](generate-ruby-sdk-of-an-api.md)
+ [

# 在 API Gateway 中使用 Amazon CLI 命令为 API 生成 SDK
](how-to-generate-sdk-cli.md)

# 简单的计算器 Lambda 函数
<a name="simple-calc-nodejs-lambda-function"></a>

我们将使用一个执行加、减、乘、除二进制运算的 Node.js Lambda 函数作为示例。

**Topics**
+ [

## 简单计算器 Lambda 函数的输入格式
](#simple-calc-lambda-function-input-format)
+ [

## 简单计算器 Lambda 函数的输出格式
](#simple-calc-lambda-function-output-format)
+ [

## 简单计算器 Lambda 函数的实施
](#simple-calc-lambda-function-implementation)

## 简单计算器 Lambda 函数的输入格式
<a name="simple-calc-lambda-function-input-format"></a>

此函数使用以下格式的输入：

```
{ "a": "Number", "b": "Number", "op": "string"}
```

其中 `op` 可以是任意 `(+, -, *, /, add, sub, mul, div)`。

## 简单计算器 Lambda 函数的输出格式
<a name="simple-calc-lambda-function-output-format"></a>

如果运算成功，则返回以下格式的结果：

```
{ "a": "Number", "b": "Number", "op": "string", "c": "Number"}
```

其中 `c` 对应着计算结果。

## 简单计算器 Lambda 函数的实施
<a name="simple-calc-lambda-function-implementation"></a>

Lambda 函数的实施方式如下：

```
export const handler = async function (event, context) {
  console.log("Received event:", JSON.stringify(event));

  if (
    event.a === undefined ||
    event.b === undefined ||
    event.op === undefined
  ) {
    return "400 Invalid Input";
  }

  const res = {};
  res.a = Number(event.a);
  res.b = Number(event.b);
  res.op = event.op;
  if (isNaN(event.a) || isNaN(event.b)) {
    return "400 Invalid Operand";
  }
  switch (event.op) {
    case "+":
    case "add":
      res.c = res.a + res.b;
      break;
    case "-":
    case "sub":
      res.c = res.a - res.b;
      break;
    case "*":
    case "mul":
      res.c = res.a * res.b;
      break;
    case "/":
    case "div":
      if (res.b == 0) {
        return "400 Divide by Zero";
      } else {
        res.c = res.a / res.b;
      }
      break;
    default:
      return "400 Invalid Operator";
  }

  return res;
};
```

# API Gateway 中的简单计算器 API
<a name="simple-calc-lambda-api"></a>

我们的简单计算器 API 公开了三种方法（GET、POST、GET）来调用 [简单的计算器 Lambda 函数](simple-calc-nodejs-lambda-function.md)。此 API 的图形表示如下：

这三种方法显示为后端 Lambda 函数提供输入来执行相同操作的不同方式：
+ `GET /?a=...&b=...&op=...` 方法使用查询参数来指定输入。
+ `POST /` 方法使用 JSON 负载 `{"a":"Number", "b":"Number", "op":"string"}` 来指定输入。
+ `GET /{a}/{b}/{op}` 方法使用路径参数来指定输入。

如果未定义，API Gateway 通过将 HTTP 方法和路径部分组合起来，生成对应的开发工具包方法名称。根路径部分 (`/`) 称为 `Api Root`。例如，API 方法 `GET /?a=...&b=...&op=...` 的默认 Java 开发工具包方法名称为 `getABOp`，`POST /` 的默认开发工具包方法名称为 `postApiRoot`，`GET /{a}/{b}/{op}` 的默认开发工具包方法名称为 `getABOp`。单独的开发工具包可以自定义约定。对于开发工具包特定的方法名称，请参考生成的开发工具包源中的文档。

您可以并且应该通过在各个 API 方法上指定 [operationName](https://docs.amazonaws.cn/apigateway/latest/api/API_Method.html#operationName) 属性来覆盖默认开发工具包方法名称。您可在使用 API Gateway REST API [创建 API 方法](https://docs.amazonaws.cn/apigateway/latest/api/API_PutMethod.html)或[更新 API 方法](https://docs.amazonaws.cn/apigateway/latest/api/API_UpdateMethod.html)时这样做。在 API Swagger 定义中，您可以设置 `operationId` 以实现相同结果。

在演示如何使用 API Gateway 为该 API 生成的开发工具包来调用这些方法之前，我们先简单回想一下如何对其进行设置。有关详细说明，请参阅[开发 API Gateway 中的 REST API](rest-api-develop.md)。如果您是 API Gateway 的新用户，请先参阅[选择 Amazon Lambda 集成教程](getting-started-with-lambda-integration.md)。

## 创建用于输入和输出的模型
<a name="simple-calc-lambda-api-create-models-for-input-and-output"></a>

为了在 SDK 中指定强类型输入，我们为该 API 创建了一个 `Input` 模型。为了描述响应正文数据类型，我们创建了一个 `Output` 模型和一个 `Result` 模型。

**创建用于输入、输出和结果的模型**

1. 在主导航窗格中，选择**模型**。

1. 选择**创建模型**。

1. 对于**名称**，请输入 **input**。

1. 对于**内容类型**，输入 **application/json**。

   如果未找到匹配的内容类型，则不执行请求验证。要使用同一模型而不考虑内容类型，请输入 **\$1default**。

1. 对于**模型架构**，输入以下模型：

   ```
   {
       "$schema" : "$schema": "http://json-schema.org/draft-04/schema#",
       "type":"object",
       "properties":{
           "a":{"type":"number"},
           "b":{"type":"number"},
           "op":{"type":"string"}
       },
       "title":"Input"
   }
   ```

1. 选择**创建模型**。

1. 重复以下步骤以创建 `Output` 模型和 `Result` 模型。

   对于 `Output` 模型，为**模型架构**输入以下内容：

   ```
   {
       "$schema": "http://json-schema.org/draft-04/schema#",
       "type": "object",
       "properties": {
           "c": {"type":"number"}
       },
       "title": "Output"
   }
   ```

   对于 `Result` 模型，为**模型架构**输入以下内容。将 API ID `abc123` 替换为您的 API ID。

   ```
   {
       "$schema": "http://json-schema.org/draft-04/schema#",
       "type":"object",
       "properties":{
           "input":{
               "$ref":"https://apigateway.amazonaws.com/restapis/abc123/models/Input"
           },
           "output":{
               "$ref":"https://apigateway.amazonaws.com/restapis/abc123/models/Output"
           }
       },
       "title":"Result"
   }
   ```

## 设置 GET/方法查询参数
<a name="simple-calc-lambda-api-set-up-get-method-query-parameters"></a>

对于 `GET /?a=..&b=..&op=..` 方法，查询参数在 **Method Request (方法请求)** 中声明：

**设置 GET / URL 查询字符串参数**

1. 在**方法请求**部分，为根 (`/`) 资源上的 `GET` 方法选择**编辑**。

1. 选择 **URL 查询字符串参数**并执行以下操作：

   1. 选择**添加查询字符串**。

   1. 在**名称**中，输入 **a**。

   1. 保持**必填**和**缓存**为已关闭状态。

   1. 将**缓存**保持为关闭状态。

   重复相同的步骤，创建一个名为 **b** 的查询字符串和一个名为 **op** 的查询字符串。

1. 选择**保存**。

## 将负载的数据模型设置为后端的输入
<a name="simple-calc-lambda-api-set-up-post-method-body-data-type"></a>

对于 `POST /` 方法，我们创建了 `Input` 模型，并将其添加到方法请求中以定义输入数据的形状。

**将负载的数据模型设置为后端的输入**

1. 在**方法请求**部分，为根 (`/`) 资源上的 `POST` 方法选择**编辑**。

1. 选择**请求正文**。

1. 选择**添加模型**。

1. 对于**内容类型**，输入 **application/json**。

1. 对于**模型**，选择**输入**。

1. 选择**保存**。

使用此模型，您的 API 客户可以通过实例化 `Input` 对象来调用开发工具包以指定输入。若不使用此模型，您的客户将需要创建词典对象来表示 Lambda 函数的 JSON 输入。

## 为后端的结果输出设置数据模型
<a name="simple-calc-lambda-api-set-up-all-methods-result-data-type"></a>

对于所有三种方法，我们将创建 `Result` 模型并将其添加到方法的 `Method Response` 中，以定义 Lambda 函数返回的输出的形状。

**为后端的结果输出设置数据模型**

1. 选择 **/\$1a\$1/\$1b\$1/\$1op\$1** 资源，然后选择 **GET** 方法。

1. 在**方法响应**选项卡的**响应 200** 下，选择**编辑**。

1. 在**响应正文**下，选择**添加模型**。

1. 对于**内容类型**，输入 **application/json**。

1. 对于**模型**，选择**结果**。

1. 选择**保存**。

使用此模型，您的 API 客户可以通过读取 `Result` 对象的属性来解析成功的输出。若不使用此模型，您的客户将需要创建词典对象来表示 JSON 输出。

# 简单计算器 API OpenAPI 定义
<a name="simple-calc-lambda-api-swagger-definition"></a>

以下是简单计算器 API 的 OpenAPI 定义。您可以将其导入到您的账户中。但是，您需要在导入后对 [Lambda 函数](simple-calc-nodejs-lambda-function.md)重置基于资源的权限。要执行此操作，请从 API Gateway 控制台中的**集成请求**重新选择您在账户中创建的 Lambda 函数。这会让 API Gateway 控制台重置所需的权限。或者，您可以使用 Amazon Command Line Interface 来执行 [add-permission](https://docs.amazonaws.cn/cli/latest/reference/lambda/add-permission.html) 的 Lambda 命令。

------
#### [ OpenAPI 2.0 ]

```
{
  "swagger": "2.0",
  "info": {
    "version": "2016-09-29T20:27:30Z",
    "title": "SimpleCalc"
  },
  "host": "t6dve4zn25.execute-api.us-west-2.amazonaws.com",
  "basePath": "/demo",
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "op",
            "in": "query",
            "required": false,
            "type": "string"
          },
          {
            "name": "a",
            "in": "query",
            "required": false,
            "type": "string"
          },
          {
            "name": "b",
            "in": "query",
            "required": false,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Result"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "requestTemplates": {
            "application/json": "#set($inputRoot = $input.path('$'))\n{\n  \"a\" : $input.params('a'),\n  \"b\" : $input.params('b'),\n  \"op\" : \"$input.params('op')\"\n}"
          },
          "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:Calc/invocations",
          "passthroughBehavior": "when_no_templates",
          "httpMethod": "POST",
          "responses": {
            "default": {
              "statusCode": "200",
              "responseTemplates": {
                "application/json": "#set($inputRoot = $input.path('$'))\n{\n  \"input\" : {\n    \"a\" : $inputRoot.a,\n    \"b\" : $inputRoot.b,\n    \"op\" : \"$inputRoot.op\"\n  },\n  \"output\" : {\n    \"c\" : $inputRoot.c\n  }\n}"
              }
            }
          },
          "type": "aws"
        }
      },
      "post": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "Input",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Input"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Result"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:Calc/invocations",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "POST",
          "responses": {
            "default": {
              "statusCode": "200",
              "responseTemplates": {
                "application/json": "#set($inputRoot = $input.path('$'))\n{\n  \"input\" : {\n    \"a\" : $inputRoot.a,\n    \"b\" : $inputRoot.b,\n    \"op\" : \"$inputRoot.op\"\n  },\n  \"output\" : {\n    \"c\" : $inputRoot.c\n  }\n}"
              }
            }
          },
          "type": "aws"
        }
      }
    },
    "/{a}": {
      "x-amazon-apigateway-any-method": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "a",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "404": {
            "description": "404 response"
          }
        },
        "x-amazon-apigateway-integration": {
          "requestTemplates": {
            "application/json": "{\"statusCode\": 200}"
          },
          "passthroughBehavior": "when_no_match",
          "responses": {
            "default": {
              "statusCode": "404",
              "responseTemplates": {
                "application/json": "{ \"Message\" : \"Can't $context.httpMethod $context.resourcePath\" }"
              }
            }
          },
          "type": "mock"
        }
      }
    },
    "/{a}/{b}": {
      "x-amazon-apigateway-any-method": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "a",
            "in": "path",
            "required": true,
            "type": "string"
          },
          {
            "name": "b",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "404": {
            "description": "404 response"
          }
        },
        "x-amazon-apigateway-integration": {
          "requestTemplates": {
            "application/json": "{\"statusCode\": 200}"
          },
          "passthroughBehavior": "when_no_match",
          "responses": {
            "default": {
              "statusCode": "404",
              "responseTemplates": {
                "application/json": "{ \"Message\" : \"Can't $context.httpMethod $context.resourcePath\" }"
              }
            }
          },
          "type": "mock"
        }
      }
    },
    "/{a}/{b}/{op}": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "a",
            "in": "path",
            "required": true,
            "type": "string"
          },
          {
            "name": "b",
            "in": "path",
            "required": true,
            "type": "string"
          },
          {
            "name": "op",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Result"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "requestTemplates": {
            "application/json": "#set($inputRoot = $input.path('$'))\n{\n  \"a\" : $input.params('a'),\n  \"b\" : $input.params('b'),\n  \"op\" : \"$input.params('op')\"\n}"
          },
          "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:Calc/invocations",
          "passthroughBehavior": "when_no_templates",
          "httpMethod": "POST",
          "responses": {
            "default": {
              "statusCode": "200",
              "responseTemplates": {
                "application/json": "#set($inputRoot = $input.path('$'))\n{\n  \"input\" : {\n    \"a\" : $inputRoot.a,\n    \"b\" : $inputRoot.b,\n    \"op\" : \"$inputRoot.op\"\n  },\n  \"output\" : {\n    \"c\" : $inputRoot.c\n  }\n}"
              }
            }
          },
          "type": "aws"
        }
      }
    }
  },
  "definitions": {
    "Input": {
      "type": "object",
      "properties": {
        "a": {
          "type": "number"
        },
        "b": {
          "type": "number"
        },
        "op": {
          "type": "string"
        }
      },
      "title": "Input"
    },
    "Output": {
      "type": "object",
      "properties": {
        "c": {
          "type": "number"
        }
      },
      "title": "Output"
    },
    "Result": {
      "type": "object",
      "properties": {
        "input": {
          "$ref": "#/definitions/Input"
        },
        "output": {
          "$ref": "#/definitions/Output"
        }
      },
      "title": "Result"
    }
  }
}
```

------
#### [ OpenAPI 3.0 ]

```
{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "SimpleCalc",
    "version" : "2016-09-29T20:27:30Z"
  },
  "servers" : [ {
    "url" : "https://t6dve4zn25.execute-api.us-west-2.amazonaws.com/{basePath}",
    "variables" : {
      "basePath" : {
        "default" : "demo"
      }
    }
  } ],
  "paths" : {
    "/{a}/{b}" : {
      "x-amazon-apigateway-any-method" : {
        "parameters" : [ {
          "name" : "a",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "b",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        } ],
        "responses" : {
          "404" : {
            "description" : "404 response",
            "content" : { }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "mock",
          "responses" : {
            "default" : {
              "statusCode" : "404",
              "responseTemplates" : {
                "application/json" : "{ \"Message\" : \"Can't $context.httpMethod $context.resourcePath\" }"
              }
            }
          },
          "requestTemplates" : {
            "application/json" : "{\"statusCode\": 200}"
          },
          "passthroughBehavior" : "when_no_match"
        }
      }
    },
    "/{a}/{b}/{op}" : {
      "get" : {
        "parameters" : [ {
          "name" : "a",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "b",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "op",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "200 response",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/Result"
                }
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "httpMethod" : "POST",
          "uri" : "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:111122223333:function:Calc/invocations",
          "responses" : {
            "default" : {
              "statusCode" : "200",
              "responseTemplates" : {
                "application/json" : "#set($inputRoot = $input.path('$'))\n{\n  \"input\" : {\n    \"a\" : $inputRoot.a,\n    \"b\" : $inputRoot.b,\n    \"op\" : \"$inputRoot.op\"\n  },\n  \"output\" : {\n    \"c\" : $inputRoot.c\n  }\n}"
              }
            }
          },
          "requestTemplates" : {
            "application/json" : "#set($inputRoot = $input.path('$'))\n{\n  \"a\" : $input.params('a'),\n  \"b\" : $input.params('b'),\n  \"op\" : \"$input.params('op')\"\n}"
          },
          "passthroughBehavior" : "when_no_templates"
        }
      }
    },
    "/" : {
      "get" : {
        "parameters" : [ {
          "name" : "op",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "a",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "b",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "200 response",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/Result"
                }
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "httpMethod" : "POST",
          "uri" : "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:111122223333:function:Calc/invocations",
          "responses" : {
            "default" : {
              "statusCode" : "200",
              "responseTemplates" : {
                "application/json" : "#set($inputRoot = $input.path('$'))\n{\n  \"input\" : {\n    \"a\" : $inputRoot.a,\n    \"b\" : $inputRoot.b,\n    \"op\" : \"$inputRoot.op\"\n  },\n  \"output\" : {\n    \"c\" : $inputRoot.c\n  }\n}"
              }
            }
          },
          "requestTemplates" : {
            "application/json" : "#set($inputRoot = $input.path('$'))\n{\n  \"a\" : $input.params('a'),\n  \"b\" : $input.params('b'),\n  \"op\" : \"$input.params('op')\"\n}"
          },
          "passthroughBehavior" : "when_no_templates"
        }
      },
      "post" : {
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/Input"
              }
            }
          },
          "required" : true
        },
        "responses" : {
          "200" : {
            "description" : "200 response",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/Result"
                }
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "httpMethod" : "POST",
          "uri" : "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:111122223333:function:Calc/invocations",
          "responses" : {
            "default" : {
              "statusCode" : "200",
              "responseTemplates" : {
                "application/json" : "#set($inputRoot = $input.path('$'))\n{\n  \"input\" : {\n    \"a\" : $inputRoot.a,\n    \"b\" : $inputRoot.b,\n    \"op\" : \"$inputRoot.op\"\n  },\n  \"output\" : {\n    \"c\" : $inputRoot.c\n  }\n}"
              }
            }
          },
          "passthroughBehavior" : "when_no_match"
        }
      }
    },
    "/{a}" : {
      "x-amazon-apigateway-any-method" : {
        "parameters" : [ {
          "name" : "a",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        } ],
        "responses" : {
          "404" : {
            "description" : "404 response",
            "content" : { }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "mock",
          "responses" : {
            "default" : {
              "statusCode" : "404",
              "responseTemplates" : {
                "application/json" : "{ \"Message\" : \"Can't $context.httpMethod $context.resourcePath\" }"
              }
            }
          },
          "requestTemplates" : {
            "application/json" : "{\"statusCode\": 200}"
          },
          "passthroughBehavior" : "when_no_match"
        }
      }
    }
  },
  "components" : {
    "schemas" : {
      "Input" : {
        "title" : "Input",
        "type" : "object",
        "properties" : {
          "a" : {
            "type" : "number"
          },
          "b" : {
            "type" : "number"
          },
          "op" : {
            "type" : "string"
          }
        }
      },
      "Output" : {
        "title" : "Output",
        "type" : "object",
        "properties" : {
          "c" : {
            "type" : "number"
          }
        }
      },
      "Result" : {
        "title" : "Result",
        "type" : "object",
        "properties" : {
          "input" : {
            "$ref" : "#/components/schemas/Input"
          },
          "output" : {
            "$ref" : "#/components/schemas/Output"
          }
        }
      }
    }
  }
}
```

------

# 在 API Gateway 中生成 API 的 Java SDK
<a name="generate-java-sdk-of-an-api"></a>

以下过程介绍如何在 API Gateway 中生成 API 的 Java SDK。

**在 API Gateway 中生成 API 的 Java 开发工具包**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.amazonaws.cn/apigateway)。

1. 选择一个 REST API。

1. 选择 **Stages (阶段)**。

1. 在**阶段**窗格中，选择阶段的名称。

1. 打开**阶段操作**菜单，然后选择**生成 SDK**。

1. 对于**平台**，选择 **Java** 平台并执行以下操作：

   1.  对于**服务名称**，指定您的开发工具包的名称。例如，**SimpleCalcSdk**。这将成为开发工具包客户端类的名称。该名称与 pom.xml 文件 (位于开发工具包的项目文件夹内) 中 `<name>` 下的 `<project>` 标记相对应。请勿包括连字符。

   1.  对于 **Java Package Name (Java 包名称)**，指定您的开发工具包的程序包名称。例如，**examples.aws.apig.simpleCalc.sdk**。此程序包名称将用作开发工具包库的命名空间。请勿包括连字符。

   1.  对于 **Java Build System (Java 构建系统)**，输入 **maven** 或 **gradle** 以指定生成系统。

   1.  对于 **Java Group Id (Java 组 ID)**，输入您开发工具包项目的组标识符。例如，输入 **my-apig-api-examples**。此标识符与 `<groupId>` 文件 (位于开发工具包的项目文件夹内) 中 `<project>` 下的 `pom.xml` 标记相对应。

   1.  对于 **Java Artifact Id (Java 构件 ID)**，输入您开发工具包项目的构件标识符。例如，输入 **simple-calc-sdk**。此标识符与 `<artifactId>` 文件 (位于开发工具包的项目文件夹内) 中 `<project>` 下的 `pom.xml` 标记相对应。

   1.  对于 **Java Artifact Version (Java 构件版本)**，输入版本标识符字符串。例如，**1.0.0**。此版本标识符与 `<version>` 文件 (位于开发工具包的项目文件夹内) 中 `<project>` 下的 `pom.xml` 标记相对应。

   1. 对于 **Source Code License Text (源代码许可证文本)**，输入源代码的许可证文本（如果有）。

1. 选择**生成开发工具包**，然后按照屏幕上的指示下载 API Gateway 生成的开发工具包。

按照[使用由 API Gateway 为 REST API 生成的 Java 开发工具包](how-to-call-apigateway-generated-java-sdk.md)中的说明使用生成的开发工具包。

 每次更新 API 后，您必须重新部署 API，并重新生成开发工具包，才能添加这些更新。

# 在 API Gateway 中生成 API 的 Android SDK
<a name="generate-android-sdk-of-an-api"></a>

以下过程介绍如何在 API Gateway 中生成 API 的 Android SDK。

**在 API Gateway 中生成 API 的 Android 开发工具包**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.amazonaws.cn/apigateway)。

1. 选择一个 REST API。

1. 选择 **Stages (阶段)**。

1. 在**阶段**窗格中，选择阶段的名称。

1. 打开**阶段操作**菜单，然后选择**生成 SDK**。

1. 对于**平台**，选择 Android 平台并执行以下操作：

   1.  对于 **Group ID (组 ID)**，输入对应项目的唯一标识符。这将在 `pom.xml` 文件中使用（例如，**com.mycompany**）。

   1.  对于 **Invoker package (调用程序包)**，请为生成的客户端类输入命名空间（例如 **com.mycompany.clientsdk**）。

   1.  对于 **Artifact ID (构件 ID)**，输入已编译的 .jar 文件的名称（不含版本）。这将在 `pom.xml` 文件中使用（例如，**aws-apigateway-api-sdk**）。

   1. 对于 **Artifact version (构件版本)**，输入已生成客户端的构件版本号。此标识符将在 `pom.xml` 文件中使用且应遵循 *major*.*minor*.*patch* 模式（例如，**1.0.0**）。

1. 选择**生成开发工具包**，然后按照屏幕上的指示下载 API Gateway 生成的开发工具包。

按照[使用由 API Gateway 为 REST API 生成的 Android 开发工具包](how-to-generate-sdk-android.md)中的说明使用生成的开发工具包。

 每次更新 API 后，您必须重新部署 API，并重新生成开发工具包，才能添加这些更新。

# 在 API Gateway 中生成 API 的 iOS SDK
<a name="generate-ios-sdk-of-an-api"></a>

以下过程介绍如何在 API Gateway 中生成 API 的 iOS SDK。

**在 API Gateway 中生成 API 的 iOS 开发工具包**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.amazonaws.cn/apigateway)。

1. 选择一个 REST API。

1. 选择 **Stages (阶段)**。

1. 在**阶段**窗格中，选择阶段的名称。

1. 打开**阶段操作**菜单，然后选择**生成 SDK**。

1. 对于**平台**，选择 **iOS (Objective-C) 或 iOS (Swift)** 平台，然后执行以下操作：

   1. 在**前缀**框中键入唯一前缀。

     前缀效果如下所示：举例来说，如果您为 [SimpleCalc](simple-calc-lambda-api.md) API 的开发工具包分配 **SIMPLE\$1CALC** 前缀，并使用 `input`、`output` 和 `result` 模型，则生成的开发工具包将包含封装 API 的 `SIMPLE_CALCSimpleCalcClient` 类，其中包括方法请求/响应。此外，生成的开发工具包将包含分别表示输入、输出和结果的 `SIMPLE_CALCinput`、`SIMPLE_CALCoutput` 和 `SIMPLE_CALCresult`，从而表示请求输入和响应输出。有关更多信息，请参阅 [在 Objective-C 或 Swift 中使用由 API Gateway 为 REST API 生成的 iOS 开发工具包](how-to-generate-sdk-ios.md)。

1. 选择**生成开发工具包**，然后按照屏幕上的指示下载 API Gateway 生成的开发工具包。

按照[在 Objective-C 或 Swift 中使用由 API Gateway 为 REST API 生成的 iOS 开发工具包](how-to-generate-sdk-ios.md)中的说明使用生成的开发工具包。

 每次更新 API 后，您必须重新部署 API，并重新生成开发工具包，才能添加这些更新。

# 在 API Gateway 中生成 REST API 的 JavaScript SDK
<a name="generate-javascript-sdk-of-an-api"></a>

以下过程介绍如何在 API Gateway 中生成 API 的 JaveScript SDK。

**在 API Gateway 中生成 API 的 JavaScript 开发工具包**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.amazonaws.cn/apigateway)。

1. 选择一个 REST API。

1. 选择 **Stages (阶段)**。

1. 在**阶段**窗格中，选择阶段的名称。

1. 打开**阶段操作**菜单，然后选择**生成 SDK**。

1. 对于**平台**，选择 **JavaScript** 平台。

1. 选择**生成开发工具包**，然后按照屏幕上的指示下载 API Gateway 生成的开发工具包。

按照[使用由 API Gateway 为 REST API 生成的 JavaScript 开发工具包](how-to-generate-sdk-javascript.md)中的说明使用生成的开发工具包。

 每次更新 API 后，您必须重新部署 API，并重新生成开发工具包，才能添加这些更新。

# 在 API Gateway 中生成 API 的 Ruby SDK
<a name="generate-ruby-sdk-of-an-api"></a>

以下过程介绍如何在 API Gateway 中生成 API 的 Ruby SDK。

**在 API Gateway 中生成 API 的 Ruby 开发工具包**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.amazonaws.cn/apigateway)。

1. 选择一个 REST API。

1. 选择 **Stages (阶段)**。

1. 在**阶段**窗格中，选择阶段的名称。

1. 打开**阶段操作**菜单，然后选择**生成 SDK**。

1. 对于**平台**，选择 **Rubya** 平台并执行以下操作：

   1.  对于**服务名称**，指定您的开发工具包的名称。例如，**SimpleCalc**。这用于生成您的 API 的 Ruby Gem 命名空间。该名称必须是全字母形式 (`a-zA-Z`)，不含任何特殊字符或数字。

   1.  对于 **Ruby Gem Name (Ruby Gem 名称)**，指定 Ruby Gem 的名称，其中将包含为您 API 生成的开发工具包源代码。默认情况下，这是小写的服务名称加上 `-sdk` 后缀 — 例如 **simplecalc-sdk**。

   1.  对于 **Ruby Gem Version (Ruby Gem 版本)**，指定所生成 Ruby Gem 的版本号。默认情况下，将它设置为 `1.0.0`。

1. 选择**生成开发工具包**，然后按照屏幕上的指示下载 API Gateway 生成的开发工具包。

按照[使用由 API Gateway 为 REST API 生成的 Ruby 开发工具包](how-to-call-sdk-ruby.md)中的说明使用生成的开发工具包。

 每次更新 API 后，您必须重新部署 API，并重新生成开发工具包，才能添加这些更新。

# 在 API Gateway 中使用 Amazon CLI 命令为 API 生成 SDK
<a name="how-to-generate-sdk-cli"></a>

您可以使用 Amazon CLI，通过调用 [get-sdk](https://docs.amazonaws.cn/cli/latest/reference/apigateway/get-sdk.html) 命令为支持的平台生成 API 的开发工具包并下载。在下文中我们将针对一些支持的平台演示此操作。

**Topics**
+ [

## 使用 Amazon CLI 生成和下载 Java for Android 开发工具包
](#how-to-generate-sdk-cli-android)
+ [

## 使用 Amazon CLI 生成和下载 JavaScript 开发工具包
](#how-to-generate-sdk-cli-js)
+ [

## 使用 Amazon CLI 生成和下载 Ruby 开发工具包
](#how-to-generate-sdk-cli-ruby)

## 使用 Amazon CLI 生成和下载 Java for Android 开发工具包
<a name="how-to-generate-sdk-cli-android"></a>

要在指定阶段 (`udpuvvzbkc`) 生成并下载由 API 的 API Gateway (`test`) 生成的 Java for Android 开发工具包，请按以下所示调用命令：

```
aws apigateway get-sdk \
            --rest-api-id udpuvvzbkc \
            --stage-name test \
            --sdk-type android \
            --parameters groupId='com.mycompany',\
                invokerPackage='com.mycompany.myApiSdk',\ 
                artifactId='myApiSdk',\
                artifactVersion='0.0.1' \
            ~/apps/myApi/myApi-android-sdk.zip
```

`~/apps/myApi/myApi-android-sdk.zip` 的最后输入是名为 `myApi-android-sdk.zip` 的已下载开发工具包文件的路径。

## 使用 Amazon CLI 生成和下载 JavaScript 开发工具包
<a name="how-to-generate-sdk-cli-js"></a>

要在指定阶段 (`udpuvvzbkc`) 生成并下载由 API 的 API Gateway (`test`) 生成的 JavaScript 开发工具包，请按以下所示调用命令：

```
aws apigateway get-sdk \
            --rest-api-id udpuvvzbkc \
            --stage-name test \
            --sdk-type javascript \
            ~/apps/myApi/myApi-js-sdk.zip
```

`~/apps/myApi/myApi-js-sdk.zip` 的最后输入是名为 `myApi-js-sdk.zip` 的已下载开发工具包文件的路径。

## 使用 Amazon CLI 生成和下载 Ruby 开发工具包
<a name="how-to-generate-sdk-cli-ruby"></a>

要在指定阶段 (`udpuvvzbkc`) 上，生成并下载 API (`test`) 的 Ruby 开发工具包，请按以下所示调用命令：

```
aws apigateway get-sdk \
            --rest-api-id udpuvvzbkc \
            --stage-name test  \
            --sdk-type ruby \
            --parameters service.name=myApiRubySdk,ruby.gem-name=myApi,ruby.gem-version=0.01 \
            ~/apps/myApi/myApi-ruby-sdk.zip
```

`~/apps/myApi/myApi-ruby-sdk.zip` 的最后输入是名为 `myApi-ruby-sdk.zip` 的已下载开发工具包文件的路径。

 接下来，我们将说明如何使用生成的开发工具包来调用底层 API。有关更多信息，请参阅 [调用 API Gateway 中的 REST API](how-to-call-api.md)。