|
| 1 | +// Copyright 2019 (c) Andrea Scuderi - https://github.com/swift-sprinter |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import NIO |
| 17 | +import NIOHTTP1 |
| 18 | +import NIOFoundationCompat |
| 19 | +import LambdaSwiftSprinter |
| 20 | + |
| 21 | +public typealias AsyncDictionaryNIOLambda = ([String: Any], Context, @escaping (DictionaryResult) -> Void) -> Void |
| 22 | +public typealias AsyncCodableNIOLambda<Event: Decodable, Response: Encodable> = (Event, Context, @escaping (Result<Response, Error>) -> Void) -> Void |
| 23 | + |
| 24 | +public typealias SyncDictionaryNIOLambda = ([String: Any], Context) throws -> [String: Any] |
| 25 | +public typealias SyncCodableNIOLambda<Event: Decodable, Response: Encodable> = (Event, Context) -> EventLoopFuture<Response> |
| 26 | + |
| 27 | +public protocol SyncNIOLambdaHandler: LambdaHandler { |
| 28 | + |
| 29 | + func handler(event: Data, context: Context) -> EventLoopFuture<Data> |
| 30 | +} |
| 31 | + |
| 32 | +public extension SyncNIOLambdaHandler { |
| 33 | + |
| 34 | + func commonHandler(event: Data, context: Context) -> LambdaResult { |
| 35 | + do { |
| 36 | + let data = try handler(event: event, context: context).wait() |
| 37 | + return .success(data) |
| 38 | + } catch { |
| 39 | + return .failure(error) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +public protocol AsyncNIOLambdaHandler: LambdaHandler { |
| 45 | + |
| 46 | + func handler(event: Data, context: Context, completion: @escaping (LambdaResult) -> Void) |
| 47 | +} |
| 48 | + |
| 49 | +public extension AsyncNIOLambdaHandler { |
| 50 | + func commonHandler(event: Data, context: Context) -> LambdaResult { |
| 51 | + let eventLoop = httpClient.eventLoopGroup.next() |
| 52 | + let promise = eventLoop.makePromise(of: LambdaResult.self) |
| 53 | + handler(event: event, context: context) { result in |
| 54 | + |
| 55 | + switch result { |
| 56 | + case .success(_): |
| 57 | + promise.succeed(result) |
| 58 | + case .failure(let error): |
| 59 | + promise.fail(error) |
| 60 | + } |
| 61 | + } |
| 62 | + do { |
| 63 | + let result = try promise.futureResult.wait() |
| 64 | + return result |
| 65 | + } catch { |
| 66 | + return .failure(error) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +struct CodableSyncNIOLambdaHandler<Event: Decodable, Response: Encodable>: SyncNIOLambdaHandler { |
| 72 | + |
| 73 | + let handlerFunction: (Event, Context) -> EventLoopFuture<Response> |
| 74 | + |
| 75 | + func handler(event: Data, context: Context) -> EventLoopFuture<Data> { |
| 76 | + |
| 77 | + let eventLoop = httpClient.eventLoopGroup.next() |
| 78 | + let promise = eventLoop.makePromise(of: Data.self) |
| 79 | + do { |
| 80 | + let eventObj = try event.decode() as Event |
| 81 | + let responseObj = try handlerFunction(eventObj, context).wait() |
| 82 | + let response = try Data(from: responseObj) |
| 83 | + promise.succeed(response) |
| 84 | + } catch { |
| 85 | + promise.fail(error) |
| 86 | + } |
| 87 | + return promise.futureResult |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +struct CodableAsyncNIOLambdaHandler<Event: Decodable, Response: Encodable>: AsyncNIOLambdaHandler { |
| 92 | + let handlerFunction: AsyncCodableLambda<Event, Response> |
| 93 | + |
| 94 | + func handler(event: Data, context: Context, completion: @escaping (LambdaResult) -> Void) { |
| 95 | + do { |
| 96 | + let data = try event.decode() as Event |
| 97 | + handlerFunction(data, context) { outputResult in |
| 98 | + switch outputResult { |
| 99 | + case .failure(let error): |
| 100 | + completion(.failure(error)) |
| 101 | + case .success(let outputDict): |
| 102 | + do { |
| 103 | + let outputData = try Data(from: outputDict) |
| 104 | + completion(.success(outputData)) |
| 105 | + } catch { |
| 106 | + completion(.failure(error)) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } catch { |
| 111 | + completion(.failure(error)) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +struct DictionarySyncNIOLambdaHandler: SyncNIOLambdaHandler { |
| 117 | + |
| 118 | + let completionHandler: ([String: Any], Context) throws -> [String: Any] |
| 119 | + func handler(event: Data, context: Context) -> EventLoopFuture<Data> { |
| 120 | + |
| 121 | + let eventLoop = httpClient.eventLoopGroup.next() |
| 122 | + let promise = eventLoop.makePromise(of: Data.self) |
| 123 | + do { |
| 124 | + let data = try event.jsonObject() |
| 125 | + let output = try completionHandler(data, context) |
| 126 | + let responseObj = try Data(jsonObject: output) |
| 127 | + promise.succeed(responseObj) |
| 128 | + } catch { |
| 129 | + promise.fail(error) |
| 130 | + } |
| 131 | + return promise.futureResult |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +struct DictionaryAsyncNIOLambdaHandler: AsyncNIOLambdaHandler { |
| 136 | + let completionHandler: AsyncDictionaryLambda |
| 137 | + |
| 138 | + func handler(event: Data, context: Context, completion: @escaping (LambdaResult) -> Void) { |
| 139 | + do { |
| 140 | + let jsonDictionary = try event.jsonObject() |
| 141 | + completionHandler(jsonDictionary, context) { outputResult in |
| 142 | + switch outputResult { |
| 143 | + case .failure(let error): |
| 144 | + completion(.failure(error)) |
| 145 | + case .success(let outputDict): |
| 146 | + do { |
| 147 | + let outputData = try Data(jsonObject: outputDict) |
| 148 | + completion(.success(outputData)) |
| 149 | + } catch { |
| 150 | + completion(.failure(error)) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } catch { |
| 155 | + completion(.failure(error)) |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +public extension Sprinter where API == LambdaApiNIO { |
| 161 | + func register(handler name: String, lambda: @escaping SyncDictionaryNIOLambda) { |
| 162 | + let handler = DictionarySyncNIOLambdaHandler(completionHandler: lambda) |
| 163 | + register(handler: name, lambda: handler) |
| 164 | + } |
| 165 | + |
| 166 | + func register(handler name: String, lambda: @escaping AsyncDictionaryNIOLambda) { |
| 167 | + let handler = DictionaryAsyncNIOLambdaHandler(completionHandler: lambda) |
| 168 | + register(handler: name, lambda: handler) |
| 169 | + } |
| 170 | + |
| 171 | + func register<Event: Decodable, Response: Encodable>(handler name: String, |
| 172 | + lambda: @escaping SyncCodableNIOLambda<Event, Response>) { |
| 173 | + let handler = CodableSyncNIOLambdaHandler(handlerFunction: lambda) |
| 174 | + register(handler: name, lambda: handler) |
| 175 | + } |
| 176 | + |
| 177 | + func register<Event: Decodable, Response: Encodable>(handler name: String, |
| 178 | + lambda: @escaping AsyncCodableNIOLambda<Event, Response>) { |
| 179 | + let handler = CodableAsyncNIOLambdaHandler(handlerFunction: lambda) |
| 180 | + register(handler: name, lambda: handler) |
| 181 | + } |
| 182 | +} |
0 commit comments