From a99e2d1f5b3488247b3efb301c562f2196deae19 Mon Sep 17 00:00:00 2001 From: Kyle Browning Date: Mon, 30 Sep 2024 08:57:56 -0700 Subject: [PATCH 1/4] Update to use lifecycle --- Package.swift | 2 ++ Sources/APNS/APNSClient.swift | 8 ++++---- Sources/APNS/Coding/APNSJSONDecoder.swift | 2 +- Sources/APNS/Coding/APNSJSONEncoder.swift | 2 +- Sources/APNSCore/APNSClient.swift | 1 - Sources/APNSExample/Program.swift | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Package.swift b/Package.swift index f7eb332d..bedc0bf1 100644 --- a/Package.swift +++ b/Package.swift @@ -24,6 +24,7 @@ let package = Package( .package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"), .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.6.0"), .package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.9.0"), + .package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"), ], targets: [ .executableTarget( @@ -52,6 +53,7 @@ let package = Package( dependencies: [ .product(name: "Crypto", package: "swift-crypto"), .product(name: "AsyncHTTPClient", package: "async-http-client"), + .product(name: "ServiceLifecycle", package: "swift-service-lifecycle"), .target(name: "APNSCore"), ] ), diff --git a/Sources/APNS/APNSClient.swift b/Sources/APNS/APNSClient.swift index b9a7dcc7..db2e675f 100644 --- a/Sources/APNS/APNSClient.swift +++ b/Sources/APNS/APNSClient.swift @@ -22,9 +22,10 @@ import NIOHTTP1 import NIOSSL import NIOTLS import NIOPosix +import ServiceLifecycle /// A client to talk with the Apple Push Notification services. -public final class APNSClient: APNSClientProtocol { +public final class APNSClient: Service, APNSClientProtocol { /// The configuration used by the ``APNSClient``. private let configuration: APNSClientConfiguration @@ -112,9 +113,8 @@ public final class APNSClient(_ type: T.Type, from buffer: ByteBuffer) throws -> T } diff --git a/Sources/APNS/Coding/APNSJSONEncoder.swift b/Sources/APNS/Coding/APNSJSONEncoder.swift index 1f04a333..5a719904 100644 --- a/Sources/APNS/Coding/APNSJSONEncoder.swift +++ b/Sources/APNS/Coding/APNSJSONEncoder.swift @@ -18,7 +18,7 @@ import NIOFoundationCompat /// A protocol that is similar to the `JSONEncoder`. This allows users of APNSwift to customize the encoder used /// for encoding the notification JSON payloads. -public protocol APNSJSONEncoder { +public protocol APNSJSONEncoder: Sendable { func encode(_ value: T, into buffer: inout ByteBuffer) throws } diff --git a/Sources/APNSCore/APNSClient.swift b/Sources/APNSCore/APNSClient.swift index ee11f37f..8c1b5a2e 100644 --- a/Sources/APNSCore/APNSClient.swift +++ b/Sources/APNSCore/APNSClient.swift @@ -14,5 +14,4 @@ public protocol APNSClientProtocol { func send(_ request: APNSRequest) async throws -> APNSResponse - func shutdown() async throws } diff --git a/Sources/APNSExample/Program.swift b/Sources/APNSExample/Program.swift index bc5dc09e..224e6b97 100644 --- a/Sources/APNSExample/Program.swift +++ b/Sources/APNSExample/Program.swift @@ -64,7 +64,7 @@ struct Main { logger.warning("error sending push: \(error)") } - try? await client.shutdown() + try await client.run() } } From 60686dd68b11b2230b7886003c45a60cd5a799b1 Mon Sep 17 00:00:00 2001 From: Kyle Browning Date: Mon, 30 Sep 2024 09:00:21 -0700 Subject: [PATCH 2/4] await gracefullShutdown --- Sources/APNS/APNSClient.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/APNS/APNSClient.swift b/Sources/APNS/APNSClient.swift index db2e675f..3e57f8d8 100644 --- a/Sources/APNS/APNSClient.swift +++ b/Sources/APNS/APNSClient.swift @@ -115,6 +115,7 @@ public final class APNSClient Date: Mon, 30 Sep 2024 13:41:23 -0700 Subject: [PATCH 3/4] pr feedback --- Sources/APNSExample/Program.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/APNSExample/Program.swift b/Sources/APNSExample/Program.swift index 224e6b97..edfd0a43 100644 --- a/Sources/APNSExample/Program.swift +++ b/Sources/APNSExample/Program.swift @@ -49,6 +49,8 @@ struct Main { responseDecoder: JSONDecoder(), requestEncoder: JSONEncoder() ) + + try await client.run() do { try await Self.sendSimpleAlert(with: client) @@ -64,7 +66,6 @@ struct Main { logger.warning("error sending push: \(error)") } - try await client.run() } } From 5c1f64fc6cbc9661ee01766c161a00581c5b43d1 Mon Sep 17 00:00:00 2001 From: Kyle Browning Date: Mon, 30 Sep 2024 14:24:42 -0700 Subject: [PATCH 4/4] update pr --- Sources/APNSExample/Program.swift | 4 +-- Tests/APNSTests/APNSClientTests.swift | 52 --------------------------- 2 files changed, 2 insertions(+), 54 deletions(-) delete mode 100644 Tests/APNSTests/APNSClientTests.swift diff --git a/Sources/APNSExample/Program.swift b/Sources/APNSExample/Program.swift index edfd0a43..2a815f51 100644 --- a/Sources/APNSExample/Program.swift +++ b/Sources/APNSExample/Program.swift @@ -49,8 +49,6 @@ struct Main { responseDecoder: JSONDecoder(), requestEncoder: JSONEncoder() ) - - try await client.run() do { try await Self.sendSimpleAlert(with: client) @@ -66,6 +64,8 @@ struct Main { logger.warning("error sending push: \(error)") } + try await client.run() + } } diff --git a/Tests/APNSTests/APNSClientTests.swift b/Tests/APNSTests/APNSClientTests.swift deleted file mode 100644 index d04679ca..00000000 --- a/Tests/APNSTests/APNSClientTests.swift +++ /dev/null @@ -1,52 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the APNSwift open source project -// -// Copyright (c) 2022 the APNSwift project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of APNSwift project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// - -@testable import APNSCore -import APNS -import Crypto -import XCTest - -final class APNSClientTests: XCTestCase { - func testShutdown() async throws { - let client = self.makeClient() - try await client.shutdown() - } - - // MARK: - Helper methods - - private func makeClient() -> APNSClient { - APNSClient( - configuration: .init( - authenticationMethod: .jwt( - privateKey: try! P256.Signing.PrivateKey(pemRepresentation: self.jwtPrivateKey), - keyIdentifier: "MY_KEY_ID", - teamIdentifier: "MY_TEAM_ID" - ), - environment: .development - ), - eventLoopGroupProvider: .createNew, - responseDecoder: JSONDecoder(), - requestEncoder: JSONEncoder() - ) - } - - private let jwtPrivateKey = """ - -----BEGIN PRIVATE KEY----- - MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg2sD+kukkA8GZUpmm - jRa4fJ9Xa/JnIG4Hpi7tNO66+OGgCgYIKoZIzj0DAQehRANCAATZp0yt0btpR9kf - ntp4oUUzTV0+eTELXxJxFvhnqmgwGAm1iVW132XLrdRG/ntlbQ1yzUuJkHtYBNve - y+77Vzsd - -----END PRIVATE KEY----- - """ -}