Skip to content

Commit acd6e6a

Browse files
author
Stormacq, Sebastien
committed
make sample a bit more complex by calling an HTTPS endpoint
1 parent f630a3d commit acd6e6a

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// LambdaRuntime API implementation for Swift 4
3+
//
4+
// Published under dual Apache 2.0
5+
// https://www.apache.org/licenses/LICENSE-2.0
6+
// Sebastien Stormacq, (c) 2018 stormacq.com
7+
//
8+
import Foundation
9+
10+
import LambdaRuntime
11+
import LoggerAPI
12+
13+
func handler(context: Context, event: LambdaEvent) throws -> LambdaResponse {
14+
Log.debug("Starting lambda handler")
15+
16+
// fetch data from https://httpbin.org/json
17+
let endpoint = "https://httpbin.org/get?value=\(event["key1"] ?? "no value provided")"
18+
Log.debug(endpoint)
19+
let (data, response, error) = URLSession.shared.synchronousDataTask(with: endpoint) //if url is invalid, it is a programming error
20+
21+
// did we receive an error ?
22+
guard error == nil else {
23+
return [ "error": error!.localizedDescription ]
24+
}
25+
26+
guard let event = data else {
27+
return [ "error": "no body returned" ]
28+
}
29+
30+
guard let resp = response else {
31+
return [ "error": "no response returned" ]
32+
}
33+
34+
if (resp.statusCode == 200) {
35+
return try JSONify(jsonData: event) as LambdaEvent
36+
} else {
37+
return [ "error": String(resp.statusCode) ]
38+
}
39+
40+
}
41+
42+
try LambdaRuntime(handler).run()
43+

lambda-function/Sources/hello/main.swift

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

0 commit comments

Comments
 (0)