BreezeLambdaWebHook

Breeze CI codecov

security status stability status licensing status

Breeze

用法

添加包依赖 BreezeLambdaWebHook 到一个包

// 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"),
            ]
        )
    ]
)

将实现添加到 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)
        }
    }
}

将 Lambda 运行时添加到文件 swift.main

import Foundation
import AWSLambdaEvents
import AWSLambdaRuntimeCore
import BreezeLambdaWebHook

BreezeLambdaWebHook<WebHook>.main()

文档

请参考主项目 https://github.com/swift-serverless/Breeze 获取更多信息和工作示例。

贡献

欢迎贡献!如果您遇到任何问题或有改进的想法,请打开 issue 或提交 pull request。