Skip to content

Commit e9b5e79

Browse files
committed
Address PR comments (and format)
1 parent 38254f2 commit e9b5e79

11 files changed

+905
-606
lines changed

Sources/AWSLambdaEvents/Codable Helpers/APIGatewayV2+Encode.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

Sources/AWSLambdaEvents/Codable Helpers/APIGatewayV2+Decode.swift renamed to Sources/AWSLambdaEvents/Codable Helpers/DecodableRequest.swift

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import HTTPTypes
16+
117
#if canImport(FoundationEssentials)
218
import FoundationEssentials
319
#else
420
import Foundation
521
#endif
622

7-
import HTTPTypes
23+
public protocol DecodableRequest {
24+
var body: String? { get }
25+
var isBase64Encoded: Bool { get }
26+
27+
func decodeBody() throws -> Data?
28+
func decodeBody<T>(
29+
_ type: T.Type,
30+
using decoder: JSONDecoder
31+
) throws -> T where T: Decodable
32+
}
833

9-
public extension APIGatewayV2Request {
34+
extension DecodableRequest {
1035
/// Decodes the body of the request into a `Data` object.
1136
///
1237
/// - Returns: The decoded body as `Data` or `nil` if the body is empty.
13-
func decodeBody() throws -> Data? {
38+
public func decodeBody() throws -> Data? {
1439
guard let body else { return nil }
1540

1641
if isBase64Encoded,
17-
let base64Decoded = Data(base64Encoded: body) {
42+
let base64Decoded = Data(base64Encoded: body)
43+
{
1844
return base64Decoded
1945
}
2046

@@ -30,7 +56,7 @@ public extension APIGatewayV2Request {
3056
///
3157
/// - Returns: The decoded body as `T`.
3258
/// - Throws: An error if the body cannot be decoded.
33-
func decodeBody<T>(
59+
public func decodeBody<T>(
3460
_ type: T.Type,
3561
using decoder: JSONDecoder = JSONDecoder()
3662
) throws -> T where T: Decodable {
@@ -39,10 +65,14 @@ public extension APIGatewayV2Request {
3965
var requestData = bodyData
4066

4167
if isBase64Encoded,
42-
let base64Decoded = Data(base64Encoded: requestData) {
68+
let base64Decoded = Data(base64Encoded: requestData)
69+
{
4370
requestData = base64Decoded
4471
}
4572

4673
return try decoder.decode(T.self, from: requestData)
4774
}
4875
}
76+
77+
extension APIGatewayV2Request: DecodableRequest {}
78+
extension FunctionURLRequest: DecodableRequest {}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import HTTPTypes
16+
17+
#if canImport(FoundationEssentials)
18+
import FoundationEssentials
19+
#else
20+
import Foundation
21+
#endif
22+
23+
public protocol EncodableResponse {
24+
static func encoding<T>(
25+
_ encodable: T,
26+
status: HTTPResponse.Status,
27+
using encoder: JSONEncoder,
28+
headers: HTTPHeaders?,
29+
cookies: [String]?,
30+
onError: ((Error) -> Self)
31+
) -> Self where T: Encodable
32+
33+
init(
34+
statusCode: HTTPResponse.Status,
35+
headers: HTTPHeaders?,
36+
body: String?,
37+
cookies: [String]?,
38+
isBase64Encoded: Bool?
39+
)
40+
}
41+
42+
extension EncodableResponse {
43+
/// Encodes a given encodable object into a response object.
44+
///
45+
/// - Parameters:
46+
/// - encodable: The object to encode.
47+
/// - status: The status code to use. Defaults to `ok`.
48+
/// - encoder: The encoder to use. Defaults to a new `JSONEncoder`.
49+
/// - onError: A closure to handle errors, and transform them into a `APIGatewayV2Response`.
50+
/// Defaults to converting the error into a 500 (Internal Server Error) response with the error message as
51+
/// the body.
52+
///
53+
/// - Returns: a response object whose body is the encoded `encodable` type and with the
54+
/// other response parameters
55+
public static func encoding<T>(
56+
_ encodable: T,
57+
status: HTTPResponse.Status = .ok,
58+
using encoder: JSONEncoder = JSONEncoder(),
59+
headers: HTTPHeaders? = nil,
60+
cookies: [String]? = nil,
61+
onError: ((Error) -> Self) = Self.defaultErrorHandler
62+
) -> Self where T: Encodable {
63+
do {
64+
let encodedResponse = try encoder.encode(encodable)
65+
return Self(
66+
statusCode: status,
67+
headers: headers,
68+
body: String(data: encodedResponse, encoding: .utf8),
69+
cookies: cookies,
70+
isBase64Encoded: nil
71+
)
72+
} catch {
73+
return onError(error)
74+
}
75+
}
76+
77+
public static var defaultErrorHandler: ((Error) -> Self) {
78+
{ error in
79+
Self(
80+
statusCode: .internalServerError,
81+
headers: nil,
82+
body: "Internal Server Error: \(String(describing: error))",
83+
cookies: nil,
84+
isBase64Encoded: nil
85+
)
86+
}
87+
}
88+
}
89+
90+
extension APIGatewayV2Response: EncodableResponse {
91+
public init(
92+
statusCode: HTTPResponse.Status,
93+
headers: HTTPHeaders?,
94+
body: String?,
95+
cookies: [String]?,
96+
isBase64Encoded: Bool?
97+
) {
98+
self.init(
99+
statusCode: statusCode,
100+
headers: headers,
101+
body: body,
102+
isBase64Encoded: isBase64Encoded,
103+
cookies: cookies
104+
)
105+
}
106+
}
107+
extension FunctionURLResponse: EncodableResponse {}

Sources/AWSLambdaEvents/Codable Helpers/FunctionURL+Decode.swift

Lines changed: 0 additions & 48 deletions
This file was deleted.

Sources/AWSLambdaEvents/Codable Helpers/FunctionURL+Encode.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
115
#if canImport(FoundationEssentials)
216
import FoundationEssentials
317
#else
418
import Foundation
519
#endif
620

7-
public extension SQSEvent {
21+
extension SQSEvent {
822
/// Decodes the records included in the event into an array of decodable objects.
923
///
1024
/// - Parameters:
@@ -13,7 +27,7 @@ public extension SQSEvent {
1327
///
1428
/// - Returns: The decoded records as `[T]`.
1529
/// - Throws: An error if any of the records cannot be decoded.
16-
func decodeBody<T>(
30+
public func decodeBody<T>(
1731
_ type: T.Type,
1832
using decoder: JSONDecoder = JSONDecoder()
1933
) throws -> [T] where T: Decodable {
@@ -23,7 +37,7 @@ public extension SQSEvent {
2337
}
2438
}
2539

26-
public extension SQSEvent.Message {
40+
extension SQSEvent.Message {
2741
/// Decodes the body of the message into a decodable object.
2842
///
2943
/// - Parameters:
@@ -32,10 +46,10 @@ public extension SQSEvent.Message {
3246
///
3347
/// - Returns: The decoded body as `T`.
3448
/// - Throws: An error if the body cannot be decoded.
35-
func decodeBody<T>(
49+
public func decodeBody<T>(
3650
_ type: T.Type,
3751
using decoder: JSONDecoder = JSONDecoder()
3852
) throws -> T where T: Decodable {
3953
try decoder.decode(T.self, from: body.data(using: .utf8) ?? Data())
4054
}
41-
}
55+
}

0 commit comments

Comments
 (0)