Skip to content

Commit 4d2c0ed

Browse files
authored
Cleanup logger usage, rename APNSwiftClient and use custom httpclient (#132)
* Cleanup logger usage, rename APNSwiftClient and use custom httpclient * fix readme * One More * Shutdown * Cleanup token factory * make signer an actor * Renames * updte readme * Update Copyright and contributers list * Add documentation * update * fixed ELG and formatted * cleanup * updates * add more documentation
1 parent 2f7c21c commit 4d2c0ed

21 files changed

+536
-539
lines changed

CONTRIBUTORS.txt

+6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ needs to be listed here.
1212
- Craig Newell <[email protected]>
1313
- Eduardo Perez <[email protected]>
1414
- Florian Reinhart <[email protected]>
15+
- Geoff Verdouw <[email protected]>
1516
- Jeffrey Macko <[email protected]>
17+
1618
- Kyle Browning <[email protected]>
1719
- Laurent Gaches <[email protected]>
20+
- Lukáš Petr <[email protected]>
1821
- Mads Odgaard <[email protected]>
22+
- Nikola Paunović <[email protected]>
1923
- Roderic Campbell <[email protected]>
24+
- Simon Kempendorf <[email protected]>
2025
2126
- Tanner Nelson <[email protected]>
27+
- Timothy Ellis <[email protected]>
2228
2329
- itcohorts <[email protected]>
2430
- tanner0101 <[email protected]>

Package.swift

+18-12
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ let package = Package(
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
16-
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
16+
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0"..<"3.0.0"),
1717
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.10.0"),
1818
],
1919
targets: [
20-
.executableTarget(name: "APNSwiftExample", dependencies: [
21-
.target(name: "APNSwift"),
22-
]),
23-
.testTarget(name: "APNSwiftTests", dependencies: [
24-
.target(name: "APNSwift"),
25-
]),
26-
.target(name: "APNSwift", dependencies: [
27-
.product(name: "Logging", package: "swift-log"),
28-
.product(name: "Crypto", package: "swift-crypto"),
29-
.product(name: "AsyncHTTPClient", package: "async-http-client")
30-
]),
20+
.executableTarget(
21+
name: "APNSwiftExample",
22+
dependencies: [
23+
.target(name: "APNSwift")
24+
]),
25+
.testTarget(
26+
name: "APNSwiftTests",
27+
dependencies: [
28+
.target(name: "APNSwift")
29+
]),
30+
.target(
31+
name: "APNSwift",
32+
dependencies: [
33+
.product(name: "Logging", package: "swift-log"),
34+
.product(name: "Crypto", package: "swift-crypto"),
35+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
36+
]),
3137
]
3238
)

README.md

+34-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[![sswg:incubating|94x20](https://img.shields.io/badge/sswg-incubating-yellow.svg)](https://github.com/swift-server/sswg/blob/master/process/incubation.md#sandbox-level)
22
[![License](https://img.shields.io/badge/License-Apache%202.0-yellow.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
33
[![Build](https://github.com/kylebrowning/APNSwift/workflows/test/badge.svg)](https://github.com/kylebrowning/APNSwift/actions)
4-
[![Swift](https://img.shields.io/badge/Swift-5.2-brightgreen.svg?colorA=orange&colorB=4E4E4E)](https://swift.org)
4+
[![Swift](https://img.shields.io/badge/Swift-5.6-brightgreen.svg?colorA=orange&colorB=4E4E4E)](https://swift.org)
5+
[![Documentation](https://img.shields.io/badge/documentation-blueviolet.svg)](https://swiftpackageindex.com/swift-server-community/APNSwift/master/documentation/apnswift)
6+
57

68
# APNSwift
79

@@ -20,117 +22,113 @@ dependencies: [
2022
## Getting Started
2123

2224
```swift
23-
struct BasicNotification: APNSwiftNotification {
24-
let aps: APNSwiftPayload
25+
struct BasicNotification: APNSNotification {
26+
let aps: APNSPayload
2527
}
2628

27-
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
2829
var logger = Logger(label: "com.apnswift")
2930
logger.logLevel = .debug
3031

31-
/// Create your HTTPClient (or pass in one you already have)
32-
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(group))
32+
/// Create your `APNSConfiguration.Authentication`
3333

34-
/// Create your `APNSwiftConfiguration.Authentication`
35-
let authenticationConfig: APNSwiftConfiguration.Authentication = .init(
34+
let authenticationConfig: APNSConfiguration.Authentication = .init(
3635
privateKey: try .loadFrom(filePath: "/Users/kylebrowning/Documents/AuthKey_9UC9ZLQ8YW.p8"),
3736
teamIdentifier: "ABBM6U9RM5",
3837
keyIdentifier: "9UC9ZLQ8YW"
3938
)
4039

41-
/// If you need to use an a secrets manager instead of read from disk, use
40+
/// If you need to use a secrets manager instead of reading from the disk, use
4241
/// `loadfrom(string:)`
4342

44-
let apnsConfig = try APNSwiftConfiguration(
43+
let apnsConfig = try APNSConfiguration(
4544
authenticationConfig: authenticationConfig,
4645
topic: "com.grasscove.Fern",
4746
environment: .sandbox,
4847
logger: logger
4948
)
50-
let apns = APNSwiftConnection(configuration: apnsConfig, logger: logger)
49+
let apns = APNSClient(configuration: apnsConfig)
5150

52-
let aps = APNSwiftPayload(alert: .init(title: "Hey There", subtitle: "Subtitle", body: "Body"), hasContentAvailable: true)
51+
let aps = APNSPayload(alert: .init(title: "Hey There", subtitle: "Subtitle", body: "Body"), hasContentAvailable: true)
5352
let deviceToken = "myDeviceToken"
5453
try await apns.send(notification, pushType: .alert, to: deviceToken)
5554
try await httpClient.shutdown()
56-
try! group.syncShutdownGracefully()
5755
exit(0)
5856
```
5957

60-
### APNSwiftConfiguration
58+
### APNSConfiguration
6159

62-
[`APNSwiftConfiguration`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSwiftConfiguration.swift) is a structure that provides the system with common configuration.
60+
[`APNSConfiguration`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSConfiguration.swift) is a structure that provides the system with common configuration.
6361

6462
```swift
65-
let apnsConfig = try APNSwiftConfiguration(
63+
let apnsConfig = try APNSConfiguration(
6664
authenticationConfig: authenticationConfig,
6765
topic: "com.grasscove.Fern",
6866
environment: .sandbox,
6967
logger: logger
7068
)
7169
```
7270

73-
#### APNSwiftConfiguration.Authentication
74-
[`APNSwiftConfiguration.Authentication`](https://github.com/swift-server-community/APNSwift/blob/master/Sources/APNSwift/APNSwiftConfiguration.swift#L26) is a struct that provides authentication keys and metadata to the signer.
71+
#### APNSConfiguration.Authentication
72+
[`APNSConfiguration.Authentication`](https://github.com/swift-server-community/APNSwift/blob/master/Sources/APNSwift/APNSConfiguration.swift#L26) is a struct that provides authentication keys and metadata to the signer.
7573

7674

7775
```swift
78-
let authenticationConfig: APNSwiftConfiguration.Authentication = .init(
76+
let authenticationConfig: APNSConfiguration.Authentication = .init(
7977
privateKey: try .loadFrom(filePath: "/Users/kylebrowning/Documents/AuthKey_9UC9ZLQ8YW.p8"),
8078
teamIdentifier: "ABBM6U9RM5",
8179
keyIdentifier: "9UC9ZLQ8YW"
8280
)
8381
```
8482

85-
### APNSwiftConnection
83+
### APNSClient
8684

87-
[`APNSwiftConnection`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSwiftConnection.swift) provides functions to send a notification to a specific device token string.
85+
[`APNSClient`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSClient.swift) provides functions to send a notification to a specific device token string.
8886

8987

90-
#### Example `APNSwiftConnection`
88+
#### Example `APNSClient`
9189
```swift
92-
let apns = APNSwiftConnection(configuration: apnsConfig, logger: logger)
90+
let apns = APNSClient(configuration: apnsConfig)
9391
```
9492

95-
### APNSwiftAlert
93+
### APNSAlert
9694

97-
[`APNSwiftAlert`](https://github.com/kylebrowning/APNSwift/blob/tn-concise-naming/Sources/APNSwift/APNSwiftAlert.swift) is the actual meta data of the push notification alert someone wishes to send. More details on the specifics of each property are provided [here](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html). They follow a 1-1 naming scheme listed in Apple's documentation
95+
[`APNSAlert`](https://github.com/kylebrowning/APNSwift/blob/tn-concise-naming/Sources/APNSwift/APNSAlert.swift) is the actual meta data of the push notification alert someone wishes to send. More details on the specifics of each property are provided [here](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html). They follow a 1-1 naming scheme listed in Apple's documentation
9896

9997

100-
#### Example `APNSwiftAlert`
98+
#### Example `APNSAlert`
10199
```swift
102-
let alert = APNSwiftAlert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")
100+
let alert = APNSAlert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")
103101
```
104102

105-
### APNSwiftPayload
103+
### APNSPayload
106104

107-
[`APNSwiftPayload`](https://github.com/kylebrowning/APNSwift/blob/tn-concise-naming/Sources/APNSwift/APNSwiftPayload.swift) is the meta data of the push notification. Things like the alert, badge count. More details on the specifics of each property are provided [here](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html). They follow a 1-1 naming scheme listed in Apple's documentation
105+
[`APNSPayload`](https://github.com/kylebrowning/APNSwift/blob/tn-concise-naming/Sources/APNSwift/APNSPayload.swift) is the meta data of the push notification. Things like the alert, badge count. More details on the specifics of each property are provided [here](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html). They follow a 1-1 naming scheme listed in Apple's documentation
108106

109107

110-
#### Example `APNSwiftPayload`
108+
#### Example `APNSPayload`
111109
```swift
112110
let alert = ...
113-
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
111+
let aps = APNSPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
114112
```
115113

116114
### Custom Notification Data
117115

118-
Apple provides engineers with the ability to add custom payload data to each notification. In order to facilitate this we have the `APNSwiftNotification`.
116+
Apple provides engineers with the ability to add custom payload data to each notification. In order to facilitate this we have the `APNSNotification`.
119117

120118
#### Example
121119
```swift
122120
struct AcmeNotification: APNSwiftNotification {
123121
let acme2: [String]
124-
let aps: APNSwiftPayload
122+
let aps: APNSPayload
125123

126-
init(acme2: [String], aps: APNSwiftPayload) {
124+
init(acme2: [String], aps: APNSPayload) {
127125
self.acme2 = acme2
128126
self.aps = aps
129127
}
130128
}
131129

132-
let apns: APNSwiftConnection: = ...
133-
let aps: APNSwiftPayload = ...
130+
let apns: APNSClient: = ...
131+
let aps: APNSPayload = ...
134132
let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps)
135133
let res = try apns.send(notification, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e")
136134
```

Sources/APNSwift/APNSwiftAlert.swift renamed to Sources/APNSwift/APNSAlert.swift

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
/// This structure provides the data structure for an APNS Alert
16-
public struct APNSwiftAlert: Codable {
16+
public struct APNSAlert: Codable {
1717
public let title: String?
1818
public let subtitle: String?
1919
public let body: String?
@@ -49,9 +49,15 @@ public struct APNSwiftAlert: Codable {
4949
````
5050
*/
5151
public init(
52-
title: String? = nil, subtitle: String? = nil, body: String? = nil,
53-
titleLocKey: String? = nil, titleLocArgs: [String]? = nil, actionLocKey: String? = nil,
54-
locKey: String? = nil, locArgs: [String]? = nil, launchImage: String? = nil
52+
title: String? = nil,
53+
subtitle: String? = nil,
54+
body: String? = nil,
55+
titleLocKey: String? = nil,
56+
titleLocArgs: [String]? = nil,
57+
actionLocKey: String? = nil,
58+
locKey: String? = nil,
59+
locArgs: [String]? = nil,
60+
launchImage: String? = nil
5561
) {
5662
self.title = title
5763
self.subtitle = subtitle

Sources/APNSwift/APNSwiftBearerTokenFactory.swift renamed to Sources/APNSwift/APNSBearerTokenFactory.swift

+15-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the APNSwift open source project
44
//
5-
// Copyright (c) 2019 the APNSwift project authors
5+
// Copyright (c) 2022 the APNSwift project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -16,51 +16,46 @@ import Crypto
1616
import Logging
1717
import NIOCore
1818

19-
internal final actor APNSwiftBearerTokenFactory {
19+
internal final actor APNSBearerTokenFactory {
2020

2121
private var cachedBearerToken: String?
22-
internal var currentBearerToken: String? {
2322

24-
guard !isTokenStale, let cachedBearerToken = cachedBearerToken else {
25-
do {
26-
tokenCreated = NIODeadline.now()
27-
let newToken = try makeNewBearerToken()
28-
cachedBearerToken = newToken
29-
return newToken
30-
} catch {
23+
internal func getCurrentBearerToken() async throws -> String {
3124

32-
logger?.error("Failed to generate token: \(error)")
33-
return nil
34-
}
25+
guard !isTokenStale, let cachedBearerToken = cachedBearerToken else {
26+
tokenCreated = NIODeadline.now()
27+
let newToken = try await makeNewBearerToken()
28+
cachedBearerToken = newToken
29+
return newToken
3530
}
3631

37-
logger?.debug("returning cached token \(cachedBearerToken.prefix(8))...")
32+
logger?.debug("APNS cached token \(cachedBearerToken.prefix(8))...")
3833
return cachedBearerToken
3934
}
4035

4136
private var isTokenStale: Bool {
4237
NIODeadline.now() - tokenCreated > TimeAmount.minutes(55)
4338
}
4439

45-
private let signer: APNSwiftSigner
40+
private let signer: APNSSigner
4641
private let logger: Logger?
4742
private var tokenCreated: NIODeadline = NIODeadline.now()
4843

4944
internal init(
50-
authenticationConfig: APNSwiftConfiguration.Authentication,
45+
authenticationConfig: APNSConfiguration.Authentication,
5146
logger: Logger? = nil
5247
) {
53-
self.signer = APNSwiftSigner(
48+
self.signer = APNSSigner(
5449
privateKey: authenticationConfig.privateKey,
5550
teamIdentifier: authenticationConfig.teamIdentifier,
5651
keyIdentifier: authenticationConfig.keyIdentifier
5752
)
5853
self.logger = logger
5954
}
6055

61-
private func makeNewBearerToken() throws -> String {
62-
let newToken = try signer.sign()
63-
logger?.debug("Creating a new APNS token \(newToken.prefix(8))...")
56+
private func makeNewBearerToken() async throws -> String {
57+
let newToken = try await signer.sign()
58+
logger?.debug("APNS new token \(newToken.prefix(8))...")
6459
return newToken
6560
}
6661

Sources/APNSwift/APNSClient+LoggerConfig.swift

-34
This file was deleted.

0 commit comments

Comments
 (0)