Skip to content

Commit a7eb085

Browse files
committed
Adapt API Gateway event data
1 parent 90d128b commit a7eb085

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

Examples/CloudFunctions/scripts/serverless/APIGateway/serverless.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ inputs:
2626
endpoints:
2727
- path: /api
2828
method: GET
29+
function:
30+
isIntegratedResponse: TRUE

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ There are several areas which need additional attention, including but not limit
2121
* Additional documentation and best practices
2222
* Additional examples
2323

24+
By August 2020, SCF Custom Runtime is also at an early stage. You may encounter some problems triggered by the SCF Runtime Engine itself, or the API changes and deprecations. You are welcome to open issues actively on those problems.
25+
2426
## Getting started
2527

2628
If you have used [Swift AWS Lambda Runtime](https://github.com/swift-server/swift-aws-lambda-runtime), you may find most of the APIs familiar. If you have never used Tencent SCF, AWS Lambda or Docker before, check out this [getting started guide](https://fabianfett.de/getting-started-with-swift-aws-lambda-runtime) which helps you with every step from zero to a running cloud function.

Sources/TencentSCFEvents/APIGateway.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public enum APIGateway {
2222
public struct Context: Codable {
2323
public let identity: [String: String]
2424
public let serviceId: String
25-
public let requestId: String
2625
public let path: String
2726
public let sourceIp: String
2827
public let stage: Stage
@@ -37,10 +36,9 @@ public enum APIGateway {
3736
public let pathParameters: [String: String]
3837
public let queryStringParameters: [String: String]
3938
public let headerParameters: [String: String]
40-
public let stageVariables: [String: String]
4139

4240
public let context: Context
43-
public let body: String
41+
public let body: String?
4442

4543
enum CodingKeys: String, CodingKey {
4644
case context = "requestContext"
@@ -53,42 +51,42 @@ public enum APIGateway {
5351
case pathParameters
5452
case queryStringParameters
5553
case headerParameters
56-
case stageVariables
5754
}
5855
}
5956

6057
public enum Stage: String, Codable {
6158
case test
59+
case debug
6260
case prepub
6361
case release
6462
}
6563

6664
public struct Response: Codable {
6765
public let statusCode: HTTPResponseStatus
68-
public let headers: HTTPHeaders?
69-
public let body: String?
66+
public let headers: HTTPHeaders
67+
public let body: String
7068
public let isBase64Encoded: Bool
7169

7270
public init(
7371
statusCode: HTTPResponseStatus,
74-
headers: HTTPHeaders? = nil,
72+
headers: HTTPHeaders = [:],
7573
body: String? = nil,
7674
isBase64Encoded: Bool = false
7775
) {
7876
self.statusCode = statusCode
7977
self.headers = headers
80-
self.body = body
78+
self.body = body ?? ""
8179
self.isBase64Encoded = isBase64Encoded
8280
}
8381

8482
public init(
8583
statusCode: HTTPResponseStatus,
86-
headers: HTTPHeaders? = nil,
87-
body: Data? = nil
84+
headers: HTTPHeaders = [:],
85+
body: Data
8886
) {
8987
self.statusCode = statusCode
9088
self.headers = headers
91-
self.body = body?.base64EncodedString()
89+
self.body = body.base64EncodedString()
9290
self.isBase64Encoded = true
9391
}
9492
}

Tests/TencentSCFEventsTests/APIGatewayTests.swift

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ class APIGatewayTests: XCTestCase {
3535
"serviceId": "service-f94sy04v",
3636
"path": "/test/{path}",
3737
"httpMethod": "POST",
38-
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
3938
"identity": {
4039
"secretId": "abdcdxxxxxxxsdfs"
4140
},
4241
"sourceIp": "10.0.2.14",
43-
"stage": "release"
42+
"stage": "debug"
4443
},
4544
"headers": {
46-
"Accept-Language": "en-US,en,cn",
47-
"Accept": "text/html,application/xml,application/json",
48-
"Host": "service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com",
49-
"User-Agent": "User Agent String"
45+
"accept-language": "en-US,en,cn",
46+
"accept": "text/html,application/xml,application/json",
47+
"host": "service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com",
48+
"user-agent": "User Agent String",
49+
"x-anonymous-consumer": "true",
50+
"x-api-requestid": "24281851d905b02add27dad71656f29b",
51+
"x-b3-traceid": "24281851d905b02add27dad71656f29b",
52+
"x-qualifier": "$DEFAULT"
5053
},
5154
"body": "{\"test\":\"body\"}",
5255
"pathParameters": {
@@ -58,9 +61,6 @@ class APIGatewayTests: XCTestCase {
5861
"headerParameters":{
5962
"Refer": "10.0.2.14"
6063
},
61-
"stageVariables": {
62-
"stage": "release"
63-
},
6464
"path": "/test/value",
6565
"queryString": {
6666
"foo": "bar",
@@ -77,29 +77,31 @@ class APIGatewayTests: XCTestCase {
7777

7878
XCTAssertEqual(req?.path, "/test/value")
7979
XCTAssertEqual(req?.body, #"{"test":"body"}"#)
80-
XCTAssertEqual(req?.headers, ["Accept-Language": "en-US,en,cn",
81-
"Accept": "text/html,application/xml,application/json",
82-
"Host": "service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com",
83-
"User-Agent": "User Agent String"])
80+
XCTAssertEqual(req?.headers, ["accept-language": "en-US,en,cn",
81+
"accept": "text/html,application/xml,application/json",
82+
"host": "service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com",
83+
"user-agent": "User Agent String",
84+
"x-anonymous-consumer": "true",
85+
"x-api-requestid": "24281851d905b02add27dad71656f29b",
86+
"x-b3-traceid": "24281851d905b02add27dad71656f29b",
87+
"x-qualifier": "$DEFAULT"])
8488
XCTAssertEqual(req?.query, ["foo": "bar",
8589
"bob": "alice"])
8690
XCTAssertEqual(req?.httpMethod, .POST)
8791

8892
XCTAssertEqual(req?.pathParameters, ["path": "value"])
8993
XCTAssertEqual(req?.queryStringParameters, ["foo": "bar"])
9094
XCTAssertEqual(req?.headerParameters, ["Refer": "10.0.2.14"])
91-
XCTAssertEqual(req?.stageVariables, ["stage": "release"])
9295

9396
XCTAssertEqual(req?.context.sourceIp, "10.0.2.14")
94-
XCTAssertEqual(req?.context.requestId, "c6af9ac6-7b61-11e6-9a41-93e8deadbeef")
9597
XCTAssertEqual(req?.context.serviceId, "service-f94sy04v")
9698
XCTAssertEqual(req?.context.path, "/test/{path}")
9799
XCTAssertEqual(req?.context.httpMethod, .POST)
98-
XCTAssertEqual(req?.context.stage, .release)
100+
XCTAssertEqual(req?.context.stage, .debug)
99101
XCTAssertEqual(req?.context.identity, ["secretId": "abdcdxxxxxxxsdfs"])
100102
}
101103

102-
func testResponseEncodingWithString() {
104+
func testResponseEncodingWithText() {
103105
let resp = APIGateway.Response(
104106
statusCode: .ok,
105107
headers: ["Content-Type": "text/plain"],
@@ -113,16 +115,16 @@ class APIGatewayTests: XCTestCase {
113115

114116
XCTAssertEqual(json?.statusCode, resp.statusCode)
115117
XCTAssertEqual(json?.body, resp.body)
116-
XCTAssertEqual(json?.isBase64Encoded, resp.isBase64Encoded)
117-
XCTAssertEqual(json?.headers?["Content-Type"], "text/plain")
118+
XCTAssertEqual(json?.isBase64Encoded, false)
119+
XCTAssertEqual(json?.headers["Content-Type"], "text/plain")
118120
}
119121

120122
func testResponseEncodingWithData() {
121123
let body = #"{"hello":"swift"}"#
122124
let resp = APIGateway.Response(
123125
statusCode: .ok,
124126
headers: ["Content-Type": "application/json"],
125-
body: body.data(using: .utf8)
127+
body: body.data(using: .utf8)!
126128
)
127129

128130
var data: Data?
@@ -137,7 +139,7 @@ class APIGatewayTests: XCTestCase {
137139

138140
XCTAssertEqual(newResp.statusCode, resp.statusCode)
139141
XCTAssertEqual(newResp.isBase64Encoded, true)
140-
XCTAssertEqual(newResp.headers?["Content-Type"], "application/json")
141-
XCTAssertEqual(Data(base64Encoded: newResp.body!), body.data(using: .utf8))
142+
XCTAssertEqual(newResp.headers["Content-Type"], "application/json")
143+
XCTAssertEqual(Data(base64Encoded: newResp.body), body.data(using: .utf8))
142144
}
143145
}

0 commit comments

Comments
 (0)