-
Notifications
You must be signed in to change notification settings - Fork 29
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 (API Gateway) WebSockets Support to Swift for AWS Lambda Events #38
base: main
Are you sure you want to change the base?
Add (API Gateway) WebSockets Support to Swift for AWS Lambda Events #38
Conversation
I think this is a fine approach. @dave-moser @sebsto opinions? |
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) YEARS Apple Inc. and the SwiftAWSLambdaRuntime project authors |
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.
// Copyright (c) YEARS Apple Inc. and the SwiftAWSLambdaRuntime project authors | |
// Copyright (c) 2023 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
|
||
/// `APIGatewayWebSocketRequest` is a variation of the`APIGatewayV2Request` | ||
/// and contains data coming from the WebSockets API Gateway. | ||
public struct APIGatewayWebSocketRequest: Codable { |
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.
can any of this vibe shared with APIGatewayRequest
or not worth it?
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.
Oh sure ... much of it can be. I just wasn't sure what the correct approach should be. I kind of aimed for a "what's the minimum to make it work" approach. :)
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.
probably some shared struct they can both include as the underlying implementation
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.
If it would be helpful, I would be happy to look at the APIGatewayV2Request
event, as well as my proposed APIGatewayWebSocketRequest
event and extract those items shared between them as a struct
both can use…and then update this pull request.
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.
If it would be helpful, I would be happy to look at the APIGatewayV2Request
event, as well as my proposed APIGatewayWebSocketRequest
event and extract those items shared between them as a struct
both can use…and then update this pull request.
@tomerd @richwolf apologies for my late feedback as I am just back from PTO. I agree with @richwolf approach to not modify My approach would be to factor out all common elements between (and the same for the Thank you Rich for proposing this PR. |
No worries! I hope all is good on your end.
I don't want to step on any coding toes ... would you all like me to work on that? I am happy to defer to others if that would be best ... or proceed along those lines if that would free up everyone's time. |
I'm planning on handling some websocket events soonish this year, would love to see this progressed and happy to help if I can? I'll need to make something work regardless of this PR, but obviously it's better to have something that works for all users of this library. |
My apologies @jsonfry…I think this is on me. I've been meaning to return to this at some point but never got to it. I think what the group wanted was to see if it's possible to have a protocol that factored out code common to all flavors of the API Gateway v2 request. I started that work, just never finished it. Lemme see if I can fix it up in the next couple of days and update this PR. |
Thank you so much! |
@tomerd, @sebsto…in catching back up with this, I notice that |
Yes :D consider splitting to multiple PRs to make it easier to review and make progress |
Oh wow, that's really sage advice! Note to self: always try to make the reviewers' lives simpler. :) I'll follow up with a separate PR for |
} | ||
|
||
public let headers: HTTPHeaders? | ||
public let multiValueHeaders: HTTPMultiValueHeaders? |
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.
I also see
public let queryStringParameters: [String: String]?
public let multiValueQueryStringParameters: [String: [String]]?
come through on a CONNECT event type
Add APIGateway WebSockets Event Type
Motivation:
What I propose is adding WebSockets support to AWS Lambda Events.
Let me begin by stating outright that I am not sure this is the correct approach to take to bring WebSockets to AWS Lambda Events. Therefore, if this pull request is outright rejected, it won't hurt my feelings in the slightest.
API Gateway supports not only RESTful APIs, but also WebSockets. The way that it works is that API Gateway manages WebSockets sessions with clients. Whenever a client sends API Gateway some WebSockets data, API Gateway bundles it up in as an APIGatewayV2 request (at least, according to Amazon) and passes it along to a designated target…usually a Lambda function. This is what a bundled request looks like:
The problem, of course, is that the current
APIGatewayV2Request
type cannot decode that JSON because it is is missing a number of non-optional data values thatAPIGatewayV2Request
expects to exist (e.g.,version
,rawPath
, etc.).There are (at least as far as I can tell) two solutions to make this work. The first is simply to alter the current
APIGatewayV2Request
so that a number of its data values become optionals. I resisted suggesting this because I suspected it could easily break production code (forcing developers toif-let
things). I thought a better solution might simply be to create a new request/response type pair that could accommodate WebSockets APIs.Modifications:
I suggest adding a new event source file to AWS Lambda Events:
APIGateway+WebSockets.swift
containing two new types:APIGatewayWebSocketRequest
andAPIGatewayWebSocketResponse
.APIGatewayWebSocketResponse
would simply be a type alias (since responses require that no changes be made to that type);APIGatewayWebSocketRequest
would be capable of decoding the JSON listed above.A typical Lambda handler supporting WebSockets would look like this:
Note that responses to WebSockets clients (including, potentially, errors) are made through Amazon's
ApiGatewayManagementApi
. However, API Gateway itself always expects some kind of response…this can be a simple as always sending a 200 "OK" back to API Gateway.Result:
The Swift for AWS Lambda Runtime would be able to support API Gateway WebSockets applications.