Use modern async APIs.

This commit is contained in:
Brent Simmons committed 2026-06-14 17:16:57 -07:00
1 parent 1769496382
commit 35e092d50b
2 files changed
+48 -198

No files matched your search

@@ -158,102 +158,40 @@ nonisolated extension URLSession {
}
}
// These methods run on the cooperative thread pool, not the caller’s actor,
// because the extension is nonisolated — so JSON decoding done by callers in
// URLSession+WebserviceJSON happens off the main actor.
@discardableResult
public func send(request: URLRequest) async throws -> (HTTPURLResponse, Data?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request) { result in
continuation.resume(with: result)
}
}
}
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 {
return completion(.failure(error))
}
guard let response = response as? HTTPURLResponse, let data = data else {
return completion(.failure(WebserviceError.noData))
}
switch response.forcedStatusCode {
case 200...399:
completion(.success((response, data)))
default:
completion(.failure(WebserviceError.httpError(status: response.forcedStatusCode)))
}
}
}
task.resume()
let (data, response) = try await data(for: request)
return (try Self.validatedHTTPResponse(response), data)
}
public func send(request: URLRequest, method: String) async throws {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method) { result in
continuation.resume(with: result)
}
}
}
func send(request: URLRequest, method: String, completion: @escaping @Sendable (Result<Void, Error>) -> Void) {
var sendRequest = request
sendRequest.httpMethod = method
let task = self.dataTask(with: sendRequest) { (_, response, error) in
DispatchQueue.main.async {
if let error = error {
return completion(.failure(error))
}
guard let response = response as? HTTPURLResponse else {
return completion(.failure(WebserviceError.noData))
}
switch response.forcedStatusCode {
case 200...399:
completion(.success(()))
default:
completion(.failure(WebserviceError.httpError(status: response.forcedStatusCode)))
}
}
}
task.resume()
let (_, response) = try await data(for: sendRequest)
_ = try Self.validatedHTTPResponse(response)
}
public func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method, payload: payload) { result in
continuation.resume(with: result)
}
}
}
func send(request: URLRequest, method: String, payload: Data, completion: @escaping @Sendable (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
var sendRequest = request
sendRequest.httpMethod = method
let (data, response) = try await upload(for: sendRequest, from: payload)
return (try Self.validatedHTTPResponse(response), data)
}
let task = self.uploadTask(with: sendRequest, from: payload) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {
return completion(.failure(error))
}
guard let response = response as? HTTPURLResponse, let data = data else {
return completion(.failure(WebserviceError.noData))
}
switch response.forcedStatusCode {
case 200...399:
completion(.success((response, data)))
default:
completion(.failure(WebserviceError.httpError(status: response.forcedStatusCode)))
}
}
/// Require an HTTP response with a 200...399 status code, or throw the matching `WebserviceError`.
private static func validatedHTTPResponse(_ response: URLResponse) throws -> HTTPURLResponse {
guard let httpResponse = response as? HTTPURLResponse else {
throw WebserviceError.noData
}
switch httpResponse.forcedStatusCode {
case 200...399:
return httpResponse
default:
throw WebserviceError.httpError(status: httpResponse.forcedStatusCode)
}
task.resume()
}
}
@@ -10,138 +10,50 @@ import Foundation
nonisolated extension URLSession {
/// Send an HTTP GET and return JSON object(s)
/// Send an HTTP GET and return JSON object(s).
public func send<R: Decodable & Sendable>(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) {
try await withCheckedThrowingContinuation { continuation in
send(request: request, resultType: resultType, dateDecoding: dateDecoding, keyDecoding: keyDecoding) { result in
continuation.resume(with: result)
}
}
}
/**
Sends an HTTP get and returns JSON object(s)
*/
func send<R: Decodable & Sendable>(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 {
switch result {
case .success(let (response, data)):
if let data = data, !data.isEmpty {
// PBS 27 Sep. 2019: decode the JSON on a background thread.
// The profiler says that this is 45% of what’s happening on the main thread
// during an initial sync with Feedbin.
DispatchQueue.global(qos: .background).async {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = dateDecoding
decoder.keyDecodingStrategy = keyDecoding
do {
let decoded = try decoder.decode(R.self, from: data)
DispatchQueue.main.async {
completion(.success((response, decoded)))
}
} catch {
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
} else {
completion(.success((response, nil)))
}
case .failure(let error):
completion(.failure(error))
}
}
let (response, data) = try await send(request: request)
guard let data, !data.isEmpty else {
return (response, nil)
}
let decoded = try await Self.decode(R.self, from: data, dateDecoding: dateDecoding, keyDecoding: keyDecoding)
return (response, decoded)
}
/// Send the specified HTTP method with a JSON payload.
public func send<P: Encodable & Sendable>(request: URLRequest, method: String, payload: P) async throws {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method, payload: payload) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
/**
Sends the specified HTTP method with a JSON payload.
*/
func send<P: Encodable>(request: URLRequest, method: String, payload: P, completion: @escaping @Sendable (Result<Void, Error>) -> Void) {
var postRequest = request
postRequest.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
let data: Data
do {
data = try JSONEncoder().encode(payload)
} catch {
completion(.failure(error))
return
}
send(request: postRequest, method: method, payload: data) { result in
DispatchQueue.main.async {
switch result {
case .success((_, _)):
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
let data = try await Self.encode(payload)
_ = try await send(request: postRequest, method: method, payload: data)
}
/// Send the specified HTTP method with a raw payload and return JSON object(s).
public func send<R: Decodable & Sendable>(request: URLRequest, method: String, data: Data, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method, data: data, resultType: resultType, dateDecoding: dateDecoding, keyDecoding: keyDecoding) { result in
switch result {
case .success(let (response, decoded)):
continuation.resume(returning: (response, decoded))
case .failure(let error):
continuation.resume(throwing: error)
}
}
let (response, responseData) = try await send(request: request, method: method, payload: data)
guard let responseData, !responseData.isEmpty else {
return (response, nil)
}
let decoded = try await Self.decode(R.self, from: responseData, dateDecoding: dateDecoding, keyDecoding: keyDecoding)
return (response, decoded)
}
/**
Sends the specified HTTP method with a Raw payload and returns JSON object(s).
*/
func send<R: Decodable>(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) {
@concurrent
private static func encode<P: Encodable & Sendable>(_ payload: P) async throws -> Data {
assert(!Thread.isMainThread, "JSON encoding should not happen on the main thread.")
return try JSONEncoder().encode(payload)
}
send(request: request, method: method, payload: data) { result in
DispatchQueue.main.async {
switch result {
case .success(let (response, data)):
do {
if let data = data, !data.isEmpty {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = dateDecoding
decoder.keyDecodingStrategy = keyDecoding
let decoded = try decoder.decode(R.self, from: data)
completion(.success((response, decoded)))
} else {
completion(.success((response, nil)))
}
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
@concurrent
private static func decode<R: Decodable & Sendable>(_ type: R.Type, from data: Data, dateDecoding: JSONDecoder.DateDecodingStrategy, keyDecoding: JSONDecoder.KeyDecodingStrategy) async throws -> R {
assert(!Thread.isMainThread, "JSON decoding should not happen on the main thread.")
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = dateDecoding
decoder.keyDecodingStrategy = keyDecoding
return try decoder.decode(R.self, from: data)
}
}