From 9fdc57408c9ef6d1d9f0fc611c252e62237c9b19 Mon Sep 17 00:00:00 2001 From: Sam Spencer Date: Thu, 2 Nov 2023 13:18:39 -0400 Subject: [PATCH] Add runWithoutResults function --- Sources/Froggy/URLSession+run.swift | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sources/Froggy/URLSession+run.swift b/Sources/Froggy/URLSession+run.swift index e381948..bb7130a 100644 --- a/Sources/Froggy/URLSession+run.swift +++ b/Sources/Froggy/URLSession+run.swift @@ -48,4 +48,29 @@ public extension URLSession { } } + func runWithoutResults(_ request: URLRequest) async throws { + do { + frogLog("Running URLSession with request: \(request)") + + let (_, response) = try await data(for: request) + guard let httpResponse = response as? HTTPURLResponse else { + frogLog("Received nil response from request.") + throw DataError.genericNetworkingFailure(code: 1000) + } + + frogLog("Request returned a \(httpResponse.statusCode) status code") + + switch httpResponse.statusCode { + case 200...299: + frogLog("Request successful.") + return + case 401, 403, 405, 422: throw DataError.notAuthed + case 404: throw DataError.nothingLoaded + default: throw DataError.genericNetworkingFailure(code: httpResponse.statusCode) + } + } catch let error { + frogLog("Failed to successfully run or complete request: \(request). Error: \(error)") + throw error + } + } }