Skip to content

Commit bc809f5

Browse files
committed
Implement APIGateway Events
1 parent 3411101 commit bc809f5

File tree

8 files changed

+244
-436
lines changed

8 files changed

+244
-436
lines changed

Examples/CloudFunctions/Sources/APIGateway/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ Lambda.run(APIGatewayProxyLambda())
3737

3838
// FIXME: Use proper Event abstractions once added to AWSLambdaRuntime
3939
struct APIGatewayProxyLambda: EventLoopLambdaHandler {
40-
public typealias In = APIGateway.V2.Request
41-
public typealias Out = APIGateway.V2.Response
40+
public typealias In = APIGateway.Request
41+
public typealias Out = APIGateway.Response
4242

43-
public func handle(context: Lambda.Context, event: APIGateway.V2.Request) -> EventLoopFuture<APIGateway.V2.Response> {
43+
public func handle(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> {
4444
context.logger.debug("hello, api gateway!")
45-
return context.eventLoop.makeSucceededFuture(APIGateway.V2.Response(statusCode: .ok, body: "hello, world!"))
45+
return context.eventLoop.makeSucceededFuture(APIGateway.Response(statusCode: .ok, body: "hello, world!"))
4646
}
4747
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ By default, the library also registers a Signal handler that traps `INT` and `TE
335335

336336
Tencent SCFs can be invoked directly from the Tencent SCF console, Tencent SCF API, TCCLI and Tencent Cloud toolkit. More commonly, they are invoked as a reaction to an events coming from the Tencent Cloud platform. To make it easier to integrate with Tencent Cloud platform events, the library includes an `TencentSCFEvents` target which provides abstractions for many commonly used events. Additional events can be easily modeled when needed following the same patterns set by `TencentSCFEvents`. Integration points with the Tencent Cloud Platform include:
337337

338+
* [APIGateway Request](https://cloud.tencent.com/document/product/583/12513)
338339
* [COS Events](https://cloud.tencent.com/document/product/583/9707)
339340
* [Timer Events](https://cloud.tencent.com/document/product/583/9708)
340341
* [CMQ Topic Messages](https://cloud.tencent.com/document/product/583/11517)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

Sources/TencentSCFEvents/AWS/APIGateway+V2.swift

Lines changed: 0 additions & 132 deletions
This file was deleted.

Sources/TencentSCFEvents/AWS/APIGateway.swift

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)