Giter Site home page Giter Site logo

swift-serverless / breezelambdawebhook Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 256 KB

Adapt the Swift AWS Lambda Runtime to interact with AWS APIGateway V2

License: Apache License 2.0

Makefile 3.45% Swift 96.13% Dockerfile 0.42%
apigateway apigateway-lambda aws-lambda serverside-swift swift serverless-swift

breezelambdawebhook's Introduction

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.

breezelambdawebhook's People

Contributors

andrea-scuderi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.