From 46645f700a0f292a8cfc555ad38d5cf8f8ea5f59 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 6 Apr 2024 19:01:27 -0700 Subject: [PATCH] Fix concurrency warnings in Transport. --- Web/Sources/Web/WebServices/Transport.swift | 20 +++++++++---------- .../Web/WebServices/TransportJSON.swift | 14 ++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Web/Sources/Web/WebServices/Transport.swift b/Web/Sources/Web/WebServices/Transport.swift index 53ddb54ef..7915953fd 100644 --- a/Web/Sources/Web/WebServices/Transport.swift +++ b/Web/Sources/Web/WebServices/Transport.swift @@ -123,19 +123,19 @@ public protocol Transport: Sendable { @discardableResult func send(request: URLRequest) async throws -> (HTTPURLResponse, Data?) - func send(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) - + func send(request: URLRequest, completion: @escaping @Sendable (Result<(HTTPURLResponse, Data?), Error>) -> Void) + /// Sends URLRequest that doesn't require any result information. func send(request: URLRequest, method: String) async throws - func send(request: URLRequest, method: String, completion: @escaping (Result) -> Void) - + func send(request: URLRequest, method: String, completion: @escaping @Sendable (Result) -> Void) + /// Sends URLRequest with a data payload and returns the HTTP headers and the data payload. @discardableResult func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data?) - func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) - + func send(request: URLRequest, method: String, payload: Data, completion: @escaping @Sendable (Result<(HTTPURLResponse, Data?), Error>) -> Void) + } extension URLSession: Transport { @@ -162,7 +162,7 @@ extension URLSession: Transport { } } - public func send(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) { + public func send(request: URLRequest, completion: @escaping @Sendable (Result<(HTTPURLResponse, Data?), Error>) -> Void) { let task = self.dataTask(with: request) { (data, response, error) in DispatchQueue.main.async { if let error = error { @@ -198,8 +198,8 @@ extension URLSession: Transport { } } - public func send(request: URLRequest, method: String, completion: @escaping (Result) -> Void) { - + public func send(request: URLRequest, method: String, completion: @escaping @Sendable (Result) -> Void) { + var sendRequest = request sendRequest.httpMethod = method @@ -238,7 +238,7 @@ extension URLSession: Transport { } } - public func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) { + public func send(request: URLRequest, method: String, payload: Data, completion: @escaping @Sendable (Result<(HTTPURLResponse, Data?), Error>) -> Void) { var sendRequest = request sendRequest.httpMethod = method diff --git a/Web/Sources/Web/WebServices/TransportJSON.swift b/Web/Sources/Web/WebServices/TransportJSON.swift index c0c4876df..a1373e8c2 100644 --- a/Web/Sources/Web/WebServices/TransportJSON.swift +++ b/Web/Sources/Web/WebServices/TransportJSON.swift @@ -10,7 +10,7 @@ import Foundation extension Transport { - public func send(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) { + public func send(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) { try await withCheckedThrowingContinuation { continuation in self.send(request: request, resultType: resultType, dateDecoding: dateDecoding, keyDecoding: keyDecoding) { result in @@ -27,8 +27,8 @@ extension Transport { /** Sends an HTTP get and returns JSON object(s) */ - public func send(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys, completion: @escaping (Result<(HTTPURLResponse, R?), Error>) -> Void) { - + public func send(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys, completion: @escaping @Sendable (Result<(HTTPURLResponse, R?), Error>) -> Void) { + send(request: request) { result in DispatchQueue.main.async { @@ -69,8 +69,8 @@ extension Transport { /** Sends the specified HTTP method with a JSON payload. */ - public func send(request: URLRequest, method: String, payload: P, completion: @escaping (Result) -> Void) { - + public func send(request: URLRequest, method: String, payload: P, completion: @escaping @Sendable (Result) -> Void) { + var postRequest = request postRequest.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType) @@ -97,7 +97,7 @@ extension Transport { /** Sends the specified HTTP method with a JSON payload and returns JSON object(s). */ - public func send(request: URLRequest, method: String, payload: P, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys, completion: @escaping (Result<(HTTPURLResponse, R?), Error>) -> Void) { + public func send(request: URLRequest, method: String, payload: P, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys, completion: @escaping @Sendable (Result<(HTTPURLResponse, R?), Error>) -> Void) { var postRequest = request postRequest.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType) @@ -154,7 +154,7 @@ extension Transport { /** Sends the specified HTTP method with a Raw payload and returns JSON object(s). */ - public func send(request: URLRequest, method: String, data: Data, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys, completion: @escaping (Result<(HTTPURLResponse, R?), Error>) -> Void) { + public func send(request: URLRequest, method: String, data: Data, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys, completion: @escaping @Sendable (Result<(HTTPURLResponse, R?), Error>) -> Void) { send(request: request, method: method, payload: data) { result in DispatchQueue.main.async {