From 0251dff4ced9c6b05a63d9cc1055cb8e3d6e7fc8 Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 18 Jul 2018 17:30:03 -0700 Subject: [PATCH 01/11] 'Cancel' for PromiseKit --- Cartfile.resolved | 2 +- Sources/NSURLSession+OMG+Promise.swift | 153 +++++++++++++++++++++++++ Tests/TestNSURLSession.swift | 78 +++++++++++++ 3 files changed, 232 insertions(+), 1 deletion(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index ebbdb14..14f3b67 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" github "PromiseKit/Foundation" "3.1.0" github "mxcl/OMGHTTPURLRQ" "3.2.5" -github "mxcl/PromiseKit" "6.3.3" +github "mxcl/PromiseKit" "6.3.4" diff --git a/Sources/NSURLSession+OMG+Promise.swift b/Sources/NSURLSession+OMG+Promise.swift index eca8512..bbd1184 100644 --- a/Sources/NSURLSession+OMG+Promise.swift +++ b/Sources/NSURLSession+OMG+Promise.swift @@ -168,3 +168,156 @@ extension URLSession { } } } + +//////////////////////////////////////////////////////////// Cancellation + +extension URLSession { + /** + Makes a cancellable **GET** request to the provided URL. + + let p = URLSession.shared.GET("http://example.com", query: ["foo": "bar"]) + p.then { data -> Void in + //… + } + p.asImage().then { image -> Void in + //… + } + p.asDictionary().then { json -> Void in + //… + } + + - Parameter url: The URL to request. + - Parameter query: The parameters to be encoded as the query string for the GET request. + - Returns: A cancellable promise that represents the GET request. + */ + public func GETCC(_ url: String, query: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.get(url, query) as URLRequest) + } + + /** + Makes a cancellable POST request to the provided URL passing form URL encoded + parameters. + + Form URL-encoding is the standard way to POST on the Internet, so + probably this is what you want. If it doesn’t work, try the `+POST:JSON` + variant. + + let url = "http://jsonplaceholder.typicode.com/posts" + let params = ["title": "foo", "body": "bar", "userId": 1] + URLSession.shared.POST(url, formData: params).asDictionary().then { json -> Void in + //… + } + + - Parameter url: The URL to request. + - Parameter formData: The parameters to be form URL-encoded and passed as the POST body. + - Returns: A cancellable promise that represents the POST request. + */ + public func POSTCC(_ url: String, formData: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.post(url, formData) as URLRequest) + } + + /** + Makes a cancellable POST request to the provided URL passing multipart form-data. + + let formData = OMGMultipartFormData() + let imgData = Data(contentsOfFile: "image.png") + formData.addFile(imgdata, parameterName: "file1", filename: "myimage1.png", contentType: "image/png") + + URLSession.shared.POST(url, multipartFormData: formData).then { data in + //… + } + + - Parameter url: The URL to request. + - Parameter multipartFormData: The parameters to be multipart form-data encoded and passed as the POST body. + - Returns: A cancellable promise that represents the POST request. + - SeeAlso: [https://github.com/mxcl/OMGHTTPURLRQ](OMGHTTPURLRQ) + */ + public func POSTCC(_ url: String, multipartFormData: OMGMultipartFormData) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.post(url, multipartFormData) as URLRequest) + } + + /** + Makes a cancellable POST request to the provided URL passing JSON encoded + parameters. + + Most web servers nowadays support POST with either JSON or form + URL-encoding. If in doubt try form URL-encoded parameters first. + + let url = "http://jsonplaceholder.typicode.com/posts" + let params = ["title": "foo", "body": "bar", "userId": 1] + URLSession.shared.POST(url, json: params).asDictionary().then { json -> Void in + //… + } + + - Parameter url: The URL to request. + - Parameter json: The parameters to be JSON-encoded and passed as the POST body. + - Returns: A cancellable promise that represents the POST request. + */ + public func POSTCC(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.post(url, json: json) as URLRequest) + } + + /** + Makes a cancellable PUT request to the provided URL passing JSON encoded parameters. + + let url = "http://jsonplaceholder.typicode.com/posts" + let params = ["title": "foo", "body": "bar", "userId": 1] + URLSession.shared.PUT(url, json: params).asDictionary().then { json -> Void in + //… + } + + - Parameter url: The URL to request. + - Parameter json: The parameters to be JSON-encoded and passed as the PUT body. + - Returns: A cancellable promise that represents the PUT request. + */ + public func PUTCC(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.put(url, json: json) as URLRequest) + } + + /** + Makes a cancellable DELETE request to the provided URL passing form URL-encoded + parameters. + + let url = "http://jsonplaceholder.typicode.com/posts/1" + URLSession.shared.DELETE(url).then.asDictionary() { json -> Void in + //… + } + + - Parameter url: The URL to request. + - Returns: A cancellable promise that represents the PUT request. + */ + public func DELETECC(_ url: String) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.delete(url, nil) as URLRequest) + } + + /** + Makes a cancellable PATCH request to the provided URL passing the provided JSON parameters. + + let url = "http://jsonplaceholder.typicode.com/posts/1" + let params = ["foo": "bar"] + NSURLConnection.PATCH(url, json: params).asDictionary().then { json -> Void in + //… + } + - Parameter url: The URL to request. + - Parameter json: The JSON parameters to encode as the PATCH body. + - Returns: A cancellable promise that represents the PUT request. + */ + public func PATCHCC(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return startCC(try OMGHTTPURLRQ.patch(url, json: json) as URLRequest) + } + + private func startCC(_ body: @autoclosure () throws -> URLRequest) -> CancellablePromise<(data: Data, response: URLResponse)> { + do { + var request = try body() + + if request.value(forHTTPHeaderField: "User-Agent") == nil { + request.setValue(OMGUserAgent(), forHTTPHeaderField: "User-Agent") + } + + return dataTaskCC(.promise, with: request) + } catch { + return CancellablePromise(error: error) + } + } +} + diff --git a/Tests/TestNSURLSession.swift b/Tests/TestNSURLSession.swift index 8e3d77e..8c089b9 100644 --- a/Tests/TestNSURLSession.swift +++ b/Tests/TestNSURLSession.swift @@ -72,3 +72,81 @@ class NSURLSessionTests: XCTestCase { OHHTTPStubs.removeAllStubs() } } + +//////////////////////////////////////////////////////////// Cancellation + +extension NSURLSessionTests { + func testCancel1() { + let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]] + + OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in + return OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil) + } + + let ex = expectation(description: "") + URLSession.shared.GETCC("http://example.com").compactMap { + XCTFail() + try JSONSerialization.jsonObject(with: $0.data) + }.done { + XCTAssertEqual(json, $0 as? NSDictionary) + XCTFail() + }.catch(policy: .allErrors) { + $0.isCancelled ? ex.fulfill() : XCTFail() + }.cancel() + waitForExpectations(timeout: 1) + } + + func testCancel2() { + + // test that Promise chains thens + // this test because I don’t trust the Swift compiler + + let dummy = ("fred" as NSString).data(using: String.Encoding.utf8.rawValue)! + + OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in + return OHHTTPStubsResponse(data: dummy, statusCode: 200, headers: [:]) + } + + let ex = expectation(description: "") + + afterCC(seconds: 0.1).then { () -> CancellablePromise<(data: Data, response: URLResponse)> in + let p = URLSession.shared.GETCC("http://example.com") + p.cancel() + return p + }.done { + XCTAssertEqual($0.data, dummy) + XCTFail() + }.catch(policy: .allErrors) { + $0.isCancelled ? ex.fulfill() : XCTFail() + } + + waitForExpectations(timeout: 1) + } + + func testCancelSyntax() { + let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]] + + OHHTTPStubs.stubRequests(passingTest: { + $0.url!.host == "example.com" + }, withStubResponse: { _ in + OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil) + }) + + let p = URLSession.shared.GETCC("http://example.com", query: [ + "1": 1, + "2": 2 + ]) + + let ex = expectation(description: "") + p.compactMap { + p.cancel() + try JSONSerialization.jsonObject(with: $0.data) + }.done { + XCTAssertEqual(json, $0 as? NSDictionary) + XCTFail() + }.catch(policy: .allErrors) { + $0.isCancelled ? ex.fulfill() : XCTFail() + } + waitForExpectations(timeout: 1) + } +} From 0acf1f6330b45228068a3aa17fb547f7601f0c83 Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 18 Jul 2018 19:21:25 -0700 Subject: [PATCH 02/11] 'Cancel' for PromiseKit -- fix Travis build -- update carthage PromiseKit dependencies for all extensions to temporarily point at the PromiseKit branch containing the cancellable classes --- Cartfile | 6 ++++-- Cartfile.resolved | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Cartfile b/Cartfile index 62674f6..c05eee3 100644 --- a/Cartfile +++ b/Cartfile @@ -1,3 +1,5 @@ -github "mxcl/PromiseKit" ~> 6.3 +#github "mxcl/PromiseKit" ~> 6.3 +github "dougzilla32/PromiseKit" "CoreCancel" github "mxcl/OMGHTTPURLRQ" ~> 3.2 -github "PromiseKit/Foundation" ~> 3.1 +#github "PromiseKit/Foundation" ~> 3.1 +github "dougzilla32/Foundation" "CoreCancel" diff --git a/Cartfile.resolved b/Cartfile.resolved index 14f3b67..74365af 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,5 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "PromiseKit/Foundation" "3.1.0" +github "dougzilla32/Foundation" "7fdbd977b1f7137a19bd5847081336c413f81b68" +github "dougzilla32/PromiseKit" "e858d54f6bcb83a435c3ec8016c9a6c2f6befeb2" github "mxcl/OMGHTTPURLRQ" "3.2.5" github "mxcl/PromiseKit" "6.3.4" From 92a47bd3f36aea57e396e73366e129876ece27bc Mon Sep 17 00:00:00 2001 From: Doug Date: Thu, 19 Jul 2018 12:56:17 -0700 Subject: [PATCH 03/11] 'Cancel' for PromiseKit -- for OMGHTTPURLRQ point Cartfile.resolved at the forked version of PromiseKit --- Cartfile.resolved | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 74365af..6e095f7 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,5 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "7fdbd977b1f7137a19bd5847081336c413f81b68" -github "dougzilla32/PromiseKit" "e858d54f6bcb83a435c3ec8016c9a6c2f6befeb2" +github "dougzilla32/Foundation" "cb9088a9f486edb4c40751f63c21fcf077fb54f1" +github "dougzilla32/PromiseKit" "1e5519010c2b519f03a306f023accc93b46e2bf3" github "mxcl/OMGHTTPURLRQ" "3.2.5" -github "mxcl/PromiseKit" "6.3.4" From f3729eeaa75848678740debfc8a0eedacfb3b75f Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Sat, 25 Aug 2018 23:55:09 -0700 Subject: [PATCH 04/11] 'Cancel' for PromiseKit -- * Delete CancellableGuarantee and all its usages * In PromiseKit Core: instead of using additional functions and methods with the 'CC' suffix, created a new 'cancellable' function to wrap any Promise or Guarantee * In the extensions: Rename methods with 'CC' suffix to 'cancellable' prefix (may revisit this) * Fix the CancellableCatchable 'recover' methods to properly handle cancellation errors * Added an 'afterTask' property to Guarantee, to keep track of the task for calls to 'after'. This way the timer can be appropriately cancelled if the returned Guarantee is passed to the new 'cancellable' function. (may revisit this) * Update all cancellable tests for the cancellable API changes --- Sources/NSURLSession+OMG+Promise.swift | 32 +++++++++++++------------- Tests/TestNSURLSession.swift | 8 +++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Sources/NSURLSession+OMG+Promise.swift b/Sources/NSURLSession+OMG+Promise.swift index bbd1184..dec4017 100644 --- a/Sources/NSURLSession+OMG+Promise.swift +++ b/Sources/NSURLSession+OMG+Promise.swift @@ -190,8 +190,8 @@ extension URLSession { - Parameter query: The parameters to be encoded as the query string for the GET request. - Returns: A cancellable promise that represents the GET request. */ - public func GETCC(_ url: String, query: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.get(url, query) as URLRequest) + public func cancellableGET(_ url: String, query: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.get(url, query) as URLRequest) } /** @@ -212,8 +212,8 @@ extension URLSession { - Parameter formData: The parameters to be form URL-encoded and passed as the POST body. - Returns: A cancellable promise that represents the POST request. */ - public func POSTCC(_ url: String, formData: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.post(url, formData) as URLRequest) + public func cancellablePOST(_ url: String, formData: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.post(url, formData) as URLRequest) } /** @@ -232,8 +232,8 @@ extension URLSession { - Returns: A cancellable promise that represents the POST request. - SeeAlso: [https://github.com/mxcl/OMGHTTPURLRQ](OMGHTTPURLRQ) */ - public func POSTCC(_ url: String, multipartFormData: OMGMultipartFormData) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.post(url, multipartFormData) as URLRequest) + public func cancellablePOST(_ url: String, multipartFormData: OMGMultipartFormData) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.post(url, multipartFormData) as URLRequest) } /** @@ -253,8 +253,8 @@ extension URLSession { - Parameter json: The parameters to be JSON-encoded and passed as the POST body. - Returns: A cancellable promise that represents the POST request. */ - public func POSTCC(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.post(url, json: json) as URLRequest) + public func cancellablePOST(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.post(url, json: json) as URLRequest) } /** @@ -270,8 +270,8 @@ extension URLSession { - Parameter json: The parameters to be JSON-encoded and passed as the PUT body. - Returns: A cancellable promise that represents the PUT request. */ - public func PUTCC(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.put(url, json: json) as URLRequest) + public func cancellablePUT(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.put(url, json: json) as URLRequest) } /** @@ -286,8 +286,8 @@ extension URLSession { - Parameter url: The URL to request. - Returns: A cancellable promise that represents the PUT request. */ - public func DELETECC(_ url: String) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.delete(url, nil) as URLRequest) + public func cancellableDELETE(_ url: String) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.delete(url, nil) as URLRequest) } /** @@ -302,11 +302,11 @@ extension URLSession { - Parameter json: The JSON parameters to encode as the PATCH body. - Returns: A cancellable promise that represents the PUT request. */ - public func PATCHCC(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return startCC(try OMGHTTPURLRQ.patch(url, json: json) as URLRequest) + public func cancellablePATCH(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { + return cancellableStart(try OMGHTTPURLRQ.patch(url, json: json) as URLRequest) } - private func startCC(_ body: @autoclosure () throws -> URLRequest) -> CancellablePromise<(data: Data, response: URLResponse)> { + private func cancellableStart(_ body: @autoclosure () throws -> URLRequest) -> CancellablePromise<(data: Data, response: URLResponse)> { do { var request = try body() @@ -314,7 +314,7 @@ extension URLSession { request.setValue(OMGUserAgent(), forHTTPHeaderField: "User-Agent") } - return dataTaskCC(.promise, with: request) + return cancellableDataTask(.promise, with: request) } catch { return CancellablePromise(error: error) } diff --git a/Tests/TestNSURLSession.swift b/Tests/TestNSURLSession.swift index 8c089b9..f9a60ac 100644 --- a/Tests/TestNSURLSession.swift +++ b/Tests/TestNSURLSession.swift @@ -84,7 +84,7 @@ extension NSURLSessionTests { } let ex = expectation(description: "") - URLSession.shared.GETCC("http://example.com").compactMap { + URLSession.shared.cancellableGET("http://example.com").compactMap { XCTFail() try JSONSerialization.jsonObject(with: $0.data) }.done { @@ -109,8 +109,8 @@ extension NSURLSessionTests { let ex = expectation(description: "") - afterCC(seconds: 0.1).then { () -> CancellablePromise<(data: Data, response: URLResponse)> in - let p = URLSession.shared.GETCC("http://example.com") + cancellable(after(seconds: 0.1)).then { () -> CancellablePromise<(data: Data, response: URLResponse)> in + let p = URLSession.shared.cancellableGET("http://example.com") p.cancel() return p }.done { @@ -132,7 +132,7 @@ extension NSURLSessionTests { OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil) }) - let p = URLSession.shared.GETCC("http://example.com", query: [ + let p = URLSession.shared.cancellableGET("http://example.com", query: [ "1": 1, "2": 2 ]) From f9d38269b001603361a7a7f00212e9e6bb2c02e1 Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Mon, 27 Aug 2018 23:21:00 -0700 Subject: [PATCH 05/11] 'Cancel' for PromiseKit -- update Cartfile.resolved for all extensions to point at the latest version of the PromiseKit 'CoreCancel' branch --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 6e095f7..2bfc345 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "cb9088a9f486edb4c40751f63c21fcf077fb54f1" -github "dougzilla32/PromiseKit" "1e5519010c2b519f03a306f023accc93b46e2bf3" +github "dougzilla32/Foundation" "fd1d66c24d9ac6b6878603e16356d8c9cac46dc8" +github "dougzilla32/PromiseKit" "5069bdc1b10847d47f5fdbe3accb623b3b1908a3" github "mxcl/OMGHTTPURLRQ" "3.2.5" From 3d2457131fdc2b2dddaeaa441f5670ea8de5073f Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Thu, 6 Sep 2018 22:00:52 -0700 Subject: [PATCH 06/11] 'Cancel' for PromiseKit -- * Eliminate duplicate code in the cancellable extensions * When using the 'cancellable' function with the extensions, ensure that the underlying task (if any) is always cancelled * When using the 'cancellable' function, call 'reject' on the underlying promise (wherever possible) if 'cancel' is invoked * Create cancellable wrappers for the extensions only for the cases where there is an underlying task that supports cancellation --- Sources/NSURLSession+OMG+Promise.swift | 31 +++++++------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/Sources/NSURLSession+OMG+Promise.swift b/Sources/NSURLSession+OMG+Promise.swift index dec4017..0e4b921 100644 --- a/Sources/NSURLSession+OMG+Promise.swift +++ b/Sources/NSURLSession+OMG+Promise.swift @@ -169,7 +169,7 @@ extension URLSession { } } -//////////////////////////////////////////////////////////// Cancellation +//////////////////////////////////////////////////////////// Cancellable wrappers extension URLSession { /** @@ -191,7 +191,7 @@ extension URLSession { - Returns: A cancellable promise that represents the GET request. */ public func cancellableGET(_ url: String, query: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.get(url, query) as URLRequest) + return cancellable(GET(url, query: query)) } /** @@ -213,7 +213,7 @@ extension URLSession { - Returns: A cancellable promise that represents the POST request. */ public func cancellablePOST(_ url: String, formData: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.post(url, formData) as URLRequest) + return cancellable(POST(url, formData: formData)) } /** @@ -233,7 +233,7 @@ extension URLSession { - SeeAlso: [https://github.com/mxcl/OMGHTTPURLRQ](OMGHTTPURLRQ) */ public func cancellablePOST(_ url: String, multipartFormData: OMGMultipartFormData) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.post(url, multipartFormData) as URLRequest) + return cancellable(POST(url, multipartFormData: multipartFormData)) } /** @@ -254,7 +254,7 @@ extension URLSession { - Returns: A cancellable promise that represents the POST request. */ public func cancellablePOST(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.post(url, json: json) as URLRequest) + return cancellable(POST(url, json: json)) } /** @@ -271,7 +271,7 @@ extension URLSession { - Returns: A cancellable promise that represents the PUT request. */ public func cancellablePUT(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.put(url, json: json) as URLRequest) + return cancellable(PUT(url, json: json)) } /** @@ -287,7 +287,7 @@ extension URLSession { - Returns: A cancellable promise that represents the PUT request. */ public func cancellableDELETE(_ url: String) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.delete(url, nil) as URLRequest) + return cancellable(DELETE(url)) } /** @@ -303,21 +303,6 @@ extension URLSession { - Returns: A cancellable promise that represents the PUT request. */ public func cancellablePATCH(_ url: String, json: [String: Any]? = nil) -> CancellablePromise<(data: Data, response: URLResponse)> { - return cancellableStart(try OMGHTTPURLRQ.patch(url, json: json) as URLRequest) - } - - private func cancellableStart(_ body: @autoclosure () throws -> URLRequest) -> CancellablePromise<(data: Data, response: URLResponse)> { - do { - var request = try body() - - if request.value(forHTTPHeaderField: "User-Agent") == nil { - request.setValue(OMGUserAgent(), forHTTPHeaderField: "User-Agent") - } - - return cancellableDataTask(.promise, with: request) - } catch { - return CancellablePromise(error: error) - } + return cancellable(PATCH(url, json: json)) } } - From 11a214c9ac851c5f451b0069146b155116f55316 Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Thu, 6 Sep 2018 22:32:29 -0700 Subject: [PATCH 07/11] 'Cancel' for PromiseKit -- * update extensions to point at the latest version of PromiseKit in the pull request * fix "generic parameter 'T' could not be inferred" compilation errors when using the new 'Promise' initializer that takes a cancellable task --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 2bfc345..3a5a35d 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "fd1d66c24d9ac6b6878603e16356d8c9cac46dc8" -github "dougzilla32/PromiseKit" "5069bdc1b10847d47f5fdbe3accb623b3b1908a3" +github "dougzilla32/Foundation" "194f0512d91ff311223d094fdbe80d04d1a57af9" +github "dougzilla32/PromiseKit" "dcf77e0afe55e3d18da9a2a5bda28cdf1adcb5a9" github "mxcl/OMGHTTPURLRQ" "3.2.5" From 67ce692764cac941464bbc1ee2ebace34c2e128b Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Fri, 7 Sep 2018 12:55:13 -0700 Subject: [PATCH 08/11] 'Cancel' for PromiseKit -- update Cartfile.resolved in all extensions to point at the latest PromiseKit --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 3a5a35d..7f66dad 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "194f0512d91ff311223d094fdbe80d04d1a57af9" -github "dougzilla32/PromiseKit" "dcf77e0afe55e3d18da9a2a5bda28cdf1adcb5a9" +github "dougzilla32/Foundation" "6c9c9d0018d1faf82ceca0b9892b27c37820fbc0" +github "dougzilla32/PromiseKit" "ff694600d4d03458121515bdc027ba76df14f7ef" github "mxcl/OMGHTTPURLRQ" "3.2.5" From d9b6bf6b2445660782050cdf0c0cb59de63f0a1e Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Sun, 23 Sep 2018 11:47:49 +0900 Subject: [PATCH 09/11] 'Cancel' for PromiseKit -- update Cartfile.resolved for all extensions to point at the current PromiseKit --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 7f66dad..614696c 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "6c9c9d0018d1faf82ceca0b9892b27c37820fbc0" -github "dougzilla32/PromiseKit" "ff694600d4d03458121515bdc027ba76df14f7ef" +github "dougzilla32/Foundation" "26c196c1a3cc85bf38802ab86a6477d649ef21ad" +github "dougzilla32/PromiseKit" "3be75b2bfd08e32c118ad9dc9c7837111712e03a" github "mxcl/OMGHTTPURLRQ" "3.2.5" From 9049a6071354ca7da90f2c0bb5ab333159461b63 Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Sun, 23 Sep 2018 13:01:36 +0900 Subject: [PATCH 10/11] 'Cancel' for PromiseKit -- update Carthage.resolved files to point at latest version of PromiseKit --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 614696c..96b00e2 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "26c196c1a3cc85bf38802ab86a6477d649ef21ad" -github "dougzilla32/PromiseKit" "3be75b2bfd08e32c118ad9dc9c7837111712e03a" +github "dougzilla32/Foundation" "87699e7591b16f78410ccebe7464f41daf98fbf3" +github "dougzilla32/PromiseKit" "d1299007c75f7f31ebeccd9a317f2c46fdeb04f3" github "mxcl/OMGHTTPURLRQ" "3.2.5" From 89d22857ee2acd4885d0ffc548ef93b0e3589e83 Mon Sep 17 00:00:00 2001 From: dougzilla32 Date: Tue, 25 Sep 2018 13:01:01 +0900 Subject: [PATCH 11/11] 'Cancel' for PromiseKit -- update Carthage.resolved files to point at the latest PromiseKit --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 96b00e2..afccdca 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,4 @@ github "AliSoftware/OHHTTPStubs" "6.1.0" -github "dougzilla32/Foundation" "87699e7591b16f78410ccebe7464f41daf98fbf3" -github "dougzilla32/PromiseKit" "d1299007c75f7f31ebeccd9a317f2c46fdeb04f3" +github "dougzilla32/Foundation" "4b6d86210cf2f3a2010d37c44d7d63ccb20547b1" +github "dougzilla32/PromiseKit" "288f7fbabc0b33c558bf908a3a0770693223d4e0" github "mxcl/OMGHTTPURLRQ" "3.2.5"