Skip to content

Commit d3b32e4

Browse files
authored
Merge pull request #409 from andre-bisa/add_APIGatewayProxyRequestEvent
Add `APIGatewayProxyRequestEvent`
2 parents 4ae770a + 7afdba3 commit d3b32e4

3 files changed

Lines changed: 254 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2021 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package feral.lambda
18+
package events
19+
20+
import io.circe.Decoder
21+
import natchez.Kernel
22+
import org.typelevel.ci.CIString
23+
24+
final case class APIGatewayProxyRequestEvent(
25+
body: Option[String],
26+
resource: String,
27+
path: String,
28+
httpMethod: String,
29+
isBase64Encoded: Boolean,
30+
queryStringParameters: Option[Map[String, String]],
31+
multiValueQueryStringParameters: Option[Map[String, List[String]]],
32+
pathParameters: Option[Map[String, String]],
33+
stageVariables: Option[Map[String, String]],
34+
headers: Option[Map[String, String]],
35+
multiValueHeaders: Option[Map[String, List[String]]]
36+
)
37+
38+
object APIGatewayProxyRequestEvent {
39+
40+
implicit def decoder: Decoder[APIGatewayProxyRequestEvent] = Decoder.forProduct11(
41+
"body",
42+
"resource",
43+
"path",
44+
"httpMethod",
45+
"isBase64Encoded",
46+
"queryStringParameters",
47+
"multiValueQueryStringParameters",
48+
"pathParameters",
49+
"stageVariables",
50+
"headers",
51+
"multiValueHeaders"
52+
)(APIGatewayProxyRequestEvent.apply)
53+
54+
implicit def kernelSource: KernelSource[APIGatewayProxyRequestEvent] =
55+
e =>
56+
Kernel(
57+
e.headers.getOrElse(Map.empty).map { case (name, value) => CIString(name) -> value })
58+
59+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package feral.lambda.events
18+
19+
import io.circe.Encoder
20+
21+
final case class APIGatewayProxyResponseEvent(
22+
statusCode: Int,
23+
body: String,
24+
isBase64Encoded: Boolean
25+
)
26+
27+
object APIGatewayProxyResponseEvent {
28+
29+
implicit def encoder: Encoder[APIGatewayProxyResponseEvent] = Encoder.forProduct3(
30+
"statusCode",
31+
"body",
32+
"isBase64Encoded"
33+
)(r => (r.statusCode, r.body, r.isBase64Encoded))
34+
35+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2021 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package feral.lambda.events
18+
19+
import io.circe.literal._
20+
import munit.FunSuite
21+
22+
class APIGatewayProxyEventSuite extends FunSuite {
23+
24+
import APIGatewayProxyEventSuite._
25+
26+
test("decoder") {
27+
event.as[APIGatewayProxyRequestEvent].toTry.get
28+
}
29+
30+
}
31+
32+
object APIGatewayProxyEventSuite {
33+
34+
def event = json"""
35+
{
36+
"body": "eyJ0ZXN0IjoiYm9keSJ9",
37+
"resource": "/{proxy+}",
38+
"path": "/path/to/resource",
39+
"httpMethod": "POST",
40+
"isBase64Encoded": true,
41+
"queryStringParameters": {
42+
"foo": "bar"
43+
},
44+
"multiValueQueryStringParameters": {
45+
"foo": [
46+
"bar"
47+
]
48+
},
49+
"pathParameters": {
50+
"proxy": "/path/to/resource"
51+
},
52+
"stageVariables": {
53+
"baz": "qux"
54+
},
55+
"headers": {
56+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
57+
"Accept-Encoding": "gzip, deflate, sdch",
58+
"Accept-Language": "en-US,en;q=0.8",
59+
"Cache-Control": "max-age=0",
60+
"CloudFront-Forwarded-Proto": "https",
61+
"CloudFront-Is-Desktop-Viewer": "true",
62+
"CloudFront-Is-Mobile-Viewer": "false",
63+
"CloudFront-Is-SmartTV-Viewer": "false",
64+
"CloudFront-Is-Tablet-Viewer": "false",
65+
"CloudFront-Viewer-Country": "US",
66+
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
67+
"Upgrade-Insecure-Requests": "1",
68+
"User-Agent": "Custom User Agent String",
69+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
70+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
71+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
72+
"X-Forwarded-Port": "443",
73+
"X-Forwarded-Proto": "https"
74+
},
75+
"multiValueHeaders": {
76+
"Accept": [
77+
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
78+
],
79+
"Accept-Encoding": [
80+
"gzip, deflate, sdch"
81+
],
82+
"Accept-Language": [
83+
"en-US,en;q=0.8"
84+
],
85+
"Cache-Control": [
86+
"max-age=0"
87+
],
88+
"CloudFront-Forwarded-Proto": [
89+
"https"
90+
],
91+
"CloudFront-Is-Desktop-Viewer": [
92+
"true"
93+
],
94+
"CloudFront-Is-Mobile-Viewer": [
95+
"false"
96+
],
97+
"CloudFront-Is-SmartTV-Viewer": [
98+
"false"
99+
],
100+
"CloudFront-Is-Tablet-Viewer": [
101+
"false"
102+
],
103+
"CloudFront-Viewer-Country": [
104+
"US"
105+
],
106+
"Host": [
107+
"0123456789.execute-api.us-east-1.amazonaws.com"
108+
],
109+
"Upgrade-Insecure-Requests": [
110+
"1"
111+
],
112+
"User-Agent": [
113+
"Custom User Agent String"
114+
],
115+
"Via": [
116+
"1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)"
117+
],
118+
"X-Amz-Cf-Id": [
119+
"cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA=="
120+
],
121+
"X-Forwarded-For": [
122+
"127.0.0.1, 127.0.0.2"
123+
],
124+
"X-Forwarded-Port": [
125+
"443"
126+
],
127+
"X-Forwarded-Proto": [
128+
"https"
129+
]
130+
},
131+
"requestContext": {
132+
"accountId": "123456789012",
133+
"resourceId": "123456",
134+
"stage": "prod",
135+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
136+
"requestTime": "09/Apr/2015:12:34:56 +0000",
137+
"requestTimeEpoch": 1428582896000,
138+
"identity": {
139+
"cognitoIdentityPoolId": null,
140+
"accountId": null,
141+
"cognitoIdentityId": null,
142+
"caller": null,
143+
"accessKey": null,
144+
"sourceIp": "127.0.0.1",
145+
"cognitoAuthenticationType": null,
146+
"cognitoAuthenticationProvider": null,
147+
"userArn": null,
148+
"userAgent": "Custom User Agent String",
149+
"user": null
150+
},
151+
"path": "/prod/path/to/resource",
152+
"resourcePath": "/{proxy+}",
153+
"httpMethod": "POST",
154+
"apiId": "1234567890",
155+
"protocol": "HTTP/1.1"
156+
}
157+
}
158+
"""
159+
160+
}

0 commit comments

Comments
 (0)