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

Created an extra package for the lambda events. #22

Merged
merged 1 commit into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ let package = Package(
name: "LambdaRuntime",
targets: ["LambdaRuntime"]
),
.library(
name: "LambdaRuntimeTestUtils",
targets: ["LambdaRuntimeTestUtils"]
),
.library(
name: "LambdaEvents",
targets: ["LambdaEvents"]
),
.library(
name: "LambdaRuntimeTestUtils",
targets: ["LambdaRuntimeTestUtils"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
Expand All @@ -22,25 +26,30 @@ let package = Package(
.package(url: "https://github.com/fabianfett/swift-base64-kit.git", .upToNextMajor(from: "0.2.0")),
],
targets: [
.target(
name: "LambdaRuntime", dependencies: [
"AsyncHTTPClient",
"NIO",
"NIOHTTP1",
"NIOFoundationCompat",
"Logging",
"Base64Kit"
.target(name: "LambdaEvents", dependencies: [
"NIO",
"NIOHTTP1",
"NIOFoundationCompat",
"Base64Kit"
]),
.target(name: "LambdaRuntime", dependencies: [
"LambdaEvents",
"AsyncHTTPClient",
"NIO",
"NIOHTTP1",
"NIOFoundationCompat",
"Logging"
]),
.target(name: "LambdaRuntimeTestUtils", dependencies: [
"NIOHTTP1",
"LambdaRuntime"
]),
.target(
name: "LambdaRuntimeTestUtils",
dependencies: ["NIOHTTP1", "LambdaRuntime"]
),
.testTarget(name: "LambdaRuntimeTests", dependencies: [
"LambdaRuntime",
"LambdaRuntimeTestUtils",
"Base64Kit",
"NIOTestUtils",
"Logging",
"LambdaEvents",
"LambdaRuntime",
"LambdaRuntimeTestUtils",
"NIOTestUtils",
"Logging",
])
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import Foundation
import NIO
import NIOHTTP1


// https://github.com/aws/aws-lambda-go/blob/master/events/alb.go

public struct ALB {

/// ALBTargetGroupRequest contains data originating from the ALB Lambda target group integration
Expand Down Expand Up @@ -53,42 +51,6 @@ public struct ALB {
}
}

// MARK: - Handler -

extension ALB {

public static func handler(
multiValueHeadersEnabled: Bool = false,
_ handler: @escaping (ALB.TargetGroupRequest, Context) -> EventLoopFuture<ALB.TargetGroupResponse>)
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
{
// reuse as much as possible
let encoder = JSONEncoder()
let decoder = JSONDecoder()
encoder.userInfo[ALB.TargetGroupResponse.MultiValueHeadersEnabledKey] = multiValueHeadersEnabled

return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in

let req: ALB.TargetGroupRequest
do {
req = try decoder.decode(ALB.TargetGroupRequest.self, from: inputBytes)
}
catch {
return ctx.eventLoop.makeFailedFuture(error)
}

return handler(req, ctx)
.flatMapErrorThrowing() { (error) -> ALB.TargetGroupResponse in
ctx.logger.error("Unhandled error. Responding with HTTP 500: \(error).")
return ALB.TargetGroupResponse(statusCode: .internalServerError)
}
.flatMapThrowing { (result: ALB.TargetGroupResponse) -> NIO.ByteBuffer in
return try encoder.encodeAsByteBuffer(result, allocator: ByteBufferAllocator())
}
}
}
}

// MARK: - Request -

extension ALB.TargetGroupRequest: Decodable {
Expand Down Expand Up @@ -154,7 +116,7 @@ extension ALB.TargetGroupRequest: Decodable {

extension ALB.TargetGroupResponse: Encodable {

internal static let MultiValueHeadersEnabledKey =
public static let MultiValueHeadersEnabledKey =
CodingUserInfoKey(rawValue: "ALB.TargetGroupResponse.MultiValueHeadersEnabledKey")!

enum CodingKeys: String, CodingKey {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import NIOFoundationCompat
import NIO
import NIOHTTP1
import NIOFoundationCompat
import Base64Kit

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

// MARK: - Handler -

extension APIGateway {

public static func handler(
_ handler: @escaping (APIGateway.Request, Context) -> EventLoopFuture<APIGateway.Response>)
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
{
// reuse as much as possible
let encoder = JSONEncoder()
let decoder = JSONDecoder()

return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in

let req: APIGateway.Request
do {
req = try decoder.decode(APIGateway.Request.self, from: inputBytes)
}
catch {
return ctx.eventLoop.makeFailedFuture(error)
}

return handler(req, ctx)
.flatMapErrorThrowing() { (error) -> APIGateway.Response in
ctx.logger.error("Unhandled error. Responding with HTTP 500: \(error).")
return APIGateway.Response(statusCode: .internalServerError)
}
.flatMapThrowing { (result: Response) -> NIO.ByteBuffer in
return try encoder.encodeAsByteBuffer(result, allocator: ByteBufferAllocator())
}
}
}
}

// MARK: - Request -

extension APIGateway.Request: Decodable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import NIO
public struct DynamoDB {

public struct Event: Decodable {
let records: [EventRecord]
public let records: [EventRecord]

public enum CodingKeys: String, CodingKey {
case records = "Records"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import Foundation
import NIO
import NIOFoundationCompat

protocol DecodableBody {
public protocol DecodableBody {

var body: String? { get }
var isBase64Encoded: Bool { get }

}

extension DecodableBody {
public extension DecodableBody {

var isBase64Encoded: Bool {
return false
}

}

extension DecodableBody {
public extension DecodableBody {

func decodeBody<T: Decodable>(_ type: T.Type, decoder: JSONDecoder = JSONDecoder()) throws -> T {

Expand Down
37 changes: 37 additions & 0 deletions Sources/LambdaRuntime/Runtime+ALB.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import NIO
@_exported import LambdaEvents

extension ALB {

public static func handler(
multiValueHeadersEnabled: Bool = false,
_ handler: @escaping (ALB.TargetGroupRequest, Context) -> EventLoopFuture<ALB.TargetGroupResponse>)
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
{
// reuse as much as possible
let encoder = JSONEncoder()
let decoder = JSONDecoder()
encoder.userInfo[ALB.TargetGroupResponse.MultiValueHeadersEnabledKey] = multiValueHeadersEnabled

return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in

let req: ALB.TargetGroupRequest
do {
req = try decoder.decode(ALB.TargetGroupRequest.self, from: inputBytes)
}
catch {
return ctx.eventLoop.makeFailedFuture(error)
}

return handler(req, ctx)
.flatMapErrorThrowing() { (error) -> ALB.TargetGroupResponse in
ctx.logger.error("Unhandled error. Responding with HTTP 500: \(error).")
return ALB.TargetGroupResponse(statusCode: .internalServerError)
}
.flatMapThrowing { (result: ALB.TargetGroupResponse) -> NIO.ByteBuffer in
return try encoder.encodeAsByteBuffer(result, allocator: ByteBufferAllocator())
}
}
}
}
35 changes: 35 additions & 0 deletions Sources/LambdaRuntime/Runtime+APIGateway.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation
import NIO
@_exported import LambdaEvents

extension APIGateway {

public static func handler(
_ handler: @escaping (APIGateway.Request, Context) -> EventLoopFuture<APIGateway.Response>)
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
{
// reuse as much as possible
let encoder = JSONEncoder()
let decoder = JSONDecoder()

return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in

let req: APIGateway.Request
do {
req = try decoder.decode(APIGateway.Request.self, from: inputBytes)
}
catch {
return ctx.eventLoop.makeFailedFuture(error)
}

return handler(req, ctx)
.flatMapErrorThrowing() { (error) -> APIGateway.Response in
ctx.logger.error("Unhandled error. Responding with HTTP 500: \(error).")
return APIGateway.Response(statusCode: .internalServerError)
}
.flatMapThrowing { (result: Response) -> NIO.ByteBuffer in
return try encoder.encodeAsByteBuffer(result, allocator: ByteBufferAllocator())
}
}
}
}
2 changes: 1 addition & 1 deletion Tests/LambdaRuntimeTests/Events/APIGatewayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class APIGatewayTests: XCTestCase {
eventLoop: eventLoopGroup.next())

let payload = APIGatewayTests.exampleGetPayload
let length = payload.lengthOfBytes(using: .utf8)
let length = payload.utf8.count
var testPayload = ByteBufferAllocator().buffer(capacity: length)
testPayload.setString(payload, at: 0)
testPayload.moveWriterIndex(forwardBy: length)
Expand Down
1 change: 1 addition & 0 deletions Tests/LambdaRuntimeTests/Events/S3Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import XCTest
import NIO
@testable import LambdaRuntime
@testable import LambdaEvents

class S3Tests: XCTestCase {

Expand Down