|
| 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 AWSLambdaEvents |
| 16 | +import Foundation |
| 17 | +import HTTPTypes |
| 18 | +import OpenAPIRuntime |
| 19 | + |
| 20 | + |
| 21 | +// TODO: add more method to simplify usage |
| 22 | + |
| 23 | +// Current API : |
| 24 | + |
| 25 | +// try router.add( |
| 26 | +// method: .get, |
| 27 | +// path: "/health" |
| 28 | +// ) { (request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse,HTTPBody?) in |
| 29 | +// return (.init(status: .ok), .init("OK")) |
| 30 | +// } |
| 31 | + |
| 32 | +// should be |
| 33 | + |
| 34 | +// try router.get("/health") { _ in |
| 35 | +// "OK" |
| 36 | +// } |
| 37 | + |
| 38 | +extension OpenAPILambdaRouter { |
| 39 | + |
| 40 | + /// Adds a GET route to the router for the given path. |
| 41 | + /// |
| 42 | + /// The given handler retruns a String that will be converted to the correct response type. |
| 43 | + /// It will return an HTTP 200 response or HTTP 500 if your handler throws and error |
| 44 | + /// - Parameters: |
| 45 | + /// - path: The path for the route. |
| 46 | + /// - handler: The handler to be executed for the route. |
| 47 | + /// - Throws: An error if the route cannot be added. |
| 48 | + public func get( |
| 49 | + _ path: String, |
| 50 | + handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> String |
| 51 | + ) throws { |
| 52 | + |
| 53 | + let openAPIHandler: OpenAPIHandler = { |
| 54 | + (request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse, HTTPBody?) in |
| 55 | + |
| 56 | + do { |
| 57 | + let response = try await handler(request, body) |
| 58 | + return (.init(status: .ok), .init(response)) |
| 59 | + } |
| 60 | + catch { |
| 61 | + return (.init(status: .internalServerError), nil) |
| 62 | + } |
| 63 | + } |
| 64 | + try add(method: .get, path: path, handler: openAPIHandler) |
| 65 | + } |
| 66 | + |
| 67 | + /// Adds a POST route to the router for the given path. |
| 68 | + /// |
| 69 | + /// The given handler retruns a String that will be converted to the correct response type. |
| 70 | + /// It will return an HTTP 200 response or HTTP 500 if your handler throws and error |
| 71 | + /// - Parameters: |
| 72 | + /// - path: The path for the route. |
| 73 | + /// - handler: The handler to be executed for the route. |
| 74 | + /// - Throws: An error if the route cannot be added. |
| 75 | + public func post( |
| 76 | + _ path: String, |
| 77 | + handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> String |
| 78 | + ) throws { |
| 79 | + |
| 80 | + let openAPIHandler: OpenAPIHandler = { |
| 81 | + (request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse, HTTPBody?) in |
| 82 | + do { |
| 83 | + let response = try await handler(request, body) |
| 84 | + return (.init(status: .ok), .init(response)) |
| 85 | + } |
| 86 | + catch { |
| 87 | + return (.init(status: .internalServerError), nil) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + try add(method: .post, path: path, handler: openAPIHandler) |
| 92 | + } |
| 93 | +} |
0 commit comments