Skip to content

Commit 6bbd287

Browse files
committed
add an encodable version of the APIGatewayResponse
1 parent 20fa5fe commit 6bbd287

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#if canImport(FoundationEssentials)
16+
import class FoundationEssentials.JSONEncoder
17+
import class FoundationEssentials.Data
18+
#else
19+
import class Foundation.JSONEncoder
20+
import struct Foundation.Data
21+
#endif
22+
23+
import HTTPTypes
24+
25+
public enum APIGatewayResponseError: Error {
26+
case failedToEncodeBody(Error)
27+
}
28+
29+
extension APIGatewayV2Response {
30+
31+
public init<Input: Encodable> (
32+
statusCode: HTTPResponse.Status,
33+
headers: HTTPHeaders? = nil,
34+
body: Input,
35+
isBase64Encoded: Bool? = nil,
36+
cookies: [String]? = nil
37+
) throws {
38+
let encodedBody: Data
39+
do {
40+
encodedBody = try JSONEncoder().encode(body)
41+
} catch {
42+
throw APIGatewayResponseError.failedToEncodeBody(error)
43+
}
44+
self.statusCode = statusCode
45+
self.headers = headers
46+
self.body = String(data: encodedBody, encoding: .utf8) ?? ""
47+
self.isBase64Encoded = isBase64Encoded
48+
self.cookies = cookies
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 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 Testing
16+
17+
import Foundation
18+
19+
@testable import AWSLambdaEvents
20+
21+
struct APIGatewayV2EncodableResponseTests {
22+
23+
24+
// MARK: Encoding
25+
struct BusinessResponse: Codable, Equatable {
26+
let message: String
27+
let code: Int
28+
}
29+
30+
@Test
31+
func testResponseEncoding() throws {
32+
33+
// given
34+
let businessResponse = BusinessResponse(message: "Hello World", code: 200)
35+
36+
var response: APIGatewayV2Response? = nil
37+
#expect(throws: Never.self) {
38+
try response = APIGatewayV2Response(statusCode: .ok, body: businessResponse)
39+
}
40+
try #require(response?.body != nil)
41+
42+
// when
43+
let body = response?.body?.data(using: .utf8)
44+
try #require(body != nil)
45+
46+
#expect(throws: Never.self) {
47+
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!)
48+
49+
// then
50+
#expect(encodedBody == businessResponse)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)