Skip to content

Commit 4c8a5bb

Browse files
committed
Add another parameter to the HTTPManager.MetricsCallback callback
In order to retain backwards compatibility, the existing initializer and property were deprecated and a new initializer and property with different names were added. Fixes #41.
1 parent 2198c6a commit 4c8a5bb

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ work by you shall be dual licensed as above, without any additional terms or con
413413
#### Development
414414

415415
* Expose `HTTPManagerTask.userInitiated` as a public property.
416+
* Add another parameter to the `HTTPManager.MetricsCallback` callback. In order to retain backwards compatibility, the old initializer and property were deprecated and a new initializer and property were added with different names.
416417

417418
#### v4.2.0 (2018-07-10)
418419

Sources/HTTPManager.swift

+37-8
Original file line numberDiff line numberDiff line change
@@ -1611,20 +1611,49 @@ public final class HTTPManagerMetricsCallback: NSObject {
16111611

16121612
/// The callback that will be invoked with task metrics.
16131613
/// - Parameter task: The task for which metrics were collected.
1614+
/// - Parameter urlTask: The underlying network task for which metrics were collected.
1615+
/// This may be different than `task.networkTask` if the task is configured with
1616+
/// automatic retry behavior.
16141617
/// - Parameter metrics: The task metrics that were collected.
1615-
@objc public let callback: (_ task: HTTPManagerTask, _ metrics: URLSessionTaskMetrics) -> Void
1618+
@objc public let handler: (_ task: HTTPManagerTask, _ urlTask: URLSessionTask, _ metrics: URLSessionTaskMetrics) -> Void
1619+
1620+
/// The callback that will be invoked with task metrics.
1621+
/// - Parameter task: The task for which metrics were collected.
1622+
/// - Parameter metrics: The task metrics that were collected.
1623+
@available(*, deprecated, message: "Use .handler instead")
1624+
@objc public var callback: (_ task: HTTPManagerTask, _ metrics: URLSessionTaskMetrics) -> Void {
1625+
return _callback ?? { [handler] (task, metrics) in
1626+
handler(task, task.networkTask, metrics)
1627+
}
1628+
}
1629+
private var _callback: ((_ task: HTTPManagerTask, _ metrics: URLSessionTaskMetrics) -> Void)?
16161630

16171631
/// Returns a new `HTTPManagerMetricsCallback` object.
16181632
/// - Parameter queue: The operation queue that the callback will be invoked on. If `nil`, the
16191633
/// callback will be invoked on a global background queue with the `.utility` QoS.
1620-
/// - Parameter callback: The callback that will be invoked with task metrics.
1634+
/// - Parameter handler: The callback that will be invoked with task metrics.
16211635
/// - Parameter task: The task for which metrics were collected.
1636+
/// - Parameter urlTask: The underlying network task for which metrics were collected.
1637+
/// This may be different than `task.networkTask` if the task is configured with
1638+
/// automatic retry behavior.
16221639
/// - Parameter metrics: The task metrics that were collected.
1623-
@objc public init(queue: OperationQueue?, callback: @escaping (_ task: HTTPManagerTask, _ metrics: URLSessionTaskMetrics) -> Void) {
1640+
@objc public init(queue: OperationQueue?, handler: @escaping (_ task: HTTPManagerTask, _ urlTask: URLSessionTask, _ metrics: URLSessionTaskMetrics) -> Void) {
16241641
self.queue = queue
1625-
self.callback = callback
1642+
self.handler = handler
16261643
super.init()
16271644
}
1645+
1646+
/// Returns a new `HTTPManagerMetricsCallback` object.
1647+
/// - Parameter queue: The operation queue that the callback will be invoked on. If `nil`, the
1648+
/// callback will be invoked on a global background queue with the `.utility` QoS.
1649+
/// - Parameter callback: The callback that will be invoked with task metrics.
1650+
/// - Parameter task: The task for which metrics were collected.
1651+
/// - Parameter metrics: The task metrics that were collected.
1652+
@available(*, deprecated, message: "Use init(queue:handler:) instead")
1653+
@objc public convenience init(queue: OperationQueue?, callback: @escaping (_ task: HTTPManagerTask, _ metrics: URLSessionTaskMetrics) -> Void) {
1654+
self.init(queue: queue, handler: { (task, urlTask, metrics) in callback(task, metrics) })
1655+
_callback = callback
1656+
}
16281657
}
16291658

16301659
// MARK: - Private
@@ -2267,12 +2296,12 @@ extension MetricsSessionDelegate {
22672296
assert(apiTask.networkTask === task, "internal HTTPManager error: taskInfo out of sync")
22682297
log("task:didFinishCollecting for task \(task)")
22692298
if let operationQueue = metricsCallback.queue {
2270-
operationQueue.addOperation { [callback=metricsCallback.callback] in
2271-
callback(apiTask, metrics)
2299+
operationQueue.addOperation { [callback=metricsCallback.handler] in
2300+
callback(apiTask, task, metrics)
22722301
}
22732302
} else {
2274-
DispatchQueue.global(qos: .utility).async { [callback=metricsCallback.callback] in
2275-
callback(apiTask, metrics)
2303+
DispatchQueue.global(qos: .utility).async { [callback=metricsCallback.handler] in
2304+
callback(apiTask, task, metrics)
22762305
}
22772306
}
22782307
}

Tests/MetricsCallbackTests.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
3030
}
3131
var task: HTTPManagerTask?
3232
let expectation = self.expectation(description: "task metrics")
33-
HTTP.metricsCallback = .init(queue: nil, callback: { (task_, metrics) in
33+
HTTP.metricsCallback = .init(queue: nil, handler: { (task_, networkTask, metrics) in
3434
XCTAssert(task === task_, "unexpected task")
3535
expectation.fulfill()
3636
})
@@ -48,7 +48,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
4848
var task: HTTPManagerTask?
4949
let operationQueue = OperationQueue()
5050
let expectation = self.expectation(description: "task metrics")
51-
HTTP.metricsCallback = .init(queue: operationQueue, callback: { (task_, metrics) in
51+
HTTP.metricsCallback = .init(queue: operationQueue, handler: { (task_, networkTask, metrics) in
5252
XCTAssertEqual(OperationQueue.current, operationQueue)
5353
XCTAssert(task === task_, "unexpected task")
5454
expectation.fulfill()
@@ -70,7 +70,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
7070
var requestFinished = false
7171
var task: HTTPManagerTask?
7272
let expectation = self.expectation(description: "task metrics")
73-
HTTP.metricsCallback = .init(queue: operationQueue, callback: { (task_, metrics) in
73+
HTTP.metricsCallback = .init(queue: operationQueue, handler: { (task_, networkTask, metrics) in
7474
XCTAssertEqual(OperationQueue.current, operationQueue)
7575
XCTAssert(task === task_, "unexpected task")
7676
XCTAssertFalse(requestFinished, "recieved metrics after task finished")
@@ -107,7 +107,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
107107
let expectation = self.expectation(description: "task metrics")
108108
expectation.expectedFulfillmentCount = 3
109109
expectation.assertForOverFulfill = true
110-
HTTP.metricsCallback = .init(queue: operationQueue, callback: { (task_, metrics) in
110+
HTTP.metricsCallback = .init(queue: operationQueue, handler: { (task_, networkTask, metrics) in
111111
XCTAssert(task === task_, "unexpected task")
112112
expectation.fulfill()
113113
})
@@ -125,7 +125,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
125125
completionHandler(HTTPServer.Response(status: .ok))
126126
}
127127
expectationForRequestSuccess(HTTP.request(GET: "foo"), startAutomatically: true)
128-
HTTP.metricsCallback = .init(queue: nil, callback: { (_, _) in }) // this resets the session asynchronously
128+
HTTP.metricsCallback = .init(queue: nil, handler: { (_, _, _) in }) // this resets the session asynchronously
129129
_ = HTTP.sessionConfiguration // this waits for the session reset to complete
130130
sema.signal()
131131
waitForExpectations(timeout: 1, handler: nil)
@@ -144,7 +144,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
144144
expectationForRequestSuccess(HTTP.request(GET: "foo"), queue: operationQueue, startAutomatically: true)
145145
let expectation = XCTestExpectation(description: "task metrics")
146146
expectation.isInverted = true
147-
HTTP.metricsCallback = .init(queue: operationQueue, callback: { (_, _) in
147+
HTTP.metricsCallback = .init(queue: operationQueue, handler: { (_, _, _) in
148148
expectation.fulfill()
149149
})
150150
sema.signal()
@@ -160,12 +160,12 @@ final class MetricsCallbackTests: PMHTTPTestCase {
160160
}
161161
let expectationFirst = XCTestExpectation(description: "first task metrics")
162162
expectationFirst.isInverted = true
163-
HTTP.metricsCallback = .init(queue: nil, callback: { (task_, metrics) in
163+
HTTP.metricsCallback = .init(queue: nil, handler: { (task_, networkTask, metrics) in
164164
expectationFirst.fulfill()
165165
})
166166
let task = expectationForRequestSuccess(HTTP.request(GET: "foo"), startAutomatically: false)
167167
let expectationSecond = self.expectation(description: "second task metrics")
168-
HTTP.metricsCallback = .init(queue: nil, callback: { (task_, metrics) in
168+
HTTP.metricsCallback = .init(queue: nil, handler: { (task_, networkTask, metrics) in
169169
XCTAssert(task === task_, "unexpected task")
170170
expectationSecond.fulfill()
171171
})
@@ -184,7 +184,7 @@ final class MetricsCallbackTests: PMHTTPTestCase {
184184
}
185185
let expectation = XCTestExpectation(description: "task metrics")
186186
expectation.isInverted = true
187-
HTTP.metricsCallback = .init(queue: nil, callback: { (_, _) in
187+
HTTP.metricsCallback = .init(queue: nil, handler: { (_, _, _) in
188188
expectation.fulfill()
189189
})
190190
let operationQueue = OperationQueue()

0 commit comments

Comments
 (0)