Skip to content

Commit 5a12994

Browse files
author
Andrea Scuderi
committed
Fix WebHook with Swift6
1 parent 7ba793f commit 5a12994

15 files changed

+255
-111
lines changed

Package.swift

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version: 6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

6+
#if os(macOS)
7+
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15), .iOS(.v13)]
8+
#else
9+
let platforms: [PackageDescription.SupportedPlatform]? = nil
10+
#endif
11+
612
let package = Package(
713
name: "BreezeLambdaWebHook",
8-
platforms: [
9-
.macOS(.v13),
10-
.iOS(.v15)
11-
],
14+
platforms: platforms,
1215
products: [
1316
.library(
14-
name: "BreezeLambdaWebHook",
15-
targets: ["BreezeLambdaWebHook"]
17+
name: "BreezeLambdaWebHookService",
18+
targets: ["BreezeLambdaWebHookService"]
19+
),
20+
.library(
21+
name: "BreezeHTTPClientService",
22+
targets: ["BreezeHTTPClientService"]
23+
),
24+
.executable(
25+
name: "BreezeDemoHTTPApplication",
26+
targets: ["BreezeDemoHTTPApplication"]
1627
)
1728
],
1829
dependencies: [
19-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.2"),
20-
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "0.1.0"),
30+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
31+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "0.5.0"),
2132
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.11.2"),
33+
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.2"),
2234
],
2335
targets: [
2436
.target(
25-
name: "BreezeLambdaWebHook",
37+
name: "BreezeLambdaWebHookService",
2638
dependencies: [
39+
"BreezeHTTPClientService",
2740
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
2841
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
2942
.product(name: "AsyncHTTPClient", package: "async-http-client"),
3043
]
3144
),
45+
.target(
46+
name: "BreezeHTTPClientService",
47+
dependencies: [
48+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
49+
.product(name: "Logging", package: "swift-log")
50+
]
51+
),
52+
.executableTarget(
53+
name: "BreezeDemoHTTPApplication",
54+
dependencies: [
55+
"BreezeLambdaWebHookService"
56+
]
57+
),
3258
.testTarget(
3359
name: "BreezeLambdaWebHookTests",
3460
dependencies: [
35-
.product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"),
36-
"BreezeLambdaWebHook"
61+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
62+
"BreezeLambdaWebHookService"
3763
],
3864
resources: [.copy("Fixtures")]
3965
),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import BreezeLambdaWebHookService
16+
import BreezeHTTPClientService
17+
import AWSLambdaEvents
18+
import AWSLambdaRuntime
19+
import Logging
20+
21+
struct DemoLambdaHandler: BreezeLambdaWebHookHandler, Sendable {
22+
var handlerContext: HandlerContext
23+
24+
init(handlerContext: HandlerContext) {
25+
self.handlerContext = handlerContext
26+
}
27+
28+
func handle(_ event: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response {
29+
context.logger.info("Received event: \(event)")
30+
return APIGatewayV2Response(with: "Hello World", statusCode: .ok)
31+
}
32+
}
33+
34+
@main
35+
struct BreezeDemoApplication {
36+
static func main() async throws {
37+
do {
38+
let logger = Logger(label: "BreezeDemoApplication")
39+
let httpClientService = BreezeHTTPClientService(
40+
timeout: .seconds(60),
41+
logger: logger
42+
)
43+
let lambdaService = BreezeLambdaWebHookService<DemoLambdaHandler>.init(
44+
serviceConfig: BreezeClientServiceConfig(
45+
httpClientService: httpClientService,
46+
logger: logger
47+
)
48+
)
49+
try await lambdaService.run()
50+
} catch {
51+
print(error.localizedDescription)
52+
}
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Logging
16+
17+
public enum BreezeClientServiceError: Error {
18+
case invalidHttpClient
19+
}
20+
21+
public struct BreezeClientServiceConfig: Sendable {
22+
23+
public let httpClientService: BreezeHTTPClientServing
24+
public let logger: Logger
25+
26+
public init(
27+
httpClientService: BreezeHTTPClientServing,
28+
logger: Logger
29+
) {
30+
self.httpClientService = httpClientService
31+
self.logger = logger
32+
}
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import ServiceLifecycle
16+
import AsyncHTTPClient
17+
import NIOCore
18+
import Logging
19+
20+
public protocol BreezeHTTPClientServing: Actor, Service {
21+
var httpClient: HTTPClient { get }
22+
}
23+
24+
public actor BreezeHTTPClientService: BreezeHTTPClientServing {
25+
26+
public let httpClient: HTTPClient
27+
let logger: Logger
28+
29+
public init(timeout: TimeAmount, logger: Logger) {
30+
self.logger = logger
31+
let timeout = HTTPClient.Configuration.Timeout(
32+
connect: timeout,
33+
read: timeout
34+
)
35+
let configuration = HTTPClient.Configuration(timeout: timeout)
36+
self.httpClient = HTTPClient(
37+
eventLoopGroupProvider: .singleton,
38+
configuration: configuration
39+
)
40+
logger.info("HTTPClientService config:")
41+
logger.info("timeout \(timeout)")
42+
}
43+
44+
public func run() async throws {
45+
logger.info("HTTPClientService started...")
46+
try await gracefulShutdown()
47+
48+
logger.info("Stopping HTTPClientService...")
49+
try await httpClient.shutdown()
50+
logger.info("HTTPClientService shutdown completed.")
51+
}
52+
53+
deinit {
54+
try? httpClient.syncShutdown()
55+
}
56+
}

Sources/BreezeLambdaWebHook/BreezeLambdaWebHook.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.

Sources/BreezeLambdaWebHook/APIGatewayV2Response+Extensions.swift renamed to Sources/BreezeLambdaWebHookService/APIGatewayV2Response+Extensions.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import class Foundation.JSONEncoder
1818

1919
public extension APIGatewayV2Response {
2020
private static let encoder = JSONEncoder()
21-
22-
/// defaultHeaders
23-
/// Override the headers in APIGatewayV2Response
24-
static var defaultHeaders = [ "Content-Type": "application/json" ]
2521

2622
struct BodyError: Codable {
2723
public let error: String
@@ -40,14 +36,19 @@ public extension APIGatewayV2Response {
4036
/// - Parameters:
4137
/// - object: Encodable Object
4238
/// - statusCode: HTTP Status Code
43-
init<Output: Encodable>(with object: Output, statusCode: HTTPResponse.Status) {
39+
/// - headers: HTTP Headers
40+
init<Output: Encodable>(
41+
with object: Output,
42+
statusCode: HTTPResponse.Status,
43+
headers: [String: String] = [ "Content-Type": "application/json" ]
44+
) {
4445
var body = "{}"
4546
if let data = try? Self.encoder.encode(object) {
4647
body = String(data: data, encoding: .utf8) ?? body
4748
}
4849
self.init(
4950
statusCode: statusCode,
50-
headers: APIGatewayV2Response.defaultHeaders,
51+
headers: headers,
5152
body: body,
5253
isBase64Encoded: false
5354
)

Sources/BreezeLambdaWebHook/BreezeLambdaWebHookHandler.swift renamed to Sources/BreezeLambdaWebHookService/BreezeLambdaWebHookHandler.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
import AsyncHTTPClient
1616
import AWSLambdaEvents
1717
import AWSLambdaRuntime
18-
import AWSLambdaRuntimeCore
1918
import Foundation
2019

21-
public protocol BreezeLambdaWebHookHandler {
20+
public protocol BreezeLambdaWebHookHandler: LambdaHandler {
2221
var handlerContext: HandlerContext { get }
2322
init(handlerContext: HandlerContext)
24-
func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response
23+
func handle(_ event: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response
2524
}
2625

2726
public extension BreezeLambdaWebHookHandler {

0 commit comments

Comments
 (0)