From e9cb882f7681213c1895e35692b234dce1f467af Mon Sep 17 00:00:00 2001 From: Sacha DSO Date: Mon, 13 Nov 2023 15:03:58 -1000 Subject: [PATCH] Simplify execute implementation with urlSession async api --- Sources/Networking/NetworkingRequest.swift | 31 +++++++--------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/Sources/Networking/NetworkingRequest.swift b/Sources/Networking/NetworkingRequest.swift index ddb741f..a113c2d 100644 --- a/Sources/Networking/NetworkingRequest.swift +++ b/Sources/Networking/NetworkingRequest.swift @@ -120,28 +120,17 @@ public class NetworkingRequest: NSObject, URLSessionTaskDelegate { logger.log(request: urlRequest) let config = sessionConfiguration ?? URLSessionConfiguration.default let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil) - return try await withCheckedThrowingContinuation { continuation in - urlSession.dataTask(with: urlRequest) { data, response, error in - guard let data = data, let response = response else { - if let error = error { - continuation.resume(throwing: error) - } - return - } - self.logger.log(response: response, data: data) - if let httpURLResponse = response as? HTTPURLResponse { - if !(200...299 ~= httpURLResponse.statusCode) { - var error = NetworkingError(errorCode: httpURLResponse.statusCode) - if let json = try? JSONSerialization.jsonObject(with: data, options: []) { - error.jsonPayload = json - } - continuation.resume(throwing: error) - return - } - } - continuation.resume(returning: data) - }.resume() + let (data, response) = try await urlSession.data(for: urlRequest) + self.logger.log(response: response, data: data) + if let httpURLResponse = response as? HTTPURLResponse, !(200...299 ~= httpURLResponse.statusCode) { + var error = NetworkingError(errorCode: httpURLResponse.statusCode) + if let json = try? JSONSerialization.jsonObject(with: data, options: []) { + error.jsonPayload = json + } + throw error } + return data + } private func getURLWithParams() -> String {