-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from Chingles2404/APIGatewayV2WebSocketEvent
Add `ApiGatewayV2WebSocketEvent`
- Loading branch information
Showing
3 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
lambda/shared/src/main/scala/feral/lambda/events/ApiGatewayV2WebSocketEvent.scala
This file contains 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,151 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package feral.lambda.events | ||
|
||
import com.comcast.ip4s.Hostname | ||
import io.circe.Decoder | ||
|
||
import java.time.Instant | ||
|
||
import codecs.decodeInstant | ||
import codecs.decodeHostname | ||
|
||
sealed abstract class ApiGatewayV2WebSocketEvent { | ||
def stageVariables: Option[Map[String, String]] | ||
def requestContext: WebSocketRequestContext | ||
def body: Option[String] | ||
def isBase64Encoded: Boolean | ||
} | ||
|
||
object ApiGatewayV2WebSocketEvent { | ||
def apply( | ||
stageVariables: Option[Map[String, String]], | ||
requestContext: WebSocketRequestContext, | ||
body: Option[String], | ||
isBase64Encoded: Boolean | ||
): ApiGatewayV2WebSocketEvent = | ||
new Impl( | ||
stageVariables, | ||
requestContext, | ||
body, | ||
isBase64Encoded | ||
) | ||
|
||
implicit val decoder: Decoder[ApiGatewayV2WebSocketEvent] = Decoder.forProduct4( | ||
"stageVariables", | ||
"requestContext", | ||
"body", | ||
"isBase64Encoded" | ||
)(ApiGatewayV2WebSocketEvent.apply) | ||
|
||
private final case class Impl( | ||
stageVariables: Option[Map[String, String]], | ||
requestContext: WebSocketRequestContext, | ||
body: Option[String], | ||
isBase64Encoded: Boolean | ||
) extends ApiGatewayV2WebSocketEvent { | ||
override def productPrefix = "ApiGatewayV2WebSocketEvent" | ||
} | ||
} | ||
|
||
sealed abstract class WebSocketEventType | ||
|
||
object WebSocketEventType { | ||
case object Connect extends WebSocketEventType | ||
case object Message extends WebSocketEventType | ||
case object Disconnect extends WebSocketEventType | ||
|
||
private[events] implicit val decoder: Decoder[WebSocketEventType] = | ||
Decoder.decodeString.map { | ||
case "CONNECT" => Connect | ||
case "MESSAGE" => Message | ||
case "DISCONNECT" => Disconnect | ||
} | ||
} | ||
|
||
sealed abstract class WebSocketRequestContext { | ||
def stage: String | ||
def requestId: String | ||
def apiId: String | ||
def connectedAt: Instant | ||
def connectionId: String | ||
def domainName: Hostname | ||
def eventType: WebSocketEventType | ||
def extendedRequestId: String | ||
def messageId: Option[String] | ||
def requestTime: Instant | ||
def routeKey: String | ||
} | ||
|
||
object WebSocketRequestContext { | ||
def apply( | ||
stage: String, | ||
requestId: String, | ||
apiId: String, | ||
connectedAt: Instant, | ||
connectionId: String, | ||
domainName: Hostname, | ||
eventType: WebSocketEventType, | ||
extendedRequestId: String, | ||
messageId: Option[String], | ||
requestTime: Instant, | ||
routeKey: String | ||
): WebSocketRequestContext = | ||
new Impl( | ||
stage, | ||
requestId, | ||
apiId, | ||
connectedAt, | ||
connectionId, | ||
domainName, | ||
eventType, | ||
extendedRequestId, | ||
messageId, | ||
requestTime, | ||
routeKey | ||
) | ||
|
||
private[events] implicit val decoder: Decoder[WebSocketRequestContext] = Decoder.forProduct11( | ||
"stage", | ||
"requestId", | ||
"apiId", | ||
"connectedAt", | ||
"connectionId", | ||
"domainName", | ||
"eventType", | ||
"extendedRequestId", | ||
"messageId", | ||
"requestTimeEpoch", | ||
"routeKey" | ||
)(WebSocketRequestContext.apply) | ||
|
||
private final case class Impl( | ||
stage: String, | ||
requestId: String, | ||
apiId: String, | ||
connectedAt: Instant, | ||
connectionId: String, | ||
domainName: Hostname, | ||
eventType: WebSocketEventType, | ||
extendedRequestId: String, | ||
messageId: Option[String], | ||
requestTime: Instant, | ||
routeKey: String | ||
) extends WebSocketRequestContext { | ||
override def productPrefix = "WebSocketRequestContext" | ||
} | ||
} |
This file contains 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
140 changes: 140 additions & 0 deletions
140
lambda/shared/src/test/scala/feral/lambda/events/ApiGatewayV2WebSocketEventSuite.scala
This file contains 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,140 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package feral.lambda.events | ||
|
||
import io.circe.literal._ | ||
import munit.FunSuite | ||
|
||
class ApiGatewayV2WebSocketEventSuite extends FunSuite { | ||
|
||
import ApiGatewayV2WebSocketEventSuite._ | ||
|
||
test("decode connect") { | ||
connectEvent.as[ApiGatewayV2WebSocketEvent].toTry.get | ||
} | ||
|
||
test("decode disconnect") { | ||
disconnectEvent.as[ApiGatewayV2WebSocketEvent].toTry.get | ||
} | ||
|
||
} | ||
|
||
object ApiGatewayV2WebSocketEventSuite { | ||
|
||
def connectEvent = json""" | ||
{ | ||
"headers": { | ||
"Host": "abcd123.execute-api.us-east-1.amazonaws.com", | ||
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits", | ||
"Sec-WebSocket-Key": "...", | ||
"Sec-WebSocket-Version": "13", | ||
"X-Amzn-Trace-Id": "...", | ||
"X-Forwarded-For": "192.0.2.1", | ||
"X-Forwarded-Port": "443", | ||
"X-Forwarded-Proto": "https" | ||
}, | ||
"multiValueHeaders": { | ||
"Host": [ | ||
"abcd123.execute-api.us-east-1.amazonaws.com" | ||
], | ||
"Sec-WebSocket-Extensions": [ | ||
"permessage-deflate; client_max_window_bits" | ||
], | ||
"Sec-WebSocket-Key": [ | ||
"..." | ||
], | ||
"Sec-WebSocket-Version": [ | ||
"13" | ||
], | ||
"X-Amzn-Trace-Id": [ | ||
"..." | ||
], | ||
"X-Forwarded-For": [ | ||
"192.0.2.1" | ||
], | ||
"X-Forwarded-Port": [ | ||
"443" | ||
], | ||
"X-Forwarded-Proto": [ | ||
"https" | ||
] | ||
}, | ||
"requestContext": { | ||
"routeKey": "$$connect", | ||
"eventType": "CONNECT", | ||
"extendedRequestId": "ABCD1234=", | ||
"requestTime": "09/Feb/2024:18:11:43 +0000", | ||
"messageDirection": "IN", | ||
"stage": "prod", | ||
"connectedAt": 1707502303419, | ||
"requestTimeEpoch": 1707502303420, | ||
"identity": { | ||
"sourceIp": "192.0.2.1" | ||
}, | ||
"requestId": "ABCD1234=", | ||
"domainName": "abcd1234.execute-api.us-east-1.amazonaws.com", | ||
"connectionId": "AAAA1234=", | ||
"apiId": "abcd1234" | ||
}, | ||
"isBase64Encoded": false | ||
} | ||
""" | ||
def disconnectEvent = json""" | ||
{ | ||
"headers": { | ||
"Host": "abcd1234.execute-api.us-east-1.amazonaws.com", | ||
"x-api-key": "", | ||
"X-Forwarded-For": "", | ||
"x-restapi": "" | ||
}, | ||
"multiValueHeaders": { | ||
"Host": [ | ||
"abcd1234.execute-api.us-east-1.amazonaws.com" | ||
], | ||
"x-api-key": [ | ||
"" | ||
], | ||
"X-Forwarded-For": [ | ||
"" | ||
], | ||
"x-restapi": [ | ||
"" | ||
] | ||
}, | ||
"requestContext": { | ||
"routeKey": "$$disconnect", | ||
"disconnectStatusCode": 1005, | ||
"eventType": "DISCONNECT", | ||
"extendedRequestId": "ABCD1234=", | ||
"requestTime": "09/Feb/2024:18:23:28 +0000", | ||
"messageDirection": "IN", | ||
"disconnectReason": "Client-side close frame status not set", | ||
"stage": "prod", | ||
"connectedAt": 1707503007396, | ||
"requestTimeEpoch": 1707503008941, | ||
"identity": { | ||
"sourceIp": "192.0.2.1" | ||
}, | ||
"requestId": "ABCD1234=", | ||
"domainName": "abcd1234.execute-api.us-east-1.amazonaws.com", | ||
"connectionId": "AAAA1234=", | ||
"apiId": "abcd1234" | ||
}, | ||
"isBase64Encoded": false | ||
} | ||
""" | ||
} |