From 8fd1148cbd126fbb1206a2467dc219d40a78482e Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Thu, 9 Jun 2022 16:00:21 +0200 Subject: [PATCH] Reduce platform requirements --- Package.swift | 4 ++-- Sources/Push/PushClient.swift | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 7934034..1bdc139 100644 --- a/Package.swift +++ b/Package.swift @@ -5,8 +5,8 @@ import PackageDescription let package = Package( name: "Push-iOS", platforms: [ - .macOS(.v12), - .iOS(.v15), + .macOS(.v10_15), + .iOS(.v13), ], products: [ .library( diff --git a/Sources/Push/PushClient.swift b/Sources/Push/PushClient.swift index db815b1..c025c27 100644 --- a/Sources/Push/PushClient.swift +++ b/Sources/Push/PushClient.swift @@ -145,6 +145,39 @@ public final class PushClient { var request = URLRequest(url: server.appendingPathComponent(route.rawValue)) request.httpBody = bodyData request.httpMethod = "POST" + + if #available(iOS 15.0, *) { + return await post(request) + } else { + return await withCheckedContinuation { continuation in + postRequest(request) { data in + continuation.resume(returning: data) + } + } + } + } + + private func postRequest(_ request: URLRequest, completion: @escaping (Data?) -> Void) { + URLSession.shared.dataTask(with: request) { data, response, error in + if let error = error { + print("Failed with error: \(error)") + completion(nil) + } + guard let httpResponse = response as? HTTPURLResponse else { + completion(nil) + return + } + guard httpResponse.statusCode == 200 else { + print("Failed with code: \(httpResponse.statusCode)") + completion(nil) + return + } + completion(data) + }.resume() + } + + @available(iOS 15.0, *) + private func post(_ request: URLRequest) async -> Data? { do { let (data, response) = try await URLSession.shared.data(for: request) guard let httpResponse = response as? HTTPURLResponse else {