-
Notifications
You must be signed in to change notification settings - Fork 32
Add an encodable version of the APIGatewayResponse #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6bbd287
add an encodable version of the APIGatewayResponse
sebsto 5781034
apply swift format
sebsto 25ea446
fix build on Linux
sebsto 6a3aa0f
simplify code and add the same initializer on APIGatewayResponse
sebsto 7424005
change the order of the args to avoid breaking the existing API
sebsto feb352b
change name of arguments to avoid overriding other init() methods
sebsto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import HTTPTypes | ||
|
||
#if canImport(FoundationEssentials) | ||
import class FoundationEssentials.JSONEncoder | ||
import struct FoundationEssentials.Data | ||
#else | ||
import class Foundation.JSONEncoder | ||
import struct Foundation.Data | ||
#endif | ||
|
||
extension Encodable { | ||
fileprivate func string() throws -> String { | ||
let encoded = try JSONEncoder().encode(self) | ||
return String(decoding: encoded, as: UTF8.self) | ||
} | ||
} | ||
|
||
extension APIGatewayResponse { | ||
|
||
public init<Input: Encodable>( | ||
statusCode: HTTPResponse.Status, | ||
headers: HTTPHeaders? = nil, | ||
multiValueHeaders: HTTPMultiValueHeaders? = nil, | ||
body: Input | ||
) throws { | ||
self.init( | ||
statusCode: statusCode, | ||
headers: headers, | ||
multiValueHeaders: multiValueHeaders, | ||
body: try body.string(), | ||
isBase64Encoded: nil | ||
) | ||
} | ||
} | ||
|
||
extension APIGatewayV2Response { | ||
|
||
public init<Input: Encodable>( | ||
statusCode: HTTPResponse.Status, | ||
headers: HTTPHeaders? = nil, | ||
body: Input, | ||
cookies: [String]? = nil | ||
) throws { | ||
self.init( | ||
statusCode: statusCode, | ||
headers: headers, | ||
body: try body.string(), | ||
isBase64Encoded: nil, | ||
cookies: cookies | ||
) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
Tests/AWSLambdaEventsTests/APIGateway+EncodableTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import Testing | ||
|
||
@testable import AWSLambdaEvents | ||
|
||
struct APIGatewayEncodableResponseTests { | ||
|
||
// MARK: Encoding | ||
struct BusinessResponse: Codable, Equatable { | ||
let message: String | ||
let code: Int | ||
} | ||
|
||
@Test | ||
func testResponseEncodingV2() throws { | ||
|
||
// given | ||
let businessResponse = BusinessResponse(message: "Hello World", code: 200) | ||
|
||
var response: APIGatewayV2Response? = nil | ||
#expect(throws: Never.self) { | ||
try response = APIGatewayV2Response(statusCode: .ok, body: businessResponse) | ||
} | ||
try #require(response?.body != nil) | ||
|
||
// when | ||
let body = response?.body?.data(using: .utf8) | ||
try #require(body != nil) | ||
|
||
#expect(throws: Never.self) { | ||
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!) | ||
|
||
// then | ||
#expect(encodedBody == businessResponse) | ||
} | ||
} | ||
|
||
@Test | ||
func testResponseEncoding() throws { | ||
|
||
// given | ||
let businessResponse = BusinessResponse(message: "Hello World", code: 200) | ||
|
||
var response: APIGatewayResponse? = nil | ||
#expect(throws: Never.self) { | ||
try response = APIGatewayResponse(statusCode: .ok, body: businessResponse) | ||
} | ||
try #require(response?.body != nil) | ||
|
||
// when | ||
let body = response?.body?.data(using: .utf8) | ||
try #require(body != nil) | ||
|
||
#expect(throws: Never.self) { | ||
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!) | ||
|
||
// then | ||
#expect(encodedBody == businessResponse) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we broken/confused the API before? Since
String
automatically conforms toEncodable
does the new API take precedence over the existingString
one meaning it always throws when it doesn't need to? (And always add an extra hop through the encoder that isn't needed, or potentially double encode a JSON body)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah would be good to ensure we have a different signature for the Codable version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
What type of difference would be acceptable ?
What about changing the order of the parameters. We sacrifice consistency for compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'd keep the ordering and make the
body
parameter different to be explicitThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. I kept the original order and renamed
body
toencodableBody