Skip to content

Commit e1eb4c7

Browse files
committed
Removed all high level stuff.
1 parent 25c8dd4 commit e1eb4c7

File tree

7 files changed

+34
-595
lines changed

7 files changed

+34
-595
lines changed

Sources/AWSLambdaEvents/ALB.swift

Lines changed: 21 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import class Foundation.JSONEncoder
1717
// https://github.com/aws/aws-lambda-go/blob/master/events/alb.go
1818
public enum ALB {
1919
/// ALBTargetGroupRequest contains data originating from the ALB Lambda target group integration
20-
public struct TargetGroupRequest {
20+
public struct TargetGroupRequest: Codable {
2121
/// ALBTargetGroupRequestContext contains the information to identify the load balancer invoking the lambda
2222
public struct Context: Codable {
2323
public let elb: ELBContext
@@ -26,7 +26,20 @@ public enum ALB {
2626
public let httpMethod: HTTPMethod
2727
public let path: String
2828
public let queryStringParameters: [String: [String]]
29-
public let headers: HTTPHeaders
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: [String: String]?
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: [String: [String]]?
3043
public let requestContext: Context
3144
public let isBase64Encoded: Bool
3245
public let body: String?
@@ -37,174 +50,28 @@ public enum ALB {
3750
public let targetGroupArn: String
3851
}
3952

40-
public struct TargetGroupResponse {
53+
public struct TargetGroupResponse: Codable {
4154
public let statusCode: HTTPResponseStatus
4255
public let statusDescription: String?
43-
public let headers: HTTPHeaders?
56+
public let headers: [String: String]?
57+
public let multiValueHeaders: [String: [String]]?
4458
public let body: String
4559
public let isBase64Encoded: Bool
4660

4761
public init(
4862
statusCode: HTTPResponseStatus,
4963
statusDescription: String? = nil,
50-
headers: HTTPHeaders? = nil,
64+
headers: [String: String]? = nil,
65+
multiValueHeaders: [String: [String]]? = nil,
5166
body: String = "",
5267
isBase64Encoded: Bool = false
5368
) {
5469
self.statusCode = statusCode
5570
self.statusDescription = statusDescription
5671
self.headers = headers
72+
self.multiValueHeaders = multiValueHeaders
5773
self.body = body
5874
self.isBase64Encoded = isBase64Encoded
5975
}
6076
}
6177
}
62-
63-
// MARK: - Request -
64-
65-
extension ALB.TargetGroupRequest: Decodable {
66-
enum CodingKeys: String, CodingKey {
67-
case httpMethod
68-
case path
69-
case queryStringParameters
70-
case multiValueQueryStringParameters
71-
case headers
72-
case multiValueHeaders
73-
case requestContext
74-
case isBase64Encoded
75-
case body
76-
}
77-
78-
public init(from decoder: Decoder) throws {
79-
let container = try decoder.container(keyedBy: CodingKeys.self)
80-
81-
let rawMethod = try container.decode(String.self, forKey: .httpMethod)
82-
guard let method = HTTPMethod(rawValue: rawMethod) else {
83-
throw DecodingError.dataCorruptedError(
84-
forKey: .httpMethod,
85-
in: container,
86-
debugDescription: #"Method "\#(rawMethod)" does not conform to allowed http method syntax defined in RFC 7230 Section 3.2.6"#
87-
)
88-
}
89-
self.httpMethod = method
90-
91-
self.path = try container.decode(String.self, forKey: .path)
92-
93-
// crazy multiple headers
94-
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
95-
96-
if let multiValueQueryStringParameters =
97-
try container.decodeIfPresent([String: [String]].self, forKey: .multiValueQueryStringParameters) {
98-
self.queryStringParameters = multiValueQueryStringParameters
99-
} else {
100-
let singleValueQueryStringParameters = try container.decode(
101-
[String: String].self,
102-
forKey: .queryStringParameters
103-
)
104-
self.queryStringParameters = singleValueQueryStringParameters.mapValues { [$0] }
105-
}
106-
107-
if let multiValueHeaders =
108-
try container.decodeIfPresent([String: [String]].self, forKey: .multiValueHeaders) {
109-
self.headers = HTTPHeaders(multiValueHeaders)
110-
} else {
111-
let singleValueHeaders = try container.decode(
112-
[String: String].self,
113-
forKey: .headers
114-
)
115-
let multiValueHeaders = singleValueHeaders.mapValues { [$0] }
116-
self.headers = HTTPHeaders(multiValueHeaders)
117-
}
118-
119-
self.requestContext = try container.decode(Context.self, forKey: .requestContext)
120-
self.isBase64Encoded = try container.decode(Bool.self, forKey: .isBase64Encoded)
121-
122-
let body = try container.decode(String.self, forKey: .body)
123-
self.body = body != "" ? body : nil
124-
}
125-
}
126-
127-
// MARK: - Response -
128-
129-
extension ALB.TargetGroupResponse: Encodable {
130-
static let MultiValueHeadersEnabledKey =
131-
CodingUserInfoKey(rawValue: "ALB.TargetGroupResponse.MultiValueHeadersEnabledKey")!
132-
133-
enum CodingKeys: String, CodingKey {
134-
case statusCode
135-
case statusDescription
136-
case headers
137-
case multiValueHeaders
138-
case body
139-
case isBase64Encoded
140-
}
141-
142-
public func encode(to encoder: Encoder) throws {
143-
var container = encoder.container(keyedBy: CodingKeys.self)
144-
try container.encode(statusCode.code, forKey: .statusCode)
145-
146-
let multiValueHeaderSupport =
147-
encoder.userInfo[ALB.TargetGroupResponse.MultiValueHeadersEnabledKey] as? Bool ?? false
148-
149-
switch (multiValueHeaderSupport, headers) {
150-
case (true, .none):
151-
try container.encode([String: String](), forKey: .multiValueHeaders)
152-
case (false, .none):
153-
try container.encode([String: [String]](), forKey: .headers)
154-
case (true, .some(let headers)):
155-
try container.encode(headers.headers, forKey: .multiValueHeaders)
156-
case (false, .some(let headers)):
157-
let singleValueHeaders = headers.headers.mapValues { (values) -> String in
158-
#warning("Is this correct?")
159-
return values.joined(separator: ", ")
160-
}
161-
try container.encode(singleValueHeaders, forKey: .headers)
162-
}
163-
164-
try container.encodeIfPresent(statusDescription, forKey: .statusDescription)
165-
try container.encodeIfPresent(body, forKey: .body)
166-
try container.encodeIfPresent(isBase64Encoded, forKey: .isBase64Encoded)
167-
}
168-
}
169-
170-
extension ALB.TargetGroupResponse {
171-
public init<Payload: Encodable>(
172-
statusCode: HTTPResponseStatus,
173-
statusDescription: String? = nil,
174-
headers: HTTPHeaders? = nil,
175-
payload: Payload,
176-
encoder: JSONEncoder = JSONEncoder()
177-
) throws {
178-
var headers = headers ?? HTTPHeaders()
179-
if !headers.contains(name: "Content-Type") {
180-
headers.add(name: "Content-Type", value: "application/json")
181-
}
182-
183-
self.statusCode = statusCode
184-
self.statusDescription = statusDescription
185-
self.headers = headers
186-
187-
let data = try encoder.encode(payload)
188-
self.body = String(decoding: data, as: Unicode.UTF8.self)
189-
self.isBase64Encoded = false
190-
}
191-
192-
public init(
193-
statusCode: HTTPResponseStatus,
194-
statusDescription: String? = nil,
195-
headers: HTTPHeaders? = nil,
196-
bytes: [UInt8]?
197-
) {
198-
let headers = headers ?? HTTPHeaders()
199-
200-
self.statusCode = statusCode
201-
self.statusDescription = statusDescription
202-
self.headers = headers
203-
if let bytes = bytes {
204-
self.body = String(base64Encoding: bytes)
205-
} else {
206-
self.body = ""
207-
}
208-
self.isBase64Encoded = true
209-
}
210-
}

Sources/AWSLambdaEvents/APIGateway.swift

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import class Foundation.JSONEncoder
1818

1919
public enum APIGateway {
2020
/// APIGatewayRequest contains data coming from the API Gateway
21-
public struct Request {
21+
public struct Request: Codable {
2222
public struct Context: Codable {
2323
public struct Identity: Codable {
2424
public let cognitoIdentityPoolId: String?
@@ -54,7 +54,8 @@ public enum APIGateway {
5454

5555
public let queryStringParameters: [String: String]?
5656
public let multiValueQueryStringParameters: [String: [String]]?
57-
public let headers: HTTPHeaders
57+
public let headers: [String: String]
58+
public let multiValueHeaders: [String: [String]]
5859
public let pathParameters: [String: String]?
5960
public let stageVariables: [String: String]?
6061

@@ -64,54 +65,28 @@ public enum APIGateway {
6465
}
6566
}
6667

67-
// MARK: - Request -
68-
69-
extension APIGateway.Request: Decodable {
70-
enum CodingKeys: String, CodingKey {
71-
case resource
72-
case path
73-
case httpMethod
74-
75-
case queryStringParameters
76-
case multiValueQueryStringParameters
77-
case headers = "multiValueHeaders"
78-
case pathParameters
79-
case stageVariables
80-
81-
case requestContext
82-
case body
83-
case isBase64Encoded
84-
}
85-
}
86-
8768
// MARK: - Response -
8869

8970
extension APIGateway {
90-
public struct Response {
71+
public struct Response: Codable {
9172
public let statusCode: HTTPResponseStatus
92-
public let headers: HTTPHeaders?
73+
public let headers: [String: String]?
74+
public let multiValueHeaders: [String: [String]]?
9375
public let body: String?
9476
public let isBase64Encoded: Bool?
9577

9678
public init(
9779
statusCode: HTTPResponseStatus,
98-
headers: HTTPHeaders? = nil,
80+
headers: [String: String]? = nil,
81+
multiValueHeaders: [String: [String]]? = nil,
9982
body: String? = nil,
10083
isBase64Encoded: Bool? = nil
10184
) {
10285
self.statusCode = statusCode
10386
self.headers = headers
87+
self.multiValueHeaders = multiValueHeaders
10488
self.body = body
10589
self.isBase64Encoded = isBase64Encoded
10690
}
10791
}
10892
}
109-
110-
extension APIGateway.Response: Encodable {
111-
enum CodingKeys: String, CodingKey {
112-
case statusCode
113-
case headers = "multiValueHeaders"
114-
case body
115-
case isBase64Encoded
116-
}
117-
}

0 commit comments

Comments
 (0)