2
2
//
3
3
// This source file is part of the SwiftTencentSCFRuntime open source project
4
4
//
5
- // Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime project authors
5
+ // Copyright (c) 2020-2021 stevapple and the SwiftTencentSCFRuntime project authors
6
6
// Licensed under Apache License v2.0
7
7
//
8
8
// See LICENSE.txt for license information
@@ -19,7 +19,7 @@ import class Foundation.JSONEncoder
19
19
20
20
public enum APIGateway {
21
21
/// `APIGateway.Request` contains data coming from the API Gateway.
22
- public struct Request < T: Decodable > {
22
+ public struct Request < T: Decodable > : Decodable {
23
23
public struct Context : Decodable {
24
24
public let identity : [ String : String ]
25
25
public let serviceId : String
@@ -40,6 +40,43 @@ public enum APIGateway {
40
40
41
41
public let context : Context
42
42
public let body : T ?
43
+
44
+ enum CodingKeys : String , CodingKey {
45
+ case context = " requestContext "
46
+ case body
47
+ case headers
48
+ case query = " queryString "
49
+ case path
50
+ case httpMethod
51
+
52
+ case pathParameters
53
+ case queryStringParameters
54
+ case headerParameters
55
+ }
56
+
57
+ public init ( from decoder: Decoder ) throws {
58
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
59
+
60
+ context = try container. decode ( Context . self, forKey: . context)
61
+ headers = try container. decode ( HTTPHeaders . self, forKey: . headers)
62
+ path = try container. decode ( String . self, forKey: . path)
63
+ httpMethod = try container. decode ( HTTPMethod . self, forKey: . httpMethod)
64
+ query = try container. decode ( [ String : String ] . self, forKey: . query)
65
+ pathParameters = try container. decode ( [ String : String ] . self, forKey: . pathParameters)
66
+ queryStringParameters = try container. decode ( [ String : String ] . self, forKey: . queryStringParameters)
67
+ headerParameters = try container. decode ( [ String : String ] . self, forKey: . headerParameters)
68
+
69
+ do {
70
+ body = try container. decodeIfPresent ( T . self, forKey: . body)
71
+ } catch {
72
+ let bodyJson = try container. decodeIfPresent ( String . self, forKey: . body)
73
+ if let data = bodyJson? . data ( using: . utf8) {
74
+ body = try APIGateway . defaultJSONDecoder. decode ( T . self, from: data)
75
+ } else {
76
+ body = nil
77
+ }
78
+ }
79
+ }
43
80
}
44
81
45
82
public enum Stage : String , Decodable {
0 commit comments