swagger for API gateway
Below is the sample snippet for get end point for pets based on id. For this endpoint pet id is passed as path parameter.
In the below snippet few values need to be substituted for it to work. PetsByIdGetLambda.Arn refers to the lambda resource with name PetsByIdGetLambda. This resource needs to be available in the SAM template and reference should be valid. Alternatively we could use the acutal arn of the lambda but it will have to be substituted as part of the provisioning process. Alternative approaches involves using function import and export available in cloudformation. We could also use shell script to token replace the value based on parameters or hard code the value for manual testing purpose and use the console to import the API
swagger: 2.0
basePath: /prod
info:
title: Pet store API
schemes:
- https
paths:
'/api/v1/pets/{id}':
get:
produces:
- application/json
parameters:
- name: id_token
in: header
required: true
type: string
- name: limit
in: query
required: false
type: string
- name: access_token
in: header
required: true
type: string
- name: offset
in: query
required: false
type: string
- name: id
in: path
required: true
type: string
schema:
type: integer
minimum: 1
description: Fetches details of the pet based on id
responses:
'200':
description: 200 response
schema:
$ref: '#/definitions/petsbyidschema'
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'400':
description: 400 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'401':
description: 401 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'403':
description: 403 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'404':
description: 404 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'500':
description: 500 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
security:
- OAuth2: []
x-amazon-apigateway-request-validator: Validate query string parameters and headers
x-amazon-apigateway-integration:
uri: !Sub >-
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PetsByIdGetLambda.Arn}/invocations
responses:
.*Encountered empty access_token.*|.*Authorization request parameters are empty*.:
statusCode: '401'
responseTemplates:
application/json: |
{
"resource-path" : "$context.resourcePath",
"errorMessage" : "Encountered bad token. Token invalid and might be expired"
}
.*NoResultException.*|.*No entity found for query*.:
statusCode: '404'
responseTemplates:
application/json: |
{
"resource-path" : "$context.resourcePath",
"errorMessage" : "Resource not found"
}
default:
statusCode: '200'
passthroughBehavior: when_no_match
httpMethod: POST
cacheNamespace: 8y1w8l
cacheKeyParameters:
- method.request.querystring.offset
- method.request.querystring.limit
- method.request.header.id_token
- method.request.header.access_token
contentHandling: CONVERT_TO_TEXT
type: aws_proxy
options:
consumes:
- application/json
produces:
- application/json
responses:
'200':
description: 200 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
x-amazon-apigateway-integration:
responses:
default:
statusCode: '200'
responseParameters:
method.response.header.Access-Control-Max-Age: '''600'''
method.response.header.Access-Control-Allow-Methods: '''GET,OPTIONS,POST,PUT'''
method.response.header.Access-Control-Allow-Headers: |-
'Content-Type, Authorization, X-Amz-Date, X-Api-Key,
X-Amz-Security-Token, access_token, id_token'
method.response.header.Access-Control-Allow-Origin: '''*'''
responseTemplates:
application/json: |
{}
requestTemplates:
application/json: |
{
"statusCode" : 200
}
passthroughBehavior: when_no_match
type: mock
In the below snippet few values need to be substituted for it to work. PetsByIdGetLambda.Arn refers to the lambda resource with name PetsByIdGetLambda. This resource needs to be available in the SAM template and reference should be valid. Alternatively we could use the acutal arn of the lambda but it will have to be substituted as part of the provisioning process. Alternative approaches involves using function import and export available in cloudformation. We could also use shell script to token replace the value based on parameters or hard code the value for manual testing purpose and use the console to import the API
swagger: 2.0
basePath: /prod
info:
title: Pet store API
schemes:
- https
paths:
'/api/v1/pets/{id}':
get:
produces:
- application/json
parameters:
- name: id_token
in: header
required: true
type: string
- name: limit
in: query
required: false
type: string
- name: access_token
in: header
required: true
type: string
- name: offset
in: query
required: false
type: string
- name: id
in: path
required: true
type: string
schema:
type: integer
minimum: 1
description: Fetches details of the pet based on id
responses:
'200':
description: 200 response
schema:
$ref: '#/definitions/petsbyidschema'
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'400':
description: 400 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'401':
description: 401 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'403':
description: 403 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'404':
description: 404 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
'500':
description: 500 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
security:
- OAuth2: []
x-amazon-apigateway-request-validator: Validate query string parameters and headers
x-amazon-apigateway-integration:
uri: !Sub >-
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PetsByIdGetLambda.Arn}/invocations
responses:
.*Encountered empty access_token.*|.*Authorization request parameters are empty*.:
statusCode: '401'
responseTemplates:
application/json: |
{
"resource-path" : "$context.resourcePath",
"errorMessage" : "Encountered bad token. Token invalid and might be expired"
}
.*NoResultException.*|.*No entity found for query*.:
statusCode: '404'
responseTemplates:
application/json: |
{
"resource-path" : "$context.resourcePath",
"errorMessage" : "Resource not found"
}
default:
statusCode: '200'
passthroughBehavior: when_no_match
httpMethod: POST
cacheNamespace: 8y1w8l
cacheKeyParameters:
- method.request.querystring.offset
- method.request.querystring.limit
- method.request.header.id_token
- method.request.header.access_token
contentHandling: CONVERT_TO_TEXT
type: aws_proxy
options:
consumes:
- application/json
produces:
- application/json
responses:
'200':
description: 200 response
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Max-Age:
type: string
Access-Control-Allow-Headers:
type: string
x-amazon-apigateway-integration:
responses:
default:
statusCode: '200'
responseParameters:
method.response.header.Access-Control-Max-Age: '''600'''
method.response.header.Access-Control-Allow-Methods: '''GET,OPTIONS,POST,PUT'''
method.response.header.Access-Control-Allow-Headers: |-
'Content-Type, Authorization, X-Amz-Date, X-Api-Key,
X-Amz-Security-Token, access_token, id_token'
method.response.header.Access-Control-Allow-Origin: '''*'''
responseTemplates:
application/json: |
{}
requestTemplates:
application/json: |
{
"statusCode" : 200
}
passthroughBehavior: when_no_match
type: mock
Comments
Post a Comment