Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 5bd0c69

Browse files
authored
Created an extra package for the lambda events. (#22)
- LambdaEvents can be imported by other runtimes for handling Events.
1 parent 1313eca commit 5bd0c69

16 files changed

+110
-102
lines changed

Package.swift

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ let package = Package(
1010
name: "LambdaRuntime",
1111
targets: ["LambdaRuntime"]
1212
),
13-
.library(
14-
name: "LambdaRuntimeTestUtils",
15-
targets: ["LambdaRuntimeTestUtils"]
16-
),
13+
.library(
14+
name: "LambdaEvents",
15+
targets: ["LambdaEvents"]
16+
),
17+
.library(
18+
name: "LambdaRuntimeTestUtils",
19+
targets: ["LambdaRuntimeTestUtils"]
20+
),
1721
],
1822
dependencies: [
1923
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
@@ -22,25 +26,30 @@ let package = Package(
2226
.package(url: "https://github.com/fabianfett/swift-base64-kit.git", .upToNextMajor(from: "0.2.0")),
2327
],
2428
targets: [
25-
.target(
26-
name: "LambdaRuntime", dependencies: [
27-
"AsyncHTTPClient",
28-
"NIO",
29-
"NIOHTTP1",
30-
"NIOFoundationCompat",
31-
"Logging",
32-
"Base64Kit"
29+
.target(name: "LambdaEvents", dependencies: [
30+
"NIO",
31+
"NIOHTTP1",
32+
"NIOFoundationCompat",
33+
"Base64Kit"
34+
]),
35+
.target(name: "LambdaRuntime", dependencies: [
36+
"LambdaEvents",
37+
"AsyncHTTPClient",
38+
"NIO",
39+
"NIOHTTP1",
40+
"NIOFoundationCompat",
41+
"Logging"
42+
]),
43+
.target(name: "LambdaRuntimeTestUtils", dependencies: [
44+
"NIOHTTP1",
45+
"LambdaRuntime"
3346
]),
34-
.target(
35-
name: "LambdaRuntimeTestUtils",
36-
dependencies: ["NIOHTTP1", "LambdaRuntime"]
37-
),
3847
.testTarget(name: "LambdaRuntimeTests", dependencies: [
39-
"LambdaRuntime",
40-
"LambdaRuntimeTestUtils",
41-
"Base64Kit",
42-
"NIOTestUtils",
43-
"Logging",
48+
"LambdaEvents",
49+
"LambdaRuntime",
50+
"LambdaRuntimeTestUtils",
51+
"NIOTestUtils",
52+
"Logging",
4453
])
4554
]
4655
)

Sources/LambdaRuntime/Events/ALB.swift renamed to Sources/LambdaEvents/ALB.swift

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import Foundation
22
import NIO
33
import NIOHTTP1
44

5-
65
// https://github.com/aws/aws-lambda-go/blob/master/events/alb.go
7-
86
public struct ALB {
97

108
/// ALBTargetGroupRequest contains data originating from the ALB Lambda target group integration
@@ -53,42 +51,6 @@ public struct ALB {
5351
}
5452
}
5553

56-
// MARK: - Handler -
57-
58-
extension ALB {
59-
60-
public static func handler(
61-
multiValueHeadersEnabled: Bool = false,
62-
_ handler: @escaping (ALB.TargetGroupRequest, Context) -> EventLoopFuture<ALB.TargetGroupResponse>)
63-
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
64-
{
65-
// reuse as much as possible
66-
let encoder = JSONEncoder()
67-
let decoder = JSONDecoder()
68-
encoder.userInfo[ALB.TargetGroupResponse.MultiValueHeadersEnabledKey] = multiValueHeadersEnabled
69-
70-
return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in
71-
72-
let req: ALB.TargetGroupRequest
73-
do {
74-
req = try decoder.decode(ALB.TargetGroupRequest.self, from: inputBytes)
75-
}
76-
catch {
77-
return ctx.eventLoop.makeFailedFuture(error)
78-
}
79-
80-
return handler(req, ctx)
81-
.flatMapErrorThrowing() { (error) -> ALB.TargetGroupResponse in
82-
ctx.logger.error("Unhandled error. Responding with HTTP 500: \(error).")
83-
return ALB.TargetGroupResponse(statusCode: .internalServerError)
84-
}
85-
.flatMapThrowing { (result: ALB.TargetGroupResponse) -> NIO.ByteBuffer in
86-
return try encoder.encodeAsByteBuffer(result, allocator: ByteBufferAllocator())
87-
}
88-
}
89-
}
90-
}
91-
9254
// MARK: - Request -
9355

9456
extension ALB.TargetGroupRequest: Decodable {
@@ -154,7 +116,7 @@ extension ALB.TargetGroupRequest: Decodable {
154116

155117
extension ALB.TargetGroupResponse: Encodable {
156118

157-
internal static let MultiValueHeadersEnabledKey =
119+
public static let MultiValueHeadersEnabledKey =
158120
CodingUserInfoKey(rawValue: "ALB.TargetGroupResponse.MultiValueHeadersEnabledKey")!
159121

160122
enum CodingKeys: String, CodingKey {

Sources/LambdaRuntime/Events/APIGateway.swift renamed to Sources/LambdaEvents/APIGateway.swift

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
2-
import NIOFoundationCompat
32
import NIO
43
import NIOHTTP1
4+
import NIOFoundationCompat
55
import Base64Kit
66

77
// https://github.com/aws/aws-lambda-go/blob/master/events/apigw.go
@@ -77,40 +77,6 @@ public struct APIGateway {
7777
}
7878
}
7979

80-
// MARK: - Handler -
81-
82-
extension APIGateway {
83-
84-
public static func handler(
85-
_ handler: @escaping (APIGateway.Request, Context) -> EventLoopFuture<APIGateway.Response>)
86-
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
87-
{
88-
// reuse as much as possible
89-
let encoder = JSONEncoder()
90-
let decoder = JSONDecoder()
91-
92-
return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in
93-
94-
let req: APIGateway.Request
95-
do {
96-
req = try decoder.decode(APIGateway.Request.self, from: inputBytes)
97-
}
98-
catch {
99-
return ctx.eventLoop.makeFailedFuture(error)
100-
}
101-
102-
return handler(req, ctx)
103-
.flatMapErrorThrowing() { (error) -> APIGateway.Response in
104-
ctx.logger.error("Unhandled error. Responding with HTTP 500: \(error).")
105-
return APIGateway.Response(statusCode: .internalServerError)
106-
}
107-
.flatMapThrowing { (result: Response) -> NIO.ByteBuffer in
108-
return try encoder.encodeAsByteBuffer(result, allocator: ByteBufferAllocator())
109-
}
110-
}
111-
}
112-
}
113-
11480
// MARK: - Request -
11581

11682
extension APIGateway.Request: Decodable {
File renamed without changes.
File renamed without changes.
File renamed without changes.

Sources/LambdaRuntime/Events/DynamoDB.swift renamed to Sources/LambdaEvents/DynamoDB.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import NIO
55
public struct DynamoDB {
66

77
public struct Event: Decodable {
8-
let records: [EventRecord]
8+
public let records: [EventRecord]
99

1010
public enum CodingKeys: String, CodingKey {
1111
case records = "Records"
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)