Photos example (API Gateway models and mapping templates)
The following example shows a photo album API in API Gateway. We provide an example data transformation, additional models, and mapping templates. For more information about data transformations, see Understanding mapping templates. For more information about data models, see Understanding data models.
Topics
Example data transformation
The following example shows how you can transform input data about photos by using a Velocity Template
Language (VTL) mapping template. For more information about the Velocity Template Language, see Apache Velocity - VTL Reference.
Input data |
|
Output mapping template |
|
Output data |
|
Input model for photo data
You can define a model for your input data. This input model requires that you upload one photo, and it specifies a minimum of 10 photos for each page. You can use this input model to generate an SDK or to turn on a request validation for your API.
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosInputModel", "type": "object", "properties": { "photos": { "type": "object", "required" : [ "photo" ], "properties": { "page": { "type": "integer" }, "pages": { "type": "string" }, "perpage": { "type": "integer", "minimum" : 10 }, "total": { "type": "string" }, "photo": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "owner": { "type": "string" }, "photographer_first_name" : {"type" : "string"}, "photographer_last_name" : {"type" : "string"}, "secret": { "type": "string" }, "server": { "type": "string" }, "farm": { "type": "integer" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } } } }
Output model for photo data
You can define a model for your output data. You can use this model for a method response model, which is necessary when you generate a strongly typed SDK for the API. This causes the output to be cast into an appropriate class in Java or Objective-C.
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosOutputModel", "type": "object", "properties": { "photos": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "photographedBy": { "type": "string" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } }
Input mapping template for photo data
You can define a mapping template to modify input data. You can modify input data for further function integration or integration responses.
#set($inputRoot = $input.path('$')) { "photos": { "page": $inputRoot.photos.page, "pages": "$inputRoot.photos.pages", "perpage": $inputRoot.photos.perpage, "total": "$inputRoot.photos.total", "photo": [ #foreach($elem in $inputRoot.photos.photo) { "id": "$elem.id", "owner": "$elem.owner", "photographer_first_name" : "$elem.photographer_first_name", "photographer_last_name" : "$elem.photographer_last_name", "secret": "$elem.secret", "server": "$elem.server", "farm": $elem.farm, "title": "$elem.title", "ispublic": $elem.ispublic, "isfriend": $elem.isfriend, "isfamily": $elem.isfamily }#if($foreach.hasNext),#end #end ] } }