Skip to content

Commit d6c9992

Browse files
committed
feature: Create LambdaProxyEvent Type
1 parent 2a118fb commit d6c9992

File tree

5 files changed

+297
-72
lines changed

5 files changed

+297
-72
lines changed

Sources/AWSLambdaEvents/APIGateway+V2.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,68 +23,69 @@ public struct APIGatewayV2Request: Codable {
2323
public let sourceIp: String
2424
public let userAgent: String
2525
}
26-
26+
2727
/// Authorizer contains authorizer information for the request context.
2828
public struct Authorizer: Codable {
2929
/// JWT contains JWT authorizer information for the request context.
3030
public struct JWT: Codable {
3131
public let claims: [String: String]?
3232
public let scopes: [String]?
3333
}
34-
34+
3535
public let jwt: JWT
3636
}
37-
37+
3838
public let accountId: String
3939
public let apiId: String
4040
public let domainName: String
4141
public let domainPrefix: String
4242
public let stage: String
4343
public let requestId: String
44-
44+
4545
public let http: HTTP
4646
public let authorizer: Authorizer?
47-
47+
4848
/// The request time in format: 23/Apr/2020:11:08:18 +0000
4949
public let time: String
5050
public let timeEpoch: UInt64
5151
}
52-
52+
5353
public let version: String
5454
public let routeKey: String
5555
public let rawPath: String
5656
public let rawQueryString: String
57-
57+
5858
public let cookies: [String]?
5959
public let headers: HTTPHeaders
6060
public let queryStringParameters: [String: String]?
6161
public let pathParameters: [String: String]?
62-
62+
6363
public let context: Context
6464
public let stageVariables: [String: String]?
65-
65+
6666
public let body: String?
6767
public let isBase64Encoded: Bool
68-
68+
6969
enum CodingKeys: String, CodingKey {
7070
case version
7171
case routeKey
7272
case rawPath
7373
case rawQueryString
74-
74+
7575
case cookies
7676
case headers
7777
case queryStringParameters
7878
case pathParameters
79-
79+
8080
case context = "requestContext"
8181
case stageVariables
82-
82+
8383
case body
8484
case isBase64Encoded
8585
}
8686
}
8787

88+
8889
public struct APIGatewayV2Response: Codable {
8990
public var statusCode: HTTPResponseStatus
9091
public var headers: HTTPHeaders?
@@ -112,6 +113,5 @@ extension APIGatewayV2Request: Sendable {}
112113
extension APIGatewayV2Request.Context: Sendable {}
113114
extension APIGatewayV2Request.Context.HTTP: Sendable {}
114115
extension APIGatewayV2Request.Context.Authorizer: Sendable {}
115-
extension APIGatewayV2Request.Context.Authorizer.JWT: Sendable {}
116116
extension APIGatewayV2Response: Sendable {}
117117
#endif
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
public struct LambdaProxyEvent: Codable {
16+
public struct Context: Codable {
17+
public struct HTTP: Codable {
18+
public let httpMethod: HTTPMethod
19+
public let path: String
20+
public let `protocol`: String
21+
public let sourceIp: String
22+
public let userAgent: String
23+
}
24+
25+
/// Authorizer contains authorizer information for the request context.
26+
public struct Authorizer: Codable {
27+
public let claims: [String: String]?
28+
public let scopes: [String]?
29+
}
30+
31+
public let accountId: String
32+
public let apiId: String
33+
public let domainName: String
34+
public let domainPrefix: String
35+
public let stage: String
36+
public let requestId: String
37+
38+
public let httpMethod: HTTPMethod
39+
public let authorizer: Authorizer?
40+
41+
public let resourcePath: String?
42+
public let path: String?
43+
44+
/// The request time in format: 23/Apr/2020:11:08:18 +0000
45+
public let time: String?
46+
public let requestTime: String?
47+
public let requestTimeEpoch: UInt64
48+
}
49+
50+
public let resource: String
51+
public let path: String
52+
public let httpMethod: String
53+
public let stageVariables: [String: String]?
54+
55+
public let cookies: [String]?
56+
public let headers: HTTPHeaders
57+
public let queryStringParameters: [String: String]?
58+
public let pathParameters: [String: String]?
59+
60+
public let context: Context
61+
62+
public let body: String?
63+
public let isBase64Encoded: Bool
64+
65+
enum CodingKeys: String, CodingKey {
66+
case resource
67+
case path
68+
case httpMethod
69+
70+
case cookies
71+
case headers
72+
case queryStringParameters
73+
case pathParameters
74+
75+
case context = "requestContext"
76+
case stageVariables
77+
78+
case body
79+
case isBase64Encoded
80+
}
81+
}
82+
83+
#if swift(>=5.6)
84+
extension LambdaProxyEvent: Sendable {}
85+
extension LambdaProxyEvent.Context: Sendable {}
86+
extension LambdaProxyEvent.Context.HTTP: Sendable {}
87+
extension LambdaProxyEvent.Context.Authorizer: Sendable {}
88+
#endif

Tests/AWSLambdaEventsTests/APIGateway+V2Tests.swift

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,68 @@ import XCTest
1717

1818
class APIGatewayV2Tests: XCTestCase {
1919
static let exampleGetEventBody = """
20-
{
21-
"routeKey":"GET /hello",
22-
"version":"2.0",
23-
"rawPath":"/hello",
24-
"stageVariables":{
25-
"foo":"bar"
26-
},
27-
"requestContext":{
28-
"timeEpoch":1587750461466,
29-
"domainPrefix":"hello",
30-
"authorizer":{
31-
"jwt":{
32-
"scopes":[
33-
"hello"
34-
],
35-
"claims":{
36-
"aud":"customers",
37-
"iss":"https://hello.test.com/",
38-
"iat":"1587749276",
39-
"exp":"1587756476"
40-
}
41-
}
42-
},
43-
"accountId":"0123456789",
44-
"stage":"$default",
45-
"domainName":"hello.test.com",
46-
"apiId":"pb5dg6g3rg",
47-
"requestId":"LgLpnibOFiAEPCA=",
48-
"http":{
49-
"path":"/hello",
50-
"userAgent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
51-
"method":"GET",
52-
"protocol":"HTTP/1.1",
53-
"sourceIp":"91.64.117.86"
54-
},
55-
"time":"24/Apr/2020:17:47:41 +0000"
56-
},
57-
"isBase64Encoded":false,
58-
"rawQueryString":"foo=bar",
59-
"queryStringParameters":{
60-
"foo":"bar"
61-
},
62-
"headers":{
63-
"x-forwarded-proto":"https",
64-
"x-forwarded-for":"91.64.117.86",
65-
"x-forwarded-port":"443",
66-
"authorization":"Bearer abc123",
67-
"host":"hello.test.com",
68-
"x-amzn-trace-id":"Root=1-5ea3263d-07c5d5ddfd0788bed7dad831",
69-
"user-agent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
70-
"content-length":"0"
71-
}
72-
}
73-
"""
74-
20+
{
21+
"routeKey":"GET /hello",
22+
"version":"2.0",
23+
"rawPath":"/hello",
24+
"stageVariables":{
25+
"foo":"bar"
26+
},
27+
"requestContext":{
28+
"timeEpoch":1587750461466,
29+
"domainPrefix":"hello",
30+
"authorizer":{
31+
"jwt":{
32+
"scopes":[
33+
"hello"
34+
],
35+
"claims":{
36+
"aud":"customers",
37+
"iss":"https://hello.test.com/",
38+
"iat":"1587749276",
39+
"exp":"1587756476"
40+
}
41+
}
42+
},
43+
"accountId":"0123456789",
44+
"stage":"$default",
45+
"domainName":"hello.test.com",
46+
"apiId":"pb5dg6g3rg",
47+
"requestId":"LgLpnibOFiAEPCA=",
48+
"http":{
49+
"path":"/hello",
50+
"userAgent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
51+
"method":"GET",
52+
"protocol":"HTTP/1.1",
53+
"sourceIp":"91.64.117.86"
54+
},
55+
"time":"24/Apr/2020:17:47:41 +0000"
56+
},
57+
"isBase64Encoded":false,
58+
"rawQueryString":"foo=bar",
59+
"queryStringParameters":{
60+
"foo":"bar"
61+
},
62+
"headers":{
63+
"x-forwarded-proto":"https",
64+
"x-forwarded-for":"91.64.117.86",
65+
"x-forwarded-port":"443",
66+
"authorization":"Bearer abc123",
67+
"host":"hello.test.com",
68+
"x-amzn-trace-id":"Root=1-5ea3263d-07c5d5ddfd0788bed7dad831",
69+
"user-agent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
70+
"content-length":"0"
71+
}
72+
}
73+
"""
74+
7575
// MARK: - Request -
76-
7776
// MARK: Decoding
78-
7977
func testRequestDecodingExampleGetRequest() {
8078
let data = APIGatewayV2Tests.exampleGetEventBody.data(using: .utf8)!
8179
var req: APIGatewayV2Request?
8280
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
83-
81+
8482
XCTAssertEqual(req?.rawPath, "/hello")
8583
XCTAssertEqual(req?.context.http.method, .GET)
8684
XCTAssertEqual(req?.queryStringParameters?.count, 1)

Tests/AWSLambdaEventsTests/File.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Idelfonso Gutierrez on 4/27/22.
6+
//
7+
8+
import Foundation

0 commit comments

Comments
 (0)