@@ -20,6 +20,8 @@ import Foundation
2020import LambdaSwiftSprinter
2121import LambdaSwiftSprinterNioPlugin
2222import Logging
23+ import NIO
24+ import NIOFoundationCompat
2325
2426struct Event : Codable {
2527 let url : String
@@ -30,32 +32,182 @@ struct Response: Codable {
3032 let content : String
3133}
3234
33- extension Array where Element == UInt8 {
34- var data : Data {
35- return Data ( self )
36- }
35+ enum MyError : Error {
36+ case invalidParameters
3737}
3838
3939let logger = Logger ( label: " AWS.Lambda.HTTPSRequest " )
4040
41- let lambda : SyncCodableLambda < Event , Response > = { ( input, context) throws -> Response in
41+ /**
42+ How to use the `SyncCodableNIOLambda<Event, Response>` lambda handler.
4243
43- let request = try HTTPClient . Request ( url: input. url)
44- let response = try httpClient. execute ( request: request) . wait ( )
44+ - The code is used by this example.
45+ - Make sure the handler is registered:
46+
47+ ```
48+ sprinter.register(handler: "getHttps", lambda: syncCodableNIOLambda)
49+ ```
50+ */
51+ let syncCodableNIOLambda : SyncCodableNIOLambda < Event , Response > = { ( event, context) throws -> EventLoopFuture < Response > in
52+
53+ let request = try HTTPClient . Request ( url: event. url)
54+ let future = httpClient. execute ( request: request, deadline: nil )
55+ . flatMapThrowing { ( response) throws -> String in
56+ guard let body = response. body,
57+ let value = body. getString ( at: 0 , length: body. readableBytes) else {
58+ throw SprinterError . invalidJSON
59+ }
60+ return value
61+ } . map { content -> Response in
62+ return Response ( url: event. url, content: content)
63+ }
64+ return future
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
4581
82+ guard let url = event [ " url " ] as? String else {
83+ throw MyError . invalidParameters
84+ }
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
99+ }
100+
101+ /**
102+ How to use the `AsyncDictionaryNIOLambda` lambda handler.
103+
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+ }
138+
139+ /**
140+ How to use the `AsyncCodableNIOLambda<Event, Response>` lambda handler.
141+
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 core framework.
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
204+
205+ /// The following code it's required to setup, register, run the lambda and log errors.
56206do {
57207 let sprinter = try SprinterNIO ( )
58- sprinter. register ( handler: " getHttps " , lambda: lambda)
208+ //Note amend this line in case if it's required to use a different lambda handler.
209+ sprinter. register ( handler: " getHttps " , lambda: syncCodableNIOLambda)
210+
59211 try sprinter. run ( )
60212} catch {
61213 logger. error ( " \( String ( describing: error) ) " )
0 commit comments