Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 58a1108

Browse files
FIX: HTTPSRequest example
TODO: UPDATE Package.swift
1 parent c463ece commit 58a1108

File tree

3 files changed

+179
-29
lines changed

3 files changed

+179
-29
lines changed

Examples/HTTPSRequest/Package.resolved

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/HTTPSRequest/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
dependencies: [
99
// Dependencies declare other packages that this package depends on.
1010
//.package(path: "../../../aws-lambda-swift-sprinter-nio-plugin"),
11-
.package(url: "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", from: "1.0.0-alpha.2"),
11+
.package(url: "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", from: "1.0.0-alpha.3"),
1212
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
1313
],
1414
targets: [

Examples/HTTPSRequest/Sources/HTTPSRequest/main.swift

Lines changed: 162 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import Foundation
2020
import LambdaSwiftSprinter
2121
import LambdaSwiftSprinterNioPlugin
2222
import Logging
23+
import NIO
24+
import NIOFoundationCompat
2325

2426
struct Event: Codable {
2527
let url: String
@@ -30,32 +32,180 @@ struct Response: Codable {
3032
let content: String
3133
}
3234

33-
extension Array where Element == UInt8 {
34-
var data: Data {
35-
return Data(self)
35+
let logger = Logger(label: "AWS.Lambda.HTTPSRequest")
36+
37+
/**
38+
How to use the `SyncCodableNIOLambda<Event, Response>` lambda handler.
39+
40+
- The code is used by this example.
41+
- Make sure the handler is registered:
42+
43+
```
44+
sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
45+
```
46+
*/
47+
let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
48+
49+
let request = try HTTPClient.Request(url: event.url)
50+
let future = httpClient.execute(request: request, deadline: nil)
51+
.flatMapThrowing { (response) throws -> String in
52+
guard let body = response.body,
53+
let value = body.getString(at: 0, length: body.readableBytes) else {
54+
throw SprinterError.invalidJSON
55+
}
56+
return value
57+
}.map { content -> Response in
58+
return Response(url: event.url, content: content)
59+
}
60+
return future
61+
}
62+
63+
enum MyError: Error {
64+
case invalidParameters
65+
}
66+
67+
/**
68+
How to use the `SyncDictionaryNIOLambda` lambda handler.
69+
70+
- The code is unused.
71+
- Make sure the handler is registered.
72+
- If it's required by the lambda implementation, amend the following lines:
73+
74+
```
75+
//sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
76+
sprinter.register(handler: "getHttps", lambda: syncDictionaryNIOLambda)
77+
78+
```
79+
*/
80+
let syncDictionaryNIOLambda: SyncDictionaryNIOLambda = { (event, context) throws -> EventLoopFuture<[String: Any]> in
81+
82+
guard let url = event["url"] as? String else {
83+
throw MyError.invalidParameters
3684
}
85+
86+
let request = try HTTPClient.Request(url: url)
87+
let future = httpClient.execute(request: request, deadline: nil)
88+
.flatMapThrowing { (response) throws -> String in
89+
guard let body = response.body,
90+
let value = body.getString(at: 0, length: body.readableBytes) else {
91+
throw SprinterError.invalidJSON
92+
}
93+
return value
94+
}.map { content -> [String: Any] in
95+
return ["url": url,
96+
"content": content]
97+
}
98+
return future
3799
}
38100

39-
let logger = Logger(label: "AWS.Lambda.HTTPSRequest")
101+
/**
102+
How to use the `AsyncDictionaryNIOLambda` lambda handler.
40103

41-
let lambda: SyncCodableLambda<Event, Response> = { (input, context) throws -> Response in
104+
- The code is unused.
105+
- Make sure the handler is registered.
106+
- If it's required by the lambda implementation, amend the following lines:
107+
108+
```
109+
//sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
110+
sprinter.register(handler: "getHttps", lambda: asynchDictionayNIOLambda)
111+
112+
```
113+
*/
114+
let asynchDictionayNIOLambda: AsyncDictionaryNIOLambda = { (event, context, completion) -> Void in
115+
guard let url = event["url"] as? String else {
116+
completion(.failure(MyError.invalidParameters))
117+
return
118+
}
119+
do {
120+
let request = try HTTPClient.Request(url: url)
121+
let dictionary: [String: Any] = try httpClient.execute(request: request, deadline: nil)
122+
.flatMapThrowing { (response) throws -> String in
123+
guard let body = response.body,
124+
let value = body.getString(at: 0, length: body.readableBytes) else {
125+
throw SprinterError.invalidJSON
126+
}
127+
return value
128+
}.map { content -> [String: Any] in
129+
return ["url": url,
130+
"content": content]
131+
}
132+
.wait()
133+
completion(.success(dictionary))
134+
} catch {
135+
completion(.failure(error))
136+
}
137+
}
42138

43-
let request = try HTTPClient.Request(url: input.url)
44-
let response = try httpClient.execute(request: request).wait()
139+
/**
140+
How to use the `AsyncCodableNIOLambda<Event, Response>` lambda handler.
45141

142+
- The code is unused.
143+
- Make sure the handler is registered.
144+
- If it's required by the lambda implementation, amend the following lines:
145+
146+
```
147+
//sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
148+
sprinter.register(handler: "getHttps", lambda: asyncCodableNIOLambda)
149+
150+
```
151+
*/
152+
let asyncCodableNIOLambda: AsyncCodableNIOLambda<Event, Response> = { (event, context, completion) -> Void in
153+
do {
154+
let request = try HTTPClient.Request(url: event.url)
155+
let reponse: Response = try httpClient.execute(request: request, deadline: nil)
156+
.flatMapThrowing { (response) throws -> String in
157+
guard let body = response.body,
158+
let value = body.getString(at: 0, length: body.readableBytes) else {
159+
throw SprinterError.invalidJSON
160+
}
161+
return value
162+
}.map { content -> Response in
163+
return Response(url: event.url, content: content)
164+
}
165+
.wait()
166+
completion(.success(reponse))
167+
} catch {
168+
completion(.failure(error))
169+
}
170+
}
171+
172+
/**
173+
Deprecated style of implementing the lambda using the NIO plugin.
174+
175+
- The example has been left to keep the compatibility with the tutorial:
176+
177+
[How to work with aws lambda in swift](https://medium.com/better-programming/how-to-work-with-aws-lambda-in-swift-28326c5cc765)
178+
179+
180+
- The code is unused.
181+
- Make sure the handler is registered.
182+
- If it's required by the lambda implementation, amend the following lines:
183+
184+
```
185+
//sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
186+
sprinter.register(handler: "getHttps", lambda: lambda)
187+
188+
```
189+
*/
190+
let lambda: SyncCodableLambda<Event, Response> = { (input, context) throws -> Response in
191+
192+
let request = try HTTPClient.Request(url: input.url)
193+
let response = try httpClient.execute(request: request, deadline: nil).wait()
194+
46195
guard let body = response.body,
47-
let buffer = body.getBytes(at: 0, length: body.readableBytes) else {
48-
throw SprinterError.invalidJSON
196+
let data = body.getData(at: 0, length: body.readableBytes) else {
197+
throw SprinterError.invalidJSON
49198
}
50-
let data = Data(buffer)
51199
let content = String(data: data, encoding: .utf8) ?? ""
52-
200+
53201
return Response(url: input.url, content: content)
54202
}
55203

56204
do {
57205
let sprinter = try SprinterNIO()
58-
sprinter.register(handler: "getHttps", lambda: lambda)
206+
//Note amend this line in case if it's required to use a different lambda handler.
207+
sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
208+
59209
try sprinter.run()
60210
} catch {
61211
logger.error("\(String(describing: error))")

0 commit comments

Comments
 (0)