From f8d2dcc4049de7778f5ed4da75ee04b2b998a951 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 15 Jun 2026 08:13:57 -0700 Subject: [PATCH] Return non-optional Data from the webservice send methods. --- .../Sources/Account/Feedbin/FeedbinAPICaller.swift | 14 ++------------ .../Account/ReaderAPI/ReaderAPICaller.swift | 6 +----- .../TestingURLProtocol+Responses.swift | 2 +- .../RSWeb/WebServices/URLSession+Webservice.swift | 11 ++++------- .../WebServices/URLSession+WebserviceJSON.swift | 4 ++-- 5 files changed, 10 insertions(+), 27 deletions(-) diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift index 5f92f4b44..d8ec69d4a 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift @@ -78,10 +78,6 @@ enum CreateSubscriptionResult { let (_, data) = try await session.send(request: request, method: HTTPMethod.post, payload: opmlData) - guard let data else { - throw WebserviceError.noData - } - let importResult = try JSONDecoder().decode(FeedbinImportResult.self, from: data) return importResult } @@ -159,21 +155,15 @@ enum CreateSubscriptionResult { switch response.forcedStatusCode { case HTTPResponseCode.created: // 201 - guard let subData = data else { - throw WebserviceError.noData - } do { - let subscription = try JSONDecoder().decode(FeedbinSubscription.self, from: subData) + let subscription = try JSONDecoder().decode(FeedbinSubscription.self, from: data) return .created(subscription) } catch { throw error } case HTTPResponseCode.redirectMultipleChoices: // 300 - guard let subData = data else { - throw WebserviceError.noData - } do { - let subscriptions = try JSONDecoder().decode([FeedbinSubscriptionChoice].self, from: subData) + let subscriptions = try JSONDecoder().decode([FeedbinSubscriptionChoice].self, from: data) return .multipleChoice(subscriptions) } catch { throw error diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPICaller.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPICaller.swift index 0be473bec..d1746c7b3 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPICaller.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPICaller.swift @@ -99,10 +99,6 @@ enum CreateReaderAPISubscriptionResult { do { let (_, data) = try await session.send(request: request) - guard let data else { - throw WebserviceError.noData - } - // Convert the return data to UTF8 and then parse out the Auth token guard let rawData = String(data: data, encoding: .utf8) else { throw WebserviceError.noData @@ -151,7 +147,7 @@ enum CreateReaderAPISubscriptionResult { let (_, data) = try await session.send(request: request) // Convert the return data to UTF8 and then parse out the Auth token - guard let data, let updatedAccessToken = String(data: data, encoding: .utf8) else { + guard let updatedAccessToken = String(data: data, encoding: .utf8) else { throw WebserviceError.noData } // Remove unwanted \n character. diff --git a/Modules/Account/Tests/AccountTests/TestingURLProtocol+Responses.swift b/Modules/Account/Tests/AccountTests/TestingURLProtocol+Responses.swift index b13895f10..c918a8e51 100644 --- a/Modules/Account/Tests/AccountTests/TestingURLProtocol+Responses.swift +++ b/Modules/Account/Tests/AccountTests/TestingURLProtocol+Responses.swift @@ -19,7 +19,7 @@ extension TestingURLProtocol { do { data = try Data(contentsOf: fileURL) } catch { - fatalError("Unable to read fixture at \(fileURL) because \(error).") + fatalError("Unable to read response file at \(fileURL) because \(error).") } responses[urlSubstring] = Response(statusCode: statusCode, data: data) } diff --git a/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+Webservice.swift b/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+Webservice.swift index be3649942..523495020 100644 --- a/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+Webservice.swift +++ b/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+Webservice.swift @@ -158,12 +158,8 @@ 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?) { + public func send(request: URLRequest) async throws -> (HTTPURLResponse, Data) { let (data, response) = try await data(for: request) return (try Self.validatedHTTPResponse(response), data) } @@ -172,10 +168,10 @@ nonisolated extension URLSession { var sendRequest = request sendRequest.httpMethod = method let (_, response) = try await data(for: sendRequest) - _ = try Self.validatedHTTPResponse(response) + try Self.validatedHTTPResponse(response) } - public func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data?) { + public func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data) { var sendRequest = request sendRequest.httpMethod = method let (data, response) = try await upload(for: sendRequest, from: payload) @@ -183,6 +179,7 @@ nonisolated extension URLSession { } /// Require an HTTP response with a 200...399 status code, or throw the matching `WebserviceError`. + @discardableResult private static func validatedHTTPResponse(_ response: URLResponse) throws -> HTTPURLResponse { guard let httpResponse = response as? HTTPURLResponse else { throw WebserviceError.noData diff --git a/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+WebserviceJSON.swift b/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+WebserviceJSON.swift index a09bd48e2..05195656d 100644 --- a/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+WebserviceJSON.swift +++ b/Modules/RSWeb/Sources/RSWeb/WebServices/URLSession+WebserviceJSON.swift @@ -14,7 +14,7 @@ nonisolated extension URLSession { public func send(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) { let (response, data) = try await send(request: request) - guard let data, !data.isEmpty else { + guard !data.isEmpty else { return (response, nil) } let decoded = try await Self.decode(R.self, from: data, dateDecoding: dateDecoding, keyDecoding: keyDecoding) @@ -35,7 +35,7 @@ nonisolated extension URLSession { public func send(request: URLRequest, method: String, data: Data, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) { let (response, responseData) = try await send(request: request, method: method, payload: data) - guard let responseData, !responseData.isEmpty else { + guard !responseData.isEmpty else { return (response, nil) } let decoded = try await Self.decode(R.self, from: responseData, dateDecoding: dateDecoding, keyDecoding: keyDecoding)