Skip to content

Commit 8509674

Browse files
authored
Add apigateway and alb (#52)
motivation: support ALB and APIGateway Events changes: add Codable abstractions for ALB and APIGateway v1+v2 Events
1 parent 2297929 commit 8509674

File tree

9 files changed

+707
-161
lines changed

9 files changed

+707
-161
lines changed

Sources/AWSLambdaEvents/ALB.swift

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 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+
import class Foundation.JSONEncoder
16+
17+
// https://github.com/aws/aws-lambda-go/blob/master/events/alb.go
18+
public enum ALB {
19+
/// ALBTargetGroupRequest contains data originating from the ALB Lambda target group integration
20+
public struct TargetGroupRequest: Codable {
21+
/// ALBTargetGroupRequestContext contains the information to identify the load balancer invoking the lambda
22+
public struct Context: Codable {
23+
public let elb: ELBContext
24+
}
25+
26+
public let httpMethod: HTTPMethod
27+
public let path: String
28+
public let queryStringParameters: [String: [String]]
29+
30+
/// Depending on your configuration of your target group either `headers` or `multiValueHeaders`
31+
/// are set.
32+
///
33+
/// For more information visit:
34+
/// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
35+
public let headers: HTTPHeaders?
36+
37+
/// Depending on your configuration of your target group either `headers` or `multiValueHeaders`
38+
/// are set.
39+
///
40+
/// For more information visit:
41+
/// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
42+
public let multiValueHeaders: HTTPMultiValueHeaders?
43+
public let requestContext: Context
44+
public let isBase64Encoded: Bool
45+
public let body: String?
46+
}
47+
48+
/// ELBContext contains the information to identify the ARN invoking the lambda
49+
public struct ELBContext: Codable {
50+
public let targetGroupArn: String
51+
}
52+
53+
public struct TargetGroupResponse: Codable {
54+
public let statusCode: HTTPResponseStatus
55+
public let statusDescription: String?
56+
public let headers: HTTPHeaders?
57+
public let multiValueHeaders: HTTPMultiValueHeaders?
58+
public let body: String
59+
public let isBase64Encoded: Bool
60+
61+
public init(
62+
statusCode: HTTPResponseStatus,
63+
statusDescription: String? = nil,
64+
headers: HTTPHeaders? = nil,
65+
multiValueHeaders: HTTPMultiValueHeaders? = nil,
66+
body: String = "",
67+
isBase64Encoded: Bool = false
68+
) {
69+
self.statusCode = statusCode
70+
self.statusDescription = statusDescription
71+
self.headers = headers
72+
self.multiValueHeaders = multiValueHeaders
73+
self.body = body
74+
self.isBase64Encoded = isBase64Encoded
75+
}
76+
}
77+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 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+
extension APIGateway {
16+
public struct V2 {}
17+
}
18+
19+
extension APIGateway.V2 {
20+
/// APIGateway.V2.Request contains data coming from the new HTTP API Gateway
21+
public struct Request: Codable {
22+
/// Context contains the information to identify the AWS account and resources invoking the Lambda function.
23+
public struct Context: Codable {
24+
public struct HTTP: Codable {
25+
public let method: HTTPMethod
26+
public let path: String
27+
public let `protocol`: String
28+
public let sourceIp: String
29+
public let userAgent: String
30+
}
31+
32+
/// Authorizer contains authorizer information for the request context.
33+
public struct Authorizer: Codable {
34+
/// JWT contains JWT authorizer information for the request context.
35+
public struct JWT: Codable {
36+
public let claims: [String: String]
37+
public let scopes: [String]?
38+
}
39+
40+
let jwt: JWT
41+
}
42+
43+
public let accountId: String
44+
public let apiId: String
45+
public let domainName: String
46+
public let domainPrefix: String
47+
public let stage: String
48+
public let requestId: String
49+
50+
public let http: HTTP
51+
public let authorizer: Authorizer?
52+
53+
/// The request time in format: 23/Apr/2020:11:08:18 +0000
54+
public let time: String
55+
public let timeEpoch: UInt64
56+
}
57+
58+
public let version: String
59+
public let routeKey: String
60+
public let rawPath: String
61+
public let rawQueryString: String
62+
63+
public let cookies: [String]?
64+
public let headers: HTTPHeaders
65+
public let queryStringParameters: [String: String]?
66+
public let pathParameters: [String: String]?
67+
68+
public let context: Context
69+
public let stageVariables: [String: String]?
70+
71+
public let body: String?
72+
public let isBase64Encoded: Bool
73+
74+
enum CodingKeys: String, CodingKey {
75+
case version
76+
case routeKey
77+
case rawPath
78+
case rawQueryString
79+
80+
case cookies
81+
case headers
82+
case queryStringParameters
83+
case pathParameters
84+
85+
case context = "requestContext"
86+
case stageVariables
87+
88+
case body
89+
case isBase64Encoded
90+
}
91+
}
92+
}
93+
94+
extension APIGateway.V2 {
95+
public struct Response: Codable {
96+
public let statusCode: HTTPResponseStatus
97+
public let headers: HTTPHeaders?
98+
public let multiValueHeaders: HTTPMultiValueHeaders?
99+
public let body: String?
100+
public let isBase64Encoded: Bool?
101+
public let cookies: [String]?
102+
103+
public init(
104+
statusCode: HTTPResponseStatus,
105+
headers: HTTPHeaders? = nil,
106+
multiValueHeaders: HTTPMultiValueHeaders? = nil,
107+
body: String? = nil,
108+
isBase64Encoded: Bool? = nil,
109+
cookies: [String]? = nil
110+
) {
111+
self.statusCode = statusCode
112+
self.headers = headers
113+
self.multiValueHeaders = multiValueHeaders
114+
self.body = body
115+
self.isBase64Encoded = isBase64Encoded
116+
self.cookies = cookies
117+
}
118+
}
119+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 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+
import class Foundation.JSONEncoder
16+
17+
// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
18+
19+
public enum APIGateway {
20+
/// APIGatewayRequest contains data coming from the API Gateway
21+
public struct Request: Codable {
22+
public struct Context: Codable {
23+
public struct Identity: Codable {
24+
public let cognitoIdentityPoolId: String?
25+
26+
public let apiKey: String?
27+
public let userArn: String?
28+
public let cognitoAuthenticationType: String?
29+
public let caller: String?
30+
public let userAgent: String?
31+
public let user: String?
32+
33+
public let cognitoAuthenticationProvider: String?
34+
public let sourceIp: String?
35+
public let accountId: String?
36+
}
37+
38+
public let resourceId: String
39+
public let apiId: String
40+
public let resourcePath: String
41+
public let httpMethod: String
42+
public let requestId: String
43+
public let accountId: String
44+
public let stage: String
45+
46+
public let identity: Identity
47+
public let extendedRequestId: String?
48+
public let path: String
49+
}
50+
51+
public let resource: String
52+
public let path: String
53+
public let httpMethod: HTTPMethod
54+
55+
public let queryStringParameters: [String: String]?
56+
public let multiValueQueryStringParameters: [String: [String]]?
57+
public let headers: HTTPHeaders
58+
public let multiValueHeaders: HTTPMultiValueHeaders
59+
public let pathParameters: [String: String]?
60+
public let stageVariables: [String: String]?
61+
62+
public let requestContext: Context
63+
public let body: String?
64+
public let isBase64Encoded: Bool
65+
}
66+
}
67+
68+
// MARK: - Response -
69+
70+
extension APIGateway {
71+
public struct Response: Codable {
72+
public let statusCode: HTTPResponseStatus
73+
public let headers: HTTPHeaders?
74+
public let multiValueHeaders: HTTPMultiValueHeaders?
75+
public let body: String?
76+
public let isBase64Encoded: Bool?
77+
78+
public init(
79+
statusCode: HTTPResponseStatus,
80+
headers: HTTPHeaders? = nil,
81+
multiValueHeaders: HTTPMultiValueHeaders? = nil,
82+
body: String? = nil,
83+
isBase64Encoded: Bool? = nil
84+
) {
85+
self.statusCode = statusCode
86+
self.headers = headers
87+
self.multiValueHeaders = multiValueHeaders
88+
self.body = body
89+
self.isBase64Encoded = isBase64Encoded
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)