mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-29 00:31:14 +00:00
Return non-optional Data from the webservice send methods.
This commit is contained in:
1 parent
35e092d50b
commit
f8d2dcc404
5 files changed
+10
-27
No files matched your search
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -14,7 +14,7 @@ nonisolated extension URLSession {
|
||||
public func send<R: Decodable & Sendable>(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<R: Decodable & Sendable>(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)
|
||||
|
||||
Reference in new issue
Block a user