22//
33// This source file is part of the SwiftTencentSCFRuntime open source project
44//
5- // Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime project authors
5+ // Copyright (c) 2020-2021 stevapple and the SwiftTencentSCFRuntime project authors
66// Licensed under Apache License v2.0
77//
88// See LICENSE.txt for license information
@@ -19,7 +19,7 @@ import class Foundation.JSONEncoder
1919
2020public enum APIGateway {
2121 /// `APIGateway.Request` contains data coming from the API Gateway.
22- public struct Request < T: Decodable > {
22+ public struct Request < T: Decodable > : Decodable {
2323 public struct Context : Decodable {
2424 public let identity : [ String : String ]
2525 public let serviceId : String
@@ -40,6 +40,43 @@ public enum APIGateway {
4040
4141 public let context : Context
4242 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+ }
4380 }
4481
4582 public enum Stage : String , Decodable {
0 commit comments