BreezeLambdaWebHook

main

Adapt the Swift AWS Lambda Runtime to interact with AWS APIGateway V2
swift-serverless/BreezeLambdaWebHook

BreezeLambdaWebHook

Breeze CI codecov

security status stability status licensing status

Breeze

Usage

Add the package dependency BreezeLambdaWebHook to a package:

// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "BreezeWebHook",
    platforms: [
        .macOS(.v13),
    ],
    products: [
        .executable(name: "WebHook", targets: ["WebHook"]),
    ],
    dependencies: [
        .package(url: "https://github.com/swift-sprinter/BreezeLambdaWebHook.git", from: "0.4.0")
    ],
    targets: [
        .executableTarget(
            name: "WebHook",
             dependencies: [
                .product(name: "BreezeLambdaWebHook", package: "Breeze"),
            ]
        )
    ]
)

Add the implementation to the Lambda:

import Foundation
import AWSLambdaEvents
import AWSLambdaRuntimeCore
import BreezeLambdaWebHook
import Foundation

enum WebHookError: Error {
    case invalidHandler
}

class WebHook: BreezeLambdaWebHookHandler {
    
    let handlerContext: HandlerContext
    
    required init(handlerContext: HandlerContext) {
        self.handlerContext = handlerContext
    }
    
    func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: AWSLambdaEvents.APIGatewayV2Request) async -> AWSLambdaEvents.APIGatewayV2Response {
        do {
            // Check the handler
            guard let handler = handlerContext.handler else {
                throw  WebHookError.invalidHandler
            }
            // Evaluate the event
            context.logger.info("event: \(event)")
            let incomingRequest: ... = try event.bodyObject()
            // Implement the business logic
            let body = ...
            // Return an APIGatewayV2Response
            return APIGatewayV2Response(with: body, statusCode: .ok)
        } catch {
            // Return an APIGatewayV2Response in case of error
            return APIGatewayV2Response(with: error, statusCode: .badRequest)
        }
    }
}

Add the Lambda runtime to the file swift.main

import Foundation
import AWSLambdaEvents
import AWSLambdaRuntimeCore
import BreezeLambdaWebHook

BreezeLambdaWebHook<WebHook>.main()

Documentation

Refer to the main project https://github.com/swift-serverless/Breeze for more info and working examples.

Contributing

Contributions are welcome! If you encounter any issues or have ideas for improvements, please open an issue or submit a pull request.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Sun May 05 2024 04:27:58 GMT-0900 (Hawaii-Aleutian Daylight Time)