Skip to content

Commit 3059e52

Browse files
committed
wip
1 parent 1ea51e5 commit 3059e52

File tree

4 files changed

+90
-106
lines changed

4 files changed

+90
-106
lines changed

Examples/quoteapi/Sources/QuoteAPI/QuoteService.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
2828
static func main() async throws {
2929

3030
// when you just need to run the Lambda function, call Self.run()
31-
try await Self.run()
31+
// try await Self.run()
3232

3333
// =============================
3434

@@ -62,18 +62,17 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
6262
// 3. create the lambda handler and pass the instance to its initializer
6363
// 4. create the lambda runtime and pass the handler to its initializer
6464

65-
// let openAPIService = QuoteServiceImpl(i: 42) // 2.
66-
// let lambda = try OpenAPILambdaHandler(service: openAPIService) // 3.
67-
// let lambdaRuntime = LambdaRuntime(body: lambda.handler) // 4.
68-
// try await lambdaRuntime.run()
65+
let openAPIService = QuoteServiceImpl(i: 42) // 2.
66+
let lambda = try OpenAPILambdaHandler(withService: openAPIService) // 3.
67+
let lambdaRuntime = LambdaRuntime(body: lambda.handle) // 4.
68+
try await lambdaRuntime.run()
6969
}
7070

7171
// example of dependency injection when using a custom initializer
72-
// let i: Int
73-
// init(i: Int) { // 1.
74-
// self.i = i
75-
// }
76-
// init() { self.i = 0 } // to comply with protocol
72+
let i: Int
73+
init(i: Int) { // 1.
74+
self.i = i
75+
}
7776

7877
func getQuote(_ input: Operations.getQuote.Input) async throws -> Operations.getQuote.Output {
7978

Sources/OpenAPILambda.swift

Lines changed: 0 additions & 78 deletions
This file was deleted.

Sources/OpenAPILambdaHandler.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,16 @@ public struct OpenAPILambdaHandler<OALS: OpenAPILambdaService>: LambdaHandler {
3232

3333
/// Initialize `OpenAPILambdaHandler`.
3434
///
35-
/// Create application, set it up and create `OpenAPILambda` from application and create responder
35+
/// This initializer decouples the OpenAPILambdaService creation from the registration of the transport
36+
/// this allows users to control the lifecycle of their service and to inject dependencies.
37+
///
3638
/// - Parameters
37-
/// - context: Lambda initialization context
38-
init() throws {
39+
/// - withService: The OpenAPI Lambda service to bind to this Lambda handler function
40+
public init(withService openAPILambdaService: OALS) throws {
3941
self.router = TrieRouter()
4042
self.transport = OpenAPILambdaTransport(router: self.router)
4143

42-
// decouple the OpenAPILambda creation from the registration of teh transport
43-
// this allows users to provide their own overloaded init() function to inject dependencies.
44-
self.openAPIService = OALS()
45-
try self.openAPIService.register(transport: self.transport)
46-
}
47-
48-
/// Initialize an `OpenAPILambdaHandler` with your own `OpenAPILambda` object.
49-
///
50-
/// Use this function when you need to inject dependencies in your OpenAPILambda
51-
/// - Parameters:
52-
/// - lambda: The `OpenAPILambda` instance to use.
53-
public init(service: OALS) throws {
54-
self.router = TrieRouter()
55-
self.transport = OpenAPILambdaTransport(router: self.router)
56-
self.openAPIService = service
44+
self.openAPIService = openAPILambdaService
5745
try self.openAPIService.register(transport: self.transport)
5846
}
5947

Sources/OpenAPILambdaService.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)