Files
swift-openapi-lambda/Sources/OpenAPILambdaService.swift
T
Sébastien Stormacq c72834d93c Update to support AWS Lambda Runtime for Swift v2 (#22)
- Update this library to support the upcoming release of AWS Lambda
Runtime for Swift v2.
This change affects the public API (some `Sendable` added) and requires
a major version bump. I propose to release v2 to align this library's
major with the Swift AWS Lambda Runtime's major.

- Add a QuoteAPI example in the `Examples` directory
2025-09-06 11:34:56 +02:00

57 lines
2.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OpenAPI Lambda open source project
//
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates
// and the Swift OpenAPI Lambda project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import AWSLambdaRuntime
import Logging
import OpenAPIRuntime
import HTTPTypes
/// A Lambda function implemented with a OpenAPI server (implementing `APIProtocol` from Swift OpenAPIRuntime)
public protocol OpenAPILambdaService: Sendable {
associatedtype Event: Decodable, Sendable
associatedtype Output: Encodable, Sendable
/// Injects the transport.
///
/// This is where your OpenAPILambdaService implementation must register the transport
func register(transport: OpenAPILambdaTransport) throws
/// Convert from `Event` type to `OpenAPILambdaRequest`
/// - Parameters:
/// - context: Lambda context
/// - from: Event
func request(context: LambdaContext, from: Event) throws -> OpenAPILambdaRequest
/// Convert from `OpenAPILambdaResponse` to `Output` type
/// - Parameter from: response from OpenAPIRuntime
func output(from: OpenAPILambdaResponse) -> Output
}
extension OpenAPILambdaService {
/// Start the Lambda Runtime with the Lambda handler function
/// for this OpenAPI Lambda service implementation with a custom logger,
///
/// - Parameter logger: The logger to use for Lambda runtime logging
public func run(logger: Logger? = nil) async throws {
let _logger = logger ?? Logger(label: "OpenAPILambdaService")
let lambda = try OpenAPILambdaHandler(withService: self)
let lambdaRuntime = LambdaRuntime(logger: _logger, body: lambda.handle)
try await lambdaRuntime.run()
}
}