|
| 1 | +//===------------------------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftTencentSCFRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime 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 SwiftTencentSCFRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===------------------------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import struct Foundation.Data |
| 16 | +import class Foundation.JSONEncoder |
| 17 | + |
| 18 | +// https://cloud.tencent.com/document/product/583/12513 |
| 19 | + |
| 20 | +public enum APIGateway { |
| 21 | + /// APIGatewayRequest contains data coming from the API Gateway |
| 22 | + public struct Request: Codable { |
| 23 | + public struct Context: Codable { |
| 24 | + public let identity: [String: String] |
| 25 | + public let serviceId: String |
| 26 | + public let requestId: String |
| 27 | + public let path: String |
| 28 | + public let sourceIp: String |
| 29 | + public let stage: Stage |
| 30 | + public let httpMethod: HTTPMethod |
| 31 | + } |
| 32 | + |
| 33 | + public let path: String |
| 34 | + public let httpMethod: HTTPMethod |
| 35 | + public let headers: HTTPHeaders |
| 36 | + public let query: [String: String] |
| 37 | + |
| 38 | + public let pathParameters: [String: String] |
| 39 | + public let queryStringParameters: [String: String] |
| 40 | + public let headerParameters: [String: String] |
| 41 | + public let stageVariables: [String: String] |
| 42 | + |
| 43 | + public let context: Context |
| 44 | + public let body: String |
| 45 | + |
| 46 | + enum CodingKeys: String, CodingKey { |
| 47 | + case context = "requestContext" |
| 48 | + case body |
| 49 | + case headers |
| 50 | + case query = "queryString" |
| 51 | + case path |
| 52 | + case httpMethod |
| 53 | + |
| 54 | + case pathParameters |
| 55 | + case queryStringParameters |
| 56 | + case headerParameters |
| 57 | + case stageVariables |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public enum Stage: String, Codable { |
| 62 | + case test |
| 63 | + case prepub |
| 64 | + case release |
| 65 | + } |
| 66 | + |
| 67 | + public struct Response: Codable { |
| 68 | + public let statusCode: HTTPResponseStatus |
| 69 | + public let headers: HTTPHeaders? |
| 70 | + public let body: String? |
| 71 | + public let isBase64Encoded: Bool |
| 72 | + |
| 73 | + public init( |
| 74 | + statusCode: HTTPResponseStatus, |
| 75 | + headers: HTTPHeaders? = nil, |
| 76 | + body: String? = nil, |
| 77 | + isBase64Encoded: Bool = false |
| 78 | + ) { |
| 79 | + self.statusCode = statusCode |
| 80 | + self.headers = headers |
| 81 | + self.body = body |
| 82 | + self.isBase64Encoded = isBase64Encoded |
| 83 | + } |
| 84 | + |
| 85 | + public init( |
| 86 | + statusCode: HTTPResponseStatus, |
| 87 | + headers: HTTPHeaders? = nil, |
| 88 | + body: Data? = nil |
| 89 | + ) { |
| 90 | + self.statusCode = statusCode |
| 91 | + self.headers = headers |
| 92 | + self.body = body?.base64EncodedString() |
| 93 | + self.isBase64Encoded = true |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments