diff --git a/Sources/SwiftJSONRPC/RequestExecutor/HTTP/Client/URLSessionHTTPClient.swift b/Sources/SwiftJSONRPC/RequestExecutor/HTTP/Client/URLSessionHTTPClient.swift index 7ab2209..5e6077d 100644 --- a/Sources/SwiftJSONRPC/RequestExecutor/HTTP/Client/URLSessionHTTPClient.swift +++ b/Sources/SwiftJSONRPC/RequestExecutor/HTTP/Client/URLSessionHTTPClient.swift @@ -7,7 +7,9 @@ import Foundation -struct URLSessionHTTPClient: HTTPClient { +private let requestTimeout: TimeInterval = 60 * 60 + +public struct URLSessionHTTPClient: HTTPClient { // MARK: - Private Properties @@ -15,7 +17,7 @@ struct URLSessionHTTPClient: HTTPClient { // MARK: - Initialization - init(urlSession: URLSession = .shared) { + init(urlSession: URLSession = newURLSession()) { self.urlSession = urlSession } @@ -57,11 +59,18 @@ struct URLSessionHTTPClient: HTTPClient { return DispatchQueue.global() } + private static func newURLSession() -> URLSession { + let config = URLSessionConfiguration.default + config.timeoutIntervalForRequest = requestTimeout + config.timeoutIntervalForResource = requestTimeout + return URLSession(configuration: config) + } + // MARK: - Inner Types - class URLSessionError: NestedError, HTTPClientError { } - class NoResponseError: HTTPClientError { } - class NoResponseDataError: HTTPClientError { } + public class URLSessionError: NestedError, HTTPClientError { } + public class NoResponseError: HTTPClientError { } + public class NoResponseDataError: HTTPClientError { } } @@ -75,6 +84,7 @@ extension HTTPRequest { request.httpMethod = method.rawValue request.allHTTPHeaderFields = headers request.httpBody = body + request.timeoutInterval = requestTimeout return request } diff --git a/Sources/SwiftJSONRPC/RequestExecutor/HTTP/HTTPRequestExecutorError.swift b/Sources/SwiftJSONRPC/RequestExecutor/HTTP/HTTPRequestExecutorError.swift index 2126f35..d796e85 100644 --- a/Sources/SwiftJSONRPC/RequestExecutor/HTTP/HTTPRequestExecutorError.swift +++ b/Sources/SwiftJSONRPC/RequestExecutor/HTTP/HTTPRequestExecutorError.swift @@ -6,6 +6,8 @@ // // ---------------------------------------------------------------------------- +import Foundation + public struct HTTPRequestExecutorError: RequestExecutorError { // MARK: - Construction @@ -47,7 +49,7 @@ public struct HTTPRequestExecutorError: RequestExecutorError { // ---------------------------------------------------------------------------- -open class NestedError: Error { +open class NestedError: LocalizedError { // MARK: - Construction public init(cause: T?) { @@ -57,7 +59,10 @@ open class NestedError: Error { // MARK: - Properties public let cause: T? - + + public var errorDescription: String? { + cause?.localizedDescription + } } // ---------------------------------------------------------------------------- @@ -65,12 +70,12 @@ open class NestedError: Error { extension NestedError: CustomStringConvertible, CustomDebugStringConvertible { // MARK: - Properties - open var description: String { + public var description: String { let cause = (self.cause != nil) ? String(describing: self.cause!) : "nil" return "\(type(of: self))(cause: \(cause))" } - open var debugDescription: String { + public var debugDescription: String { return self.description } diff --git a/Sources/SwiftJSONRPC/RequestExecutor/Response.swift b/Sources/SwiftJSONRPC/RequestExecutor/Response.swift index 81fe9ee..cfe5acd 100644 --- a/Sources/SwiftJSONRPC/RequestExecutor/Response.swift +++ b/Sources/SwiftJSONRPC/RequestExecutor/Response.swift @@ -16,7 +16,7 @@ public class Response { // MARK: - Initialization - init(response: Any) throws { + public init(response: Any) throws { guard let json = (response as? [String: AnyObject]), let version = (json[JsonKeys.JsonRPC] as? String), (version == RPCClient.Version), let id = (json[JsonKeys.Id] as? String)