|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift OpenAPI Lambda open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Amazon.com, Inc. or its affiliates |
| 6 | +// and the Swift OpenAPI Lambda project authors |
| 7 | +// Licensed under Apache License v2.0 |
| 8 | +// |
| 9 | +// See LICENSE.txt for license information |
| 10 | +// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors |
| 11 | +// |
| 12 | +// SPDX-License-Identifier: Apache-2.0 |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | +import Foundation |
| 16 | +import AWSLambdaRuntime |
| 17 | +import Logging |
| 18 | +import OpenAPIRuntime |
| 19 | +import HTTPTypes |
| 20 | + |
| 21 | +/// A Lambda function implemented with a OpenAPI server (implementing `APIProtocol` from Swift OpenAPIRuntime) |
| 22 | +public protocol OpenAPILambdaService: Sendable { |
| 23 | + |
| 24 | + associatedtype Event: Decodable, Sendable |
| 25 | + associatedtype Output: Encodable, Sendable |
| 26 | + |
| 27 | + /// Injects the transport. |
| 28 | + /// |
| 29 | + /// This is where your OpenAPILambdaService implementation must register the transport |
| 30 | + func register(transport: OpenAPILambdaTransport) throws |
| 31 | + |
| 32 | + /// Convert from `Event` type to `OpenAPILambdaRequest` |
| 33 | + /// - Parameters: |
| 34 | + /// - context: Lambda context |
| 35 | + /// - from: Event |
| 36 | + func request(context: LambdaContext, from: Event) throws -> OpenAPILambdaRequest |
| 37 | + |
| 38 | + /// Convert from `OpenAPILambdaResponse` to `Output` type |
| 39 | + /// - Parameter from: response from OpenAPIRuntime |
| 40 | + func output(from: OpenAPILambdaResponse) -> Output |
| 41 | +} |
| 42 | + |
| 43 | +// extension OpenAPILambdaService { |
| 44 | +// /// Returns the Lambda handler function for this OpenAPILambdaService implementation. |
| 45 | +// /// Use this function when you create a vanilla OpenAPILambdaService and just need to access its handler |
| 46 | +// /// If you need to inject dependencies into your OpenAPILambdaService implementation, |
| 47 | +// /// write your own initializer, such as `init(dbConnection:)` on the OpenAPILambdaService implementation, |
| 48 | +// /// then create the OpenAPILambdaHandler and the LambdaRuntime manually. |
| 49 | +// /// For example: |
| 50 | +// /// let openAPIService = QuoteServiceImpl(i: 42) // my custom OpenAPI service initializer |
| 51 | +// /// let lambda = try OpenAPILambdaHandler(service: openAPIService) |
| 52 | +// /// let lambdaRuntime = LambdaRuntime(body: lambda.handler) |
| 53 | +// /// try await lambdaRuntime.run() |
| 54 | +// /// |
| 55 | +// /// - Returns: A handler function that can be used with AWS Lambda Runtime |
| 56 | +// public static func makeHandler() throws -> OpenAPILambdaHandler<Self> { |
| 57 | +// try OpenAPILambdaHandler<Self>() |
| 58 | +// } |
| 59 | + |
| 60 | +// /// Start the Lambda Runtime with the Lambda handler function for this OpenAPI Lambda implementation with a custom logger, |
| 61 | +// /// when one is given. |
| 62 | +// /// - Parameter logger: The logger to use for Lambda runtime logging |
| 63 | +// public static func run(logger: Logger? = nil) async throws { |
| 64 | +// let _logger = logger ?? Logger(label: "OpenAPILambdaService") |
| 65 | + |
| 66 | +// let handler = LambdaCodableAdapter( |
| 67 | +// encoder: JSONEncoder(), |
| 68 | +// decoder: JSONDecoder(), |
| 69 | +// handler: LambdaHandlerAdapter(handler: try Self.makeHandler()) |
| 70 | +// ) |
| 71 | + |
| 72 | +// let lambdaRuntime = LambdaRuntime(handler: handler, logger: _logger) |
| 73 | +// try await lambdaRuntime.run() |
| 74 | +// } |
| 75 | +// } |
0 commit comments