Skip to content

Commit

Permalink
Merge pull request #72 from CalvHobbes/feature/request-timeinterval
Browse files Browse the repository at this point in the history
 Add optional  requestTimeInterval  property to NetworkResourceType to set a URLRequest specific timeout
  • Loading branch information
samdods authored Aug 3, 2018
2 parents 3213ad2 + a4e8065 commit 197dc0b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 7.2.2

- Adds optional `requestTimeoutInterval` 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 requestTimeoutInterval: TimeInterval? { get }

/**
Convenience function that builds a URLRequest for this resource
Expand All @@ -74,15 +77,20 @@ public extension NetworkResourceType {
public var httpHeaderFields: [String: String]? { return [:] }
public var jsonBody: Any? { return nil }
public var queryItems: [URLQueryItem]? { return nil }
public var requestTimeoutInterval: TimeInterval? { return nil }

public func urlRequest() -> URLRequest? {
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)
let initialItems = urlComponents?.queryItems
urlComponents?.queryItems = allQueryItems(initialItems: initialItems)

guard let urlFromComponents = urlComponents?.url else { return nil }

var request = URLRequest(url: urlFromComponents)
var request: URLRequest
if let timeoutInterval = requestTimeoutInterval {
request = URLRequest(url: urlFromComponents, timeoutInterval: timeoutInterval)
} else {
request = URLRequest(url: urlFromComponents)
}
request.allHTTPHeaderFields = httpHeaderFields
request.httpMethod = httpRequestMethod.rawValue

Expand Down
2 changes: 1 addition & 1 deletion TABResourceLoader.podspec
Original file line number Diff line number Diff line change
@@ -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' => '[email protected]' }
spec.summary = 'Framework for loading resources from a network service'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private struct MockCustomNetworkResource: NetworkResourceType {
let httpHeaderFields: [String: String]?
let jsonBody: Any?
let queryItems: [URLQueryItem]?
let requestTimeoutInterval: TimeInterval? = 27

init(url: URL, httpRequestMethod: HTTPMethod = .get, httpHeaderFields: [String : String]? = nil, jsonBody: Any? = nil, queryItems: [URLQueryItem]? = nil) {
self.url = url
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 197dc0b

Please sign in to comment.