rename LambdaOpenAPITransport to OpenAPILambdaTransport for consistency

This commit is contained in:
Sebastien Stormacq
2023-12-13 20:41:26 -05:00
parent 9784bd4774
commit b2fa4ccf00
9 changed files with 24 additions and 24 deletions
+4 -4
View File
@@ -69,7 +69,7 @@ import OpenAPILambda // <-- 1. import this library
@main // <-- 2. flag this struct as the executable target entrypoint
struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi { // <-- 3. add the OpenAPILambdaHttpApi protocol
init(transport: LambdaOpenAPITransport) throws { // <-- 4. add this constructor (don't remove the call to `registerHandlers(on:)`)
init(transport: OpenAPILambdaTransport) throws { // <-- 4. add this constructor (don't remove the call to `registerHandlers(on:)`)
try self.registerHandlers(on: transport)
}
@@ -247,7 +247,7 @@ import OpenAPILambda
@main
struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
init(transport: LambdaOpenAPITransport) throws {
init(transport: OpenAPILambdaTransport) throws {
try self.registerHandlers(on: transport)
}
@@ -494,7 +494,7 @@ Here is an example using `APIGatewayRequest` and `APIGatewayResponse`:
struct QuoteServiceLambda: OpenAPILambda {
typealias Event = APIGatewayRequest
typealias Output = APIGatewayResponse
public init(transport: LambdaOpenAPITransport) throws {
public init(transport: OpenAPILambdaTransport) throws {
let openAPIHandler = QuoteServiceImpl()
try openAPIHandler.registerHandlers(on: transport)
}
@@ -529,7 +529,7 @@ extension APIGatewayRequest {
/// Return an `HTTPRequest.Method` for this `APIGatewayRequest`
public func httpRequestMethod() throws -> HTTPRequest.Method {
guard let method = HTTPRequest.Method(rawValue: self.httpMethod.rawValue) else {
throw LambdaOpenAPIHttpError.invalidMethod(self.httpMethod.rawValue)
throw OpenAPILambdaHttpError.invalidMethod(self.httpMethod.rawValue)
}
return method
}
@@ -21,7 +21,7 @@ extension APIGatewayV2Request {
/// Return an `HTTPRequest.Method` for this `APIGatewayV2Request`
public func httpRequestMethod() throws -> HTTPRequest.Method {
guard let method = HTTPRequest.Method(rawValue: self.context.http.method.rawValue) else {
throw LambdaOpenAPIHttpError.invalidMethod(self.context.http.method.rawValue)
throw OpenAPILambdaHttpError.invalidMethod(self.context.http.method.rawValue)
}
return method
}
+1 -1
View File
@@ -19,7 +19,7 @@ import OpenAPIRuntime
import HTTPTypes
/// The errors that can be generated
public enum LambdaOpenAPIHttpError: Error {
public enum OpenAPILambdaHttpError: Error {
case invalidMethod(String)
}
+1 -1
View File
@@ -26,7 +26,7 @@ public protocol OpenAPILambda {
/// Initialize application.
///
/// This is where you create your OpenAPI service implementation and register the transport
init(transport: LambdaOpenAPITransport) throws
init(transport: OpenAPILambdaTransport) throws
/// Convert from `Event` type to `OpenAPILambdaRequest`
/// - Parameters:
+3 -3
View File
@@ -33,7 +33,7 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
/// - context: Lambda initialization context
public init(context: LambdaInitializationContext) throws {
self.router = TrieRouter()
self.transport = LambdaOpenAPITransport(router: self.router)
self.transport = OpenAPILambdaTransport(router: self.router)
self.lambda = try .init(transport: self.transport)
}
@@ -67,7 +67,7 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
return lambda.output(from: lambdaResponse)
}
let router: LambdaOpenAPIRouter
let transport: LambdaOpenAPITransport
let router: OpenAPILambdaRouter
let transport: OpenAPILambdaTransport
let lambda: L
}
+4 -4
View File
@@ -35,12 +35,12 @@ public typealias OpenAPIHandler = @Sendable (HTTPRequest, HTTPBody?, ServerReque
)
/// Lambda Transport for OpenAPI generator
public struct LambdaOpenAPITransport: ServerTransport {
public struct OpenAPILambdaTransport: ServerTransport {
private var router: LambdaOpenAPIRouter
private var router: OpenAPILambdaRouter
/// Create a `LambdaOpenAPITransport` with the given `LambdaOpenAPIRouter`
public init(router: LambdaOpenAPIRouter) { self.router = router }
/// Create a `OpenAPILambdaTransport` with the given `OpenAPILambdaRouter`
public init(router: OpenAPILambdaRouter) { self.router = router }
/// Registers an HTTP operation handler at the provided path and method.
/// - Parameters:
+2 -2
View File
@@ -17,14 +17,14 @@ import Foundation
import HTTPTypes
/// Errors returned by the router
public enum LambdaOpenAPIRouterError: Error {
public enum OpenAPILambdaRouterError: Error {
case noRouteForPath(String)
case noHandlerForPath(String)
case noRouteForMethod(HTTPRequest.Method)
}
/// A router API
public protocol LambdaOpenAPIRouter {
public protocol OpenAPILambdaRouter {
/// add a route for a given HTTP method and path and associate a handler
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws
+8 -8
View File
@@ -15,7 +15,7 @@
import HTTPTypes
/// A Trie router implementation
public struct TrieRouter: LambdaOpenAPIRouter {
public struct TrieRouter: OpenAPILambdaRouter {
private let uriPath: URIPathCollection = URIPath()
/// add a route for a given HTTP method and path and associate a handler
@@ -88,16 +88,16 @@ struct URIPath: URIPathCollection {
/// - the OpenAPIHandler for this path
/// - the OpenAI ServerRequestMetadata (a [String:String] with parameter names and their values
/// - Throws:
/// - LambdaOpenAPIRouterError.noRouteForPath when there is no handler in the graph for the given combination of HTTP method and path
/// - LambdaOpenAPIRouterError.noRouteForMethod when there is no handler for that HTTP method
/// - LambdaOpenAPIRouterError.noHandlerForPath when there is no handler as leaf node of the tree. This is a programming error and should not happen
/// - OpenAPILambdaRouterError.noRouteForPath when there is no handler in the graph for the given combination of HTTP method and path
/// - OpenAPILambdaRouterError.noRouteForMethod when there is no handler for that HTTP method
/// - OpenAPILambdaRouterError.noHandlerForPath when there is no handler as leaf node of the tree. This is a programming error and should not happen
func find(method: HTTPRequest.Method, path: String) throws -> (OpenAPIHandler, OpenAPILambdaRequestParameters) {
var parameters: OpenAPILambdaRequestParameters = [:]
let root: Node = root()
// first node is the HTTP Method
guard let nodeHTTP = root.children[method.rawValue] else {
throw LambdaOpenAPIRouterError.noRouteForMethod(method)
throw OpenAPILambdaRouterError.noRouteForMethod(method)
}
// search for each path component. If a component is not found, it might be a parameter
@@ -125,18 +125,18 @@ struct URIPath: URIPathCollection {
currentNode = child
}
else {
throw LambdaOpenAPIRouterError.noRouteForPath(path)
throw OpenAPILambdaRouterError.noRouteForPath(path)
}
}
}
//at this stage, current node must have a handler child
guard let handlerNode = currentNode.handlerChild() else {
throw LambdaOpenAPIRouterError.noHandlerForPath(path)
throw OpenAPILambdaRouterError.noHandlerForPath(path)
}
// did we found an handler ?
guard let handler = handlerNode.value.handler else { throw LambdaOpenAPIRouterError.noHandlerForPath(path) }
guard let handler = handlerNode.value.handler else { throw OpenAPILambdaRouterError.noHandlerForPath(path) }
return (handler, parameters)
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 672 KiB

After

Width:  |  Height:  |  Size: 1.4 MiB