Skip to content

Commit f5e48fb

Browse files
committed
Migrated to new swift concurrency
1 parent 143a84e commit f5e48fb

File tree

8 files changed

+69
-136
lines changed

8 files changed

+69
-136
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.6
22

33
import PackageDescription
44

55
let package = Package(
66
name: "SwiftyAPNS",
77
platforms: [
8-
.iOS(.v12),
9-
.macOS(.v10_13)
8+
.iOS(.v13),
9+
.macOS(.v12)
1010
],
1111
products: [
1212
.library(

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,12 @@ let notification = APNSNotification.init(payload: <#Notification Payload#>,
4444
The values in responses can be handled asynchronously.
4545

4646
```swift
47-
provider.push(notification) { (result) in
48-
switch(result) {
49-
case .success(let responce):
50-
if let error = responce.reason {
51-
// Push Notification failure
52-
} else {
53-
// Push Notification was successfully sent
54-
}
55-
case .failure(let error):
47+
do {
48+
let responce = try await provider.push(notification)
49+
// Push Notification was successfully sent
50+
} catch {
5651
// Push Notification failure
5752
}
58-
}
5953
```
6054

6155
### [More examples](/Tests/SwiftyAPNSTests/SwiftyAPNSTests.swift)

Sources/SwiftyAPNS/Provider/CertificateProvider.swift

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,26 @@ import Foundation
1111
internal final class APNSCertificateProvider: NSObject, APNSSendMessageProtocol {
1212

1313
private var identity: SecIdentity
14-
private var sesion: URLSession = URLSession.shared
14+
private var session: URLSession = URLSession.shared
1515

1616
private static let decoder = JSONDecoder()
1717

18-
public init(identity: SecIdentity, sandbox: Bool = true, configuration: URLSessionConfiguration = URLSessionConfiguration.default, qeue: OperationQueue = OperationQueue.main) {
18+
public init(identity: SecIdentity, sandbox: Bool = true, configuration: URLSessionConfiguration = URLSessionConfiguration.default) {
1919
self.identity = identity
2020
super.init()
21-
self.sesion = URLSession.init(configuration: configuration, delegate: self, delegateQueue: qeue)
21+
self.session = URLSession.init(configuration: configuration, delegate: self, delegateQueue: nil)
2222
}
2323

24-
public func push<P: Payloadable>(_ notification: APNSNotification<P>, completion: @escaping (Result<APNSResponse, Error>) -> Void) {
25-
do {
26-
let request = try APNSRequestFactory.makeRequest(notification)
27-
let task = self.sesion.dataTask(with: request) { (data, responce, error) in
28-
if let error = error {
29-
completion(.failure(error))
30-
} else if let responce = responce as? HTTPURLResponse, let data = data {
31-
if let apnsStatus = APNSStatus(code: responce.statusCode),
32-
let apnsId = responce.allHeaderFields["apns-id"] as? String
33-
{
34-
let reason = try? Self.decoder.decode(APNSError.self, from: data)
35-
let apnsResponce = APNSResponse(status: apnsStatus, apnsId: apnsId, reason: reason)
36-
completion(.success(apnsResponce))
37-
} else {
38-
completion(.failure(APNSProviderError.parseResponce))
39-
}
40-
} else {
41-
completion(.failure(APNSProviderError.emptyData))
42-
}
43-
}
44-
task.resume()
45-
} catch {
46-
completion(.failure(error))
24+
public func push<P: Payloadable>(_ notification: APNSNotification<P>) async throws -> APNSResponse {
25+
let request = try APNSRequestFactory.makeRequest(notification)
26+
let (data, response) = try await session.data(for: request)
27+
if let responce = response as? HTTPURLResponse,
28+
let apnsStatus = APNSStatus(code: responce.statusCode),
29+
let apnsId = responce.allHeaderFields["apns-id"] as? String {
30+
let reason = try? Self.decoder.decode(APNSError.self, from: data)
31+
return APNSResponse(status: apnsStatus, apnsId: apnsId, reason: reason)
32+
} else {
33+
throw APNSProviderError.parseResponce
4734
}
4835
}
4936
}

Sources/SwiftyAPNS/Provider/KeyProvider.swift

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,30 @@ import Foundation
1111
internal final class APNSKeyProvider: APNSSendMessageProtocol {
1212

1313
private let token: APNSBearerToken
14-
private let sesion: URLSession
14+
private let session: URLSession
1515

16-
private static let encoder = JSONEncoder()
1716
private static let decoder = JSONDecoder()
1817

1918
public init(p8: P8, keyId: String, teamId: String, sandbox: Bool = true,
20-
configuration: URLSessionConfiguration = URLSessionConfiguration.default,
21-
qeue: OperationQueue = OperationQueue.main)
19+
configuration: URLSessionConfiguration = URLSessionConfiguration.default)
2220
{
2321
self.token = APNSBearerToken(p8: p8, keyId: keyId, teamId: teamId)
24-
self.sesion = URLSession.init(configuration: configuration, delegate: nil, delegateQueue: qeue)
22+
self.session = URLSession.init(configuration: configuration)
2523
}
2624

27-
public func push<P: Payloadable>(_ notification: APNSNotification<P>, completion: @escaping (Result<APNSResponse, Error>) -> Void) {
28-
do {
29-
var request = try APNSRequestFactory.makeRequest(notification)
30-
let dataToken = try token.generateIfExpired()
31-
request.setValue("application/json;", forHTTPHeaderField: "Content-Type")
32-
request.setValue("bearer \(dataToken)", forHTTPHeaderField: "Authorization")
33-
let task = self.sesion.dataTask(with: request) { (data, responce, error) in
34-
if let error = error {
35-
completion(.failure(error))
36-
} else if let responce = responce as? HTTPURLResponse, let data = data {
37-
if let apnsStatus = APNSStatus(code: responce.statusCode),
38-
let apnsId = responce.allHeaderFields["apns-id"] as? String
39-
{
40-
let reason = try? Self.decoder.decode(APNSError.self, from: data)
41-
let apnsResponce = APNSResponse(status: apnsStatus, apnsId: apnsId, reason: reason)
42-
completion(.success(apnsResponce))
43-
} else {
44-
completion(.failure(APNSProviderError.parseResponce))
45-
}
46-
} else {
47-
completion(.failure(APNSProviderError.emptyData))
48-
}
49-
}
50-
task.resume()
51-
} catch {
52-
completion(.failure(error))
25+
public func push<P: Payloadable>(_ notification: APNSNotification<P>) async throws -> APNSResponse {
26+
var request = try APNSRequestFactory.makeRequest(notification)
27+
let dataToken = try token.generateIfExpired()
28+
request.setValue("application/json;", forHTTPHeaderField: "Content-Type")
29+
request.setValue("bearer \(dataToken)", forHTTPHeaderField: "Authorization")
30+
let (data, response) = try await session.data(for: request)
31+
if let responce = response as? HTTPURLResponse,
32+
let apnsStatus = APNSStatus(code: responce.statusCode),
33+
let apnsId = responce.allHeaderFields["apns-id"] as? String {
34+
let reason = try? Self.decoder.decode(APNSError.self, from: data)
35+
return APNSResponse(status: apnsStatus, apnsId: apnsId, reason: reason)
36+
} else {
37+
throw APNSProviderError.parseResponce
5338
}
5439
}
5540
}

Sources/SwiftyAPNS/Provider/Provider.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@ import Foundation
1111
public struct APNSProvider {
1212
private let provider: APNSSendMessageProtocol
1313

14-
public func push<P: Payloadable>(_ notification: APNSNotification<P>, completion: @escaping (Result<APNSResponse, Error>) -> Void) {
15-
self.provider.push(notification, completion: completion)
14+
public func push<P: Payloadable>(_ notification: APNSNotification<P>) async throws -> APNSResponse {
15+
try await self.provider.push(notification)
1616
}
1717
}
1818

1919
extension APNSProvider {
2020
public init(identity: SecIdentity, sandbox: Bool = true,
21-
configuration: URLSessionConfiguration = URLSessionConfiguration.default,
22-
qeue: OperationQueue = OperationQueue.main)
21+
configuration: URLSessionConfiguration = URLSessionConfiguration.default)
2322
{
24-
self.provider = APNSCertificateProvider(identity: identity, sandbox: sandbox, configuration: configuration, qeue: qeue)
23+
self.provider = APNSCertificateProvider(identity: identity, sandbox: sandbox, configuration: configuration)
2524
}
2625

2726
public init(p8: P8, keyId: String, teamId: String, sandbox: Bool = true,
28-
configuration: URLSessionConfiguration = URLSessionConfiguration.default,
29-
qeue: OperationQueue = OperationQueue.main)
27+
configuration: URLSessionConfiguration = URLSessionConfiguration.default)
3028
{
31-
self.provider = APNSKeyProvider(p8: p8, keyId: keyId, teamId: teamId, sandbox: sandbox, configuration: configuration, qeue: qeue)
29+
self.provider = APNSKeyProvider(p8: p8, keyId: keyId, teamId: teamId, sandbox: sandbox, configuration: configuration)
3230
}
3331
}

Sources/SwiftyAPNS/Provider/SendMessageProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
//
88

99
internal protocol APNSSendMessageProtocol {
10-
func push<P: Payloadable>(_ notification: APNSNotification<P>, completion: @escaping (Result<APNSResponse, Error>) -> Void)
10+
func push<P: Payloadable>(_ notification: APNSNotification<P>) async throws -> APNSResponse
1111
}

Tests/SwiftyAPNSTests/SwiftyAPNSTests+Private.swift

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,23 @@ import XCTest
77
@testable import SwiftyAPNS
88

99
extension SwiftyAPNSTests {
10-
func sendPushNotification<P: Payloadable>(_ notification: APNSNotification<P>) {
10+
func sendPushNotification<P: Payloadable>(_ notification: APNSNotification<P>) async throws {
1111
#if false
1212
let encoder = JSONEncoder()
1313
encoder.outputFormatting = .prettyPrinted
1414
let encoded = try! encoder.encode(notification.payload)
1515
print("Payload:\n\(String(data: encoded, encoding: .utf8)!)")
1616
#endif
17-
let expect = self.expectation(description: "APNSExpectation")
18-
provider.push(notification) { (result) in
19-
switch(result) {
20-
case .success(let responce):
21-
if let error = responce.reason {
22-
XCTFail(error.errorDescription ?? "Failure send push notification")
23-
} else {
24-
print("ApnsId: \(responce.apnsId)")
25-
expect.fulfill()
26-
}
27-
case .failure(let error):
28-
if let error = error as? LocalizedError {
29-
XCTFail(error.localizedDescription)
30-
} else {
31-
XCTFail("Failure send push notification")
32-
}
33-
}
34-
}
35-
}
36-
37-
func waitForResponce() {
38-
self.waitForExpectations(timeout: 30.0) { (error) in
39-
if let error = error {
40-
XCTFail(error.localizedDescription)
41-
}
42-
}
17+
let responce = try await provider.push(notification)
18+
XCTAssertNil(responce.reason)
4319
}
4420

45-
func visit(notification: Notification) {
21+
func visit(notification: Notification) async throws {
4622
switch notification {
4723
case .payload(let notificatiuon):
48-
sendPushNotification(notificatiuon)
24+
try await sendPushNotification(notificatiuon)
4925
case .payload4(let notificatiuon):
50-
sendPushNotification(notificatiuon)
26+
try await sendPushNotification(notificatiuon)
5127
}
5228
}
5329

Tests/SwiftyAPNSTests/SwiftyAPNSTests.swift

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,56 +116,49 @@ final class SwiftyAPNSTests: XCTestCase {
116116
#endif
117117
}
118118

119-
func testAlertPushExample() {
120-
sendPushNotification(alertPushExample)
121-
waitForResponce()
119+
func testAlertPushExample() async throws {
120+
try await sendPushNotification(alertPushExample)
122121
}
123122

124-
func testAlertWithSubtitlePushExample() {
125-
sendPushNotification(alertWithSubtitlePushExample)
126-
waitForResponce()
123+
func testAlertWithSubtitlePushExample() async throws {
124+
try await sendPushNotification(alertWithSubtitlePushExample)
127125
}
128126

129-
func testLocalizableAlertPushExample() {
130-
sendPushNotification(localizableAlertPushExample)
131-
waitForResponce()
127+
func testLocalizableAlertPushExample() async throws {
128+
try await sendPushNotification(localizableAlertPushExample)
132129
}
133130

134-
func testAlertWithCustomActionsPushExample() {
135-
sendPushNotification(alertWithCustomActionsPushExample)
136-
waitForResponce()
131+
func testAlertWithCustomActionsPushExample() async throws {
132+
try await sendPushNotification(alertWithCustomActionsPushExample)
137133
}
138134

139-
func testLocalizableAlertPushWithCustomPayloadExample1() {
140-
sendPushNotification(localizableAlertPushWithCustomPayloadExample1)
141-
waitForResponce()
135+
func testLocalizableAlertPushWithCustomPayloadExample1() async throws {
136+
try await sendPushNotification(localizableAlertPushWithCustomPayloadExample1)
142137
}
143138

144-
func testLocalizableAlertPushWithCustomPayloadExample2() {
145-
sendPushNotification(localizableAlertPushWithCustomPayloadExample2)
146-
waitForResponce()
139+
func testLocalizableAlertPushWithCustomPayloadExample2() async throws {
140+
try await sendPushNotification(localizableAlertPushWithCustomPayloadExample2)
147141
}
148142

149-
func testModifyingContentPushExample() {
150-
sendPushNotification(modifyingContentPushExample)
151-
waitForResponce()
143+
func testModifyingContentPushExample() async throws {
144+
try await sendPushNotification(modifyingContentPushExample)
152145
}
153146

154-
func testBackgroundPushExample() {
155-
sendPushNotification(backgroundPushExample)
156-
waitForResponce()
147+
func testBackgroundPushExample() async throws {
148+
try await sendPushNotification(backgroundPushExample)
157149
}
158150

159-
func testSendingMultiplePushes() {
151+
func testSendingMultiplePushes() async throws {
160152
let notifications: [Notification] = [
161153
.payload(notificatiuon: alertPushExample),
162154
.payload(notificatiuon: alertWithSubtitlePushExample),
163155
.payload(notificatiuon: localizableAlertPushExample),
164156
.payload(notificatiuon: alertWithCustomActionsPushExample),
165157
.payload4(notificatiuon: backgroundPushExample)
166158
]
167-
notifications.forEach(visit)
168-
waitForResponce()
159+
for notification in notifications {
160+
try await visit(notification: notification)
161+
}
169162
}
170163

171164
static var allTests = [

0 commit comments

Comments
 (0)