Simple calculator API in API Gateway - Amazon API Gateway
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Simple calculator API in API Gateway

Our simple calculator API exposes three methods (GET, POST, GET) to invoke the Simple calculator Lambda function. A graphical representation of this API is shown as follows:

These three methods show different ways to supply the input for the backend Lambda function to perform the same operation:

  • The GET /?a=...&b=...&op=... method uses the query parameters to specify the input.

  • The POST / method uses a JSON payload of {"a":"Number", "b":"Number", "op":"string"} to specify the input.

  • The GET /{a}/{b}/{op} method uses the path parameters to specify the input.

If not defined, API Gateway generates the corresponding SDK method name by combining the HTTP method and path parts. The root path part (/) is referred to as Api Root. For example, the default Java SDK method name for the API method of GET /?a=...&b=...&op=... is getABOp, the default SDK method name for POST / is postApiRoot, and the default SDK method name for GET /{a}/{b}/{op} is getABOp. Individual SDKs may customize the convention. Consult the documentation in the generated SDK source for SDK specific method names.

You can, and should, override the default SDK method names by specifying the operationName property on each API method. You can do so when creating the API method or updating the API method using the API Gateway REST API. In the API Swagger definition, you can set the operationId to achieve the same result.

Before showing how to call these methods using an SDK generated by API Gateway for this API, let's recall briefly how to set them up. For detailed instructions, see Develop REST APIs in API Gateway. If you're new to API Gateway, see Choose an Amazon Lambda integration tutorial first.

Create models for input and output

To specify strongly typed input in the SDK, we create an Input model for the API. To describe the response body data type, we create an Output model and a Result model.

To create models for the input, output, and result
  1. In the main navigation pane, choose Models.

  2. Choose Create model.

  3. For Name, enter input.

  4. For Content type, enter application/json.

    If no matching content type is found, request validation is not performed. To use the same model regardless of the content type, enter $default.

  5. For Model schema, enter the following model:

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

  7. Repeat the following steps to create an Output model and a Result model.

    For the Output model, enter the following for the Model schema:

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

    For the Result model, enter the following for the Model schema. Replace the API ID abc123 with your 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" }

Set up GET / method query parameters

For the GET /?a=..&b=..&op=.. method, the query parameters are declared in Method Request:

To set up GET / URL query string parameters
  1. In the Method request section for the GET method on the root (/) resource, choose Edit.

  2. Choose URL query string parameters and do the following:

    1. Choose Add query string.

    2. For Name, enter a.

    3. Keep Required and Caching turned off.

    4. Keep Caching turned off.

    Repeat the same steps and create a query string named b and a query string named op.

  3. Choose Save.

Set up data model for the payload as input to the backend

For the POST / method, we create the Input model and add it to the method request to define the shape of input data.

To set up the data model for the payload as input to the backend
  1. In the Method request section, for the POST method on the root (/) resource choose Edit.

  2. Choose Request body.

  3. Choose Add model.

  4. For Content type, enter application/json.

  5. For Model, select Input.

  6. Choose Save.

With this model, your API customers can call the SDK to specify the input by instantiating an Input object. Without this model, your customers would be required to create dictionary object to represent the JSON input to the Lambda function.

Set up data model for the result output from the backend

For all three methods, we create the Result model and add it to the method's Method Response to define the shape of output returned by the Lambda function.

To set up the data model for the result output from the backend
  1. Select the /{a}/{b}/{op} resource, and then choose the GET method.

  2. On the Method response tab, under Response 200, choose Edit.

  3. Under Response body, choose Add model.

  4. For Content type, enter application/json.

  5. For Model, select Result.

  6. Choose Save.

With this model, your API customers can parse a successful output by reading properties of a Result object. Without this model, customers would be required to create dictionary object to represent the JSON output.