-
Notifications
You must be signed in to change notification settings - Fork 275
/
api.yaml
403 lines (388 loc) · 15.8 KB
/
api.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
openapi: "3.0.1"
info:
title: "URL Shortener API"
version: "1.0.0"
x-amazon-apigateway-request-validators:
all:
validateRequestBody: true
validateRequestParameters: true
params:
validateRequestBody: false
validateRequestParameters: true
body:
validateRequestBody: true
validateRequestParameters: false
paths:
/{linkId}:
## URL redirector
get:
summary: Get a url by ID and redirect
x-amazon-apigateway-request-validator: params
parameters:
- in: path
name: linkId
schema:
type: string
required: true
description: Short link ID for full URL
responses:
"301":
description: "301 redirect"
headers:
Location:
type: "string"
Cache-Control:
type: "string"
## API Gateway Integration
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ DDBReadRole, Arn ]
uri: {"Fn::Sub":"arn:aws:apigateway:${AWS::Region}:dynamodb:action/GetItem"}
httpMethod: "POST"
requestTemplates:
application/json: {"Fn::Sub": "{\"Key\": {\"id\": {\"S\": \"$input.params().path.linkId\"}}, \"TableName\": \"${LinkTable}\"}"}
passthroughBehavior: "when_no_templates"
responses:
"200":
statusCode: "301"
responseParameters:
method.response.header.Location: {"Fn::Sub": ["'master.${ampDomain}?error=url_not_found'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}
method.response.header.Cache-Control: "'max-age=300'"
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \
#if($inputRoot.toString().contains(\"Item\")) \
#set($context.responseOverride.header.Location = $inputRoot.Item.url.S) \
#end"
type: "aws"
/app:
## Get all links for user
get:
summary: Fetch all links for authenticated user
security:
- UserAuthorizer: []
parameters:
- $ref: '#/components/parameters/authHeader'
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
type: "string"
Cache-Control:
type: "string"
## API Gateway Integration
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ DDBReadRole, Arn ]
uri: {"Fn::Sub":"arn:aws:apigateway:${AWS::Region}:dynamodb:action/Query"}
httpMethod: "POST"
requestTemplates:
application/json: { "Fn::Sub": "{\"TableName\": \"${LinkTable}\", \
\"IndexName\":\"OwnerIndex\",\"KeyConditionExpression\": \"#n_owner = :v_owner\", \
\"ExpressionAttributeValues\": \
{\":v_owner\": {\"S\": \"$context.authorizer.claims.email\"}},\"ExpressionAttributeNames\": {\"#n_owner\": \"owner\"}}"}
passthroughBehavior: "when_no_templates"
responses:
"200":
statusCode: "200"
responseParameters:
method.response.header.Cache-Control: "'no-cache, no-store'"
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
responseTemplates:
application/json: "#set($inputRoot = $input.path('$'))[ \
#foreach($elem in $inputRoot.Items) { \
\"id\":\"$elem.id.S\", \
\"url\": \"$elem.url.S\", \
\"timestamp\": \"$elem.timestamp.S\", \
\"owner\": \"$elem.owner.S\"} \
#if($foreach.hasNext),#end \
#end]"
type: "AWS"
## Create a new link
post:
summary: Create new url
security:
- UserAuthorizer: []
x-amazon-apigateway-request-validator: body
parameters:
- $ref: '#/components/parameters/authHeader'
requestBody:
description: Optional description in *Markdown*
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PostBody'
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
type: "string"
"400":
description: "400 response"
headers:
Access-Control-Allow-Origin:
type: "string"
## API Gateway integration
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ DDBCrudRole, Arn ]
uri: { "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/UpdateItem" }
httpMethod: "POST"
requestTemplates:
application/json: { "Fn::Sub": "{\"TableName\": \"${LinkTable}\",\
\"ConditionExpression\":\"attribute_not_exists(id)\", \
\"Key\": {\"id\": {\"S\": $input.json('$.id')}}, \
\"ExpressionAttributeNames\": {\"#u\": \"url\",\"#o\": \"owner\",\"#ts\": \"timestamp\"}, \
\"ExpressionAttributeValues\":{\":u\": {\"S\": $input.json('$.url')},\":o\": {\"S\": \"$context.authorizer.claims.email\"},\":ts\": {\"S\": \"$context.requestTime\"}}, \
\"UpdateExpression\": \"SET #u = :u, #o = :o, #ts = :ts\", \
\"ReturnValues\": \"ALL_NEW\"}" }
passthroughBehavior: "when_no_templates"
responses:
"200":
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \
{\"id\":\"$inputRoot.Attributes.id.S\", \
\"url\":\"$inputRoot.Attributes.url.S\",
\"timestamp\":\"$inputRoot.Attributes.timestamp.S\", \
\"owner\":\"$inputRoot.Attributes.owner.S\"}"
"400":
statusCode: "400"
responseParameters:
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \
#if($inputRoot.toString().contains(\"ConditionalCheckFailedException\")) \
#set($context.responseOverride.status = 200)
{\"error\": true,\"message\": \"URL link already exists\"} \
#end"
type: "aws"
## Options for get and post that do not have a linkId
options:
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
Access-Control-Allow-Methods:
schema:
type: "string"
Access-Control-Allow-Headers:
schema:
type: "string"
x-amazon-apigateway-integration:
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'POST, GET, OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'authorization, content-type'"
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
type: "mock"
/app/{linkId}:
## Delete link
delete:
summary: Delete url
security:
- UserAuthorizer: []
x-amazon-apigateway-request-validator: params
parameters:
- $ref: '#/components/parameters/authHeader'
- $ref: '#/components/parameters/linkIdHeader'
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
type: "string"
"400":
description: "400 response"
## AOI gateway integration
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ DDBCrudRole, Arn ]
uri: { "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/DeleteItem" }
httpMethod: "POST"
requestTemplates:
application/json: { "Fn::Sub": "{\"Key\": {\"id\": {\"S\": \"$input.params().path.linkId\"}}, \
\"TableName\": \"${LinkTable}\", \
\"ConditionExpression\": \"#owner = :owner\", \
\"ExpressionAttributeValues\":{\":owner\": {\"S\": \"$context.authorizer.claims.email\"}}, \
\"ExpressionAttributeNames\": {\"#owner\": \"owner\"}}" }
passthroughBehavior: "when_no_templates"
responses:
"200":
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
"400":
statusCode: "400"
responseTemplates:
application/json: "{\"error\": {\"message\":\"Either you do not have permission to do this operation or a parameter is missing\"}}"
type: "aws"
## Update links
put:
summary: Update specific URL
security:
- UserAuthorizer: []
x-amazon-apigateway-request-validator: body
requestBody:
description: Optional description in *Markdown*
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PutBody'
parameters:
- $ref: '#/components/parameters/authHeader'
- $ref: '#/components/parameters/linkIdHeader'
responses:
"200":
description: "301 response"
headers:
Access-Control-Allow-Origin:
type: "string"
"400":
description: "400 response"
headers:
Access-Control-Allow-Origin:
type: "string"
## API gateway integration
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ DDBCrudRole, Arn ]
uri: { "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/UpdateItem" }
httpMethod: "POST"
requestTemplates:
application/json: { "Fn::Sub": "{\"TableName\": \"${LinkTable}\",\
\"Key\": {\"id\": {\"S\": $input.json('$.id')}}, \
\"ExpressionAttributeNames\": {\"#u\": \"url\", \"#owner\": \"owner\", \"#id\":\"id\"}, \
\"ExpressionAttributeValues\":{\":u\": {\"S\": $input.json('$.url')}, \":owner\": {\"S\": \"$context.authorizer.claims.email\"}, \":linkId\":{\"S\":\"$input.params().path.linkId\"}}, \
\"UpdateExpression\": \"SET #u = :u\", \
\"ReturnValues\": \"ALL_NEW\", \
\"ConditionExpression\": \"#owner = :owner AND #id = :linkId\"}" }
passthroughBehavior: "when_no_templates"
responses:
"200":
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \
{\"id\":\"$inputRoot.Attributes.id.S\", \
\"url\":\"$inputRoot.Attributes.url.S\",
\"timestamp\":\"$inputRoot.Attributes.timestamp.S\", \
\"owner\":\"$inputRoot.Attributes.owner.S\"}"
"400":
statusCode: "400"
responseParameters:
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
application/json: "{\"error\": {\"message\":\"Either you do not have permission to do this operation or a parameter is missing\"}}"
type: "aws"
options:
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
Access-Control-Allow-Methods:
schema:
type: "string"
Access-Control-Allow-Headers:
schema:
type: "string"
x-amazon-apigateway-integration:
requestTemplates:
application/json: "{\"statusCode\" : 200}"
passthroughBehavior: "when_no_match"
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'PUT, DELETE, OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'authorization, content-type'"
method.response.header.Access-Control-Allow-Origin: {"Fn::If": ["IsLocal", "'*'", {"Fn::Sub": ["'https://master.${ampDomain}'", { "ampDomain": {"Fn::GetAtt":["AmplifyApp", "DefaultDomain"]} }]}]}
type: "mock"
## Validation models
components:
schemas:
PostBody:
type: object
properties:
id:
type: string
url:
type: string
pattern: ^https?://[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
required:
- id
- url
PutBody:
type: object
properties:
id:
type: string
url:
type: string
pattern: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/
timestamp:
type: string
owner:
type: string
required:
- id
- url
- timestamp
- owner
parameters:
authHeader:
in: header
name: Authorization
required: true
description: Contains authorization token
schema:
type: string
linkIdHeader:
in: path
name: linkId
required: true
description: Short link ID for full URL
schema:
type: string
## Authorizer definition
securityDefinitions:
UserAuthorizer:
type: "apiKey"
name: "Authorization"
in: "header"
x-amazon-apigateway-authtype: "cognito_user_pools"
x-amazon-apigateway-authorizer:
providerARNs:
- Fn::GetAtt: [ UserPool, Arn ]
type: "cognito_user_pools"