From 21f2703922656b9d04e389f7daec84cc4eb94a0b Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Tue, 12 Mar 2024 17:20:34 +0100 Subject: [PATCH] Add timeout errors --- Sources/WireKit/Protocols/WKRequest.swift | 5 +++++ Sources/WireKit/WKAPIClient.swift | 2 +- Sources/WireKit/WKNetworkDispatcher.swift | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/WireKit/Protocols/WKRequest.swift b/Sources/WireKit/Protocols/WKRequest.swift index 868c6c9..76c0f8d 100644 --- a/Sources/WireKit/Protocols/WKRequest.swift +++ b/Sources/WireKit/Protocols/WKRequest.swift @@ -37,6 +37,7 @@ public protocol WKRequest { var queryParams: WKHTTPParams? { get } var body: WKHTTPParams? { get } var headers: WKHTTPHeaders? { get } + var timeout: TimeInterval? { get } var decoder: JSONDecoder? { get } } @@ -49,6 +50,7 @@ public extension WKRequest { var body: WKHTTPParams? { return nil } var headers: WKHTTPHeaders? { return nil } var debug: Bool { return false } + var timeout: TimeInterval? { return nil } var decoder: JSONDecoder? { return JSONDecoder() } } @@ -93,6 +95,9 @@ extension WKRequest { WKHTTPHeaderField.contentType.rawValue: contentType.rawValue, WKHTTPHeaderField.acceptType.rawValue: contentType.rawValue ] + if let timeoutInterval = timeout { + request.timeoutInterval = timeoutInterval + } request.allHTTPHeaderFields = defaultHeaders.merging(headers ?? [:], uniquingKeysWith: { (first, _) in first }) return request } diff --git a/Sources/WireKit/WKAPIClient.swift b/Sources/WireKit/WKAPIClient.swift index 5864821..c039a7d 100644 --- a/Sources/WireKit/WKAPIClient.swift +++ b/Sources/WireKit/WKAPIClient.swift @@ -22,7 +22,7 @@ public struct WKAPIClient { /// Dispatches an WKRequest and returns a publisher /// - Parameter request: WKRequest to Dispatch - /// - Returns: The generic return typ + /// - Returns: The generic return type public func dispatch(_ request: Request) async throws -> Request.ReturnType { guard let urlRequest = request.asURLRequest(baseURL: baseURL) else { throw WKNetworkRequestError.badRequest() diff --git a/Sources/WireKit/WKNetworkDispatcher.swift b/Sources/WireKit/WKNetworkDispatcher.swift index 91d18e1..a2adf5d 100644 --- a/Sources/WireKit/WKNetworkDispatcher.swift +++ b/Sources/WireKit/WKNetworkDispatcher.swift @@ -33,7 +33,7 @@ public struct WKNetworkDispatcher { /// Dispatches an URLRequest and returns a publisher /// - Parameter request: URLRequest - /// - Returns: A genery return type + /// - Returns: A generic return type public func dispatch(request: URLRequest, decoder: JSONDecoder?) async throws -> ReturnType { let decoder = decoder ?? JSONDecoder() @@ -49,7 +49,7 @@ public struct WKNetworkDispatcher { do { return try decoder.decode(ReturnType.self, from: data) - } catch { + } catch(let error) { throw WKNetworkRequestError.decodingError(error.localizedDescription) } } catch {