|
7 | 7 |
|
8 | 8 | 
|
9 | 9 |
|
| 10 | +## Usage |
| 11 | + |
| 12 | +Add the package `BreezeLambdaWebHook` to a package: |
| 13 | + |
| 14 | +```swift |
| 15 | +// swift-tools-version:5.7 |
| 16 | +// The swift-tools-version declares the minimum version of Swift required to build this package. |
| 17 | + |
| 18 | +import PackageDescription |
| 19 | + |
| 20 | +let package = Package( |
| 21 | + name: "BreezeWebHook", |
| 22 | + platforms: [ |
| 23 | + .macOS(.v13), |
| 24 | + ], |
| 25 | + products: [ |
| 26 | + .executable(name: "WebHook", targets: ["WebHook"]), |
| 27 | + ], |
| 28 | + dependencies: [ |
| 29 | + .package(url: "https://github.com/swift-sprinter/BreezeLambdaWebHook.git", from: "0.4.0") |
| 30 | + ], |
| 31 | + targets: [ |
| 32 | + .executableTarget( |
| 33 | + name: "WebHook", |
| 34 | + dependencies: [ |
| 35 | + .product(name: "BreezeLambdaWebHook", package: "Breeze"), |
| 36 | + ] |
| 37 | + ) |
| 38 | + ] |
| 39 | +) |
| 40 | +``` |
| 41 | + |
| 42 | +Add the implementation to the Lambda: |
| 43 | + |
| 44 | +```swift |
| 45 | +import Foundation |
| 46 | +import AWSLambdaEvents |
| 47 | +import AWSLambdaRuntimeCore |
| 48 | +import BreezeLambdaWebHook |
| 49 | +import Foundation |
| 50 | + |
| 51 | +enum WebHookError: Error { |
| 52 | + case invalidHandler |
| 53 | +} |
| 54 | + |
| 55 | +class WebHook: BreezeLambdaWebHookHandler { |
| 56 | + |
| 57 | + let handlerContext: HandlerContext |
| 58 | + |
| 59 | + required init(handlerContext: HandlerContext) { |
| 60 | + self.handlerContext = handlerContext |
| 61 | + } |
| 62 | + |
| 63 | + func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: AWSLambdaEvents.APIGatewayV2Request) async -> AWSLambdaEvents.APIGatewayV2Response { |
| 64 | + do { |
| 65 | + // Check the handler |
| 66 | + guard let handler = handlerContext.handler else { |
| 67 | + throw WebHookError.invalidHandler |
| 68 | + } |
| 69 | + // Evaluate the event |
| 70 | + context.logger.info("event: \(event)") |
| 71 | + let incomingRequest: ... = try event.bodyObject() |
| 72 | + // Implement the business logic |
| 73 | + let body = ... |
| 74 | + // Return an APIGatewayV2Response |
| 75 | + return APIGatewayV2Response(with: body, statusCode: .ok) |
| 76 | + } catch { |
| 77 | + // Return an APIGatewayV2Response in case of error |
| 78 | + return APIGatewayV2Response(with: error, statusCode: .badRequest) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +Add the Lambda runtime to the file `swift.main` |
| 85 | +``` |
| 86 | +import Foundation |
| 87 | +import AWSLambdaEvents |
| 88 | +import AWSLambdaRuntimeCore |
| 89 | +import BreezeLambdaWebHook |
| 90 | +
|
| 91 | +BreezeLambdaWebHook<WebHook>.main() |
| 92 | +``` |
| 93 | + |
| 94 | +## Documentation |
| 95 | + |
| 96 | +Refer to the main project https://github.com/swift-serverless/Breeze for more info and working examples. |
| 97 | + |
10 | 98 | ## Contributing
|
11 | 99 |
|
12 | 100 | Contributions are welcome! If you encounter any issues or have ideas for improvements, please open an issue or submit a pull request.
|
|
0 commit comments