-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add an encodable version of the APIGatewayResponse #86
Conversation
Thank you @adam-fowler |
func testResponseEncoding() { | ||
let resp = APIGatewayResponse( | ||
func testResponseEncoding() throws { | ||
let resp = try APIGatewayResponse( |
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 to Encodable
does the new API take precedence over the existing String
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.
extension APIGatewayV2Response {
public init<Input: Encodable>(
body: Input,
statusCode: HTTPResponse.Status,
headers: HTTPHeaders? = nil,
cookies: [String]? = nil
) throws {
self.init(
statusCode: statusCode,
headers: headers,
body: try body.string(),
isBase64Encoded: nil,
cookies: cookies
)
}
}
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 explicit
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.
done. I kept the original order and renamed body
to encodableBody
@0xTim @adam-fowler would this be acceptable ? I changed the order of the args. |
I'd prefer you changed the name of the body parameter. |
…initializer (#437) After the merge of swift-server/swift-aws-lambda-events#86 I updated the `APIGatewayV2Response` example to use the new initializer
Add a convenience initializer to the APIGatewayResponse v2 that accepts an Encodable object
Motivation:
Most Lambda developers will return a JSON object when exposing their function through the API Gateway. The current initializer only accepts a
String
body as per the AWS message definition.This obliges all developers to write two lines of code + error handling to encode their object and transform the
Data
to a string.Modifications:
Add a new initializer that accepts a
body
asEncodable
Result:
Developers can now pass a Swift struct to the APIGatewayResponse.