From abbed0ad2300598b2ed81a1e302e4834df678d7d Mon Sep 17 00:00:00 2001 From: Priya Marwaha Date: Thu, 2 Aug 2018 18:10:24 +0530 Subject: [PATCH 1/3] Added optional `requestTimeInterval` to `NetworkResourceType` --- CHANGELOG.md | 4 ++++ .../Resource types/NetworkResourceType.swift | 12 ++++++++++-- TABResourceLoader.podspec | 2 +- .../Protocols/NetworkResourceTypeTests.swift | 10 ++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77e3ce0..d04b76a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 7.2.2 + +- Adds optional `requestTimeInterval` property to `NetworkResourceType` which can be used to set a `URLRequest` specific timeout for the resource. + ## 7.2.1 - Fixes an issue where `super.start()` was being called unnecessarily. diff --git a/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift b/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift index effba77..3510c99 100644 --- a/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift +++ b/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift @@ -58,6 +58,9 @@ public protocol NetworkResourceType { /// The query items to be added to the url to fetch this resource var queryItems: [URLQueryItem]? { get } + + /// The time interval for the URLRequest for this resource + var requestTimeInterval: TimeInterval? { get } /** Convenience function that builds a URLRequest for this resource @@ -74,6 +77,7 @@ public extension NetworkResourceType { public var httpHeaderFields: [String: String]? { return [:] } public var jsonBody: Any? { return nil } public var queryItems: [URLQueryItem]? { return nil } + public var requestTimeInterval: TimeInterval? { return nil } public func urlRequest() -> URLRequest? { var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) @@ -81,8 +85,12 @@ public extension NetworkResourceType { urlComponents?.queryItems = allQueryItems(initialItems: initialItems) guard let urlFromComponents = urlComponents?.url else { return nil } - - var request = URLRequest(url: urlFromComponents) + var request: URLRequest + if let timeoutInterval = requestTimeInterval { + request = URLRequest(url: urlFromComponents, timeoutInterval: timeoutInterval) + } else { + request = URLRequest(url: urlFromComponents) + } request.allHTTPHeaderFields = httpHeaderFields request.httpMethod = httpRequestMethod.rawValue diff --git a/TABResourceLoader.podspec b/TABResourceLoader.podspec index ecf519b..37ca0e2 100644 --- a/TABResourceLoader.podspec +++ b/TABResourceLoader.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = 'TABResourceLoader' spec.homepage = 'https://github.com/theappbusiness/TABResourceLoader' - spec.version = '7.2.1' + spec.version = '7.2.2' spec.license = { :type => 'MIT' } spec.authors = { 'Luciano Marisi' => 'luciano@techbrewers.com' } spec.summary = 'Framework for loading resources from a network service' diff --git a/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift b/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift index eb6a7ec..12b3f6e 100644 --- a/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift +++ b/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift @@ -21,6 +21,7 @@ private struct MockCustomNetworkResource: NetworkResourceType { let httpHeaderFields: [String: String]? let jsonBody: Any? let queryItems: [URLQueryItem]? + let requestTimeInterval: TimeInterval? = 27 init(url: URL, httpRequestMethod: HTTPMethod = .get, httpHeaderFields: [String : String]? = nil, jsonBody: Any? = nil, queryItems: [URLQueryItem]? = nil) { self.url = url @@ -35,13 +36,21 @@ class NetworkResourceTypeTests: XCTestCase { let url = URL(string: "www.test.com")! let urlWithQueryItem = URL(string: "www.test.com?query-name=query-value")! + // using the defaut time interval dynamically instead of hard-coding a value that may change at + // some point of time + private lazy var defaultRequestTimeOut: TimeInterval = { + let request = URLRequest(url: url) + return request.timeoutInterval + }() func test_correctDefaultValues() { let resource = MockDefaultNetworkResource(url: url) + let request = resource.urlRequest() XCTAssertEqual(resource.httpRequestMethod, HTTPMethod.get) XCTAssertEqual(resource.httpHeaderFields!, [:]) XCTAssertNil(resource.jsonBody) XCTAssertNil(resource.queryItems) + XCTAssertEqual(request?.timeoutInterval, defaultRequestTimeOut) } func test_urlRequest_allProperties() { @@ -59,6 +68,7 @@ class NetworkResourceTypeTests: XCTestCase { XCTAssertEqual(urlRequest!.allHTTPHeaderFields!, expectedAllHTTPHeaderFields) let expectedJSONData = try? JSONSerialization.data(withJSONObject: expectedJSONBody, options: JSONSerialization.WritingOptions.prettyPrinted) XCTAssertEqual(urlRequest!.httpBody, expectedJSONData) + XCTAssertEqual(urlRequest?.timeoutInterval, 27) } func test_urlRequest_resourceURLWithQueryParameters() { From c8abc6d3429ba2e5b9de606a103c472e4fd9e4bc Mon Sep 17 00:00:00 2001 From: Priya Marwaha Date: Fri, 3 Aug 2018 11:26:38 +0530 Subject: [PATCH 2/3] fixed indentation --- .../Protocols/NetworkResourceTypeTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift b/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift index 12b3f6e..94b857f 100644 --- a/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift +++ b/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift @@ -50,7 +50,7 @@ class NetworkResourceTypeTests: XCTestCase { XCTAssertEqual(resource.httpHeaderFields!, [:]) XCTAssertNil(resource.jsonBody) XCTAssertNil(resource.queryItems) - XCTAssertEqual(request?.timeoutInterval, defaultRequestTimeOut) + XCTAssertEqual(request?.timeoutInterval, defaultRequestTimeOut) } func test_urlRequest_allProperties() { From a4e80657c8b4c7fa45d622cc30f60fbf913120bd Mon Sep 17 00:00:00 2001 From: Priya Marwaha Date: Fri, 3 Aug 2018 11:42:53 +0530 Subject: [PATCH 3/3] fixed variable name --- CHANGELOG.md | 2 +- .../Protocols/Resource types/NetworkResourceType.swift | 6 +++--- .../Protocols/NetworkResourceTypeTests.swift | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d04b76a..2fe5a83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 7.2.2 -- Adds optional `requestTimeInterval` property to `NetworkResourceType` which can be used to set a `URLRequest` specific timeout for the resource. +- Adds optional `requestTimeoutInterval` property to `NetworkResourceType` which can be used to set a `URLRequest` specific timeout for the resource. ## 7.2.1 diff --git a/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift b/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift index 3510c99..84ad6a4 100644 --- a/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift +++ b/Sources/TABResourceLoader/Protocols/Resource types/NetworkResourceType.swift @@ -60,7 +60,7 @@ public protocol NetworkResourceType { var queryItems: [URLQueryItem]? { get } /// The time interval for the URLRequest for this resource - var requestTimeInterval: TimeInterval? { get } + var requestTimeoutInterval: TimeInterval? { get } /** Convenience function that builds a URLRequest for this resource @@ -77,7 +77,7 @@ public extension NetworkResourceType { public var httpHeaderFields: [String: String]? { return [:] } public var jsonBody: Any? { return nil } public var queryItems: [URLQueryItem]? { return nil } - public var requestTimeInterval: TimeInterval? { return nil } + public var requestTimeoutInterval: TimeInterval? { return nil } public func urlRequest() -> URLRequest? { var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) @@ -86,7 +86,7 @@ public extension NetworkResourceType { guard let urlFromComponents = urlComponents?.url else { return nil } var request: URLRequest - if let timeoutInterval = requestTimeInterval { + if let timeoutInterval = requestTimeoutInterval { request = URLRequest(url: urlFromComponents, timeoutInterval: timeoutInterval) } else { request = URLRequest(url: urlFromComponents) diff --git a/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift b/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift index 94b857f..1655c8b 100644 --- a/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift +++ b/Tests/TABResourceLoaderTests/Protocols/NetworkResourceTypeTests.swift @@ -21,7 +21,7 @@ private struct MockCustomNetworkResource: NetworkResourceType { let httpHeaderFields: [String: String]? let jsonBody: Any? let queryItems: [URLQueryItem]? - let requestTimeInterval: TimeInterval? = 27 + let requestTimeoutInterval: TimeInterval? = 27 init(url: URL, httpRequestMethod: HTTPMethod = .get, httpHeaderFields: [String : String]? = nil, jsonBody: Any? = nil, queryItems: [URLQueryItem]? = nil) { self.url = url