Skip to content

Commit

Permalink
Add timeout errors
Browse files Browse the repository at this point in the history
  • Loading branch information
afterxleep committed Mar 12, 2024
1 parent 9d12b29 commit 21f2703
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Sources/WireKit/Protocols/WKRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand All @@ -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() }

}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/WireKit/WKAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: WKRequest>(_ request: Request) async throws -> Request.ReturnType {
guard let urlRequest = request.asURLRequest(baseURL: baseURL) else {
throw WKNetworkRequestError.badRequest()
Expand Down
4 changes: 2 additions & 2 deletions Sources/WireKit/WKNetworkDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType: Codable>(request: URLRequest, decoder: JSONDecoder?) async throws -> ReturnType {
let decoder = decoder ?? JSONDecoder()

Expand All @@ -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 {
Expand Down

0 comments on commit 21f2703

Please sign in to comment.