@@ -17,9 +17,10 @@ enum HttpIoError : Error {
1717
1818public struct HttpIo : NetworkIo {
1919
20- public func receiveFrom( _ source: String , completionHandler: @escaping ( Data ) throws -> Void ) throws {
20+ public func receiveFrom( _ source: String , completionHandler: @escaping ( Data ) throws -> Void ) throws -> CancellableRequest {
2121 guard let url = URL ( string: source) else { throw HttpIoError . urlError ( " Invalid URL " ) }
2222 print ( " HttpIo receiveFrom url is \( url) " )
23+
2324 let task = URLSession . shared. dataTask ( with: url) {
2425 ( data: Data ? , response: URLResponse ? , error: Error ? ) in
2526
@@ -35,14 +36,16 @@ public struct HttpIo : NetworkIo {
3536 print ( " Error " , error, " in completionHandler passed to fetchData " )
3637 }
3738 }
38-
39+
3940 task. resume ( )
41+
42+ return CancellableDataTask ( request: task)
4043 }
4144
4245
4346 public func streamFrom( _ source: String ,
4447 updateHandler: @escaping ( Data , URLSessionDataTask ) throws -> Bool ,
45- completionHandler: @escaping ( AnyObject ) throws -> Void ) throws {
48+ completionHandler: @escaping ( AnyObject ) throws -> Void ) throws -> CancellableRequest {
4649
4750 guard let url = URL ( string: source) else { throw HttpIoError . urlError ( " Invalid URL " ) }
4851 let config = URLSessionConfiguration . default
@@ -51,23 +54,24 @@ public struct HttpIo : NetworkIo {
5154 let task = session. dataTask ( with: url)
5255
5356 task. resume ( )
57+ return CancellableDataTask ( request: task)
5458 }
5559
56- public func sendTo( _ target: String , content: Data , completionHandler: @escaping ( Data ) -> Void ) throws {
60+ public func sendTo( _ target: String , content: Data , completionHandler: @escaping ( Data ) -> Void ) throws -> CancellableRequest {
5761
5862 var multipart = try Multipart ( targetUrl: target, encoding: . utf8)
5963 multipart = try Multipart . addFilePart ( multipart, fileName: nil , fileData: content)
60- Multipart . finishMultipart ( multipart, completionHandler: completionHandler)
64+ return Multipart . finishMultipart ( multipart, completionHandler: completionHandler)
6165 }
6266
6367
64- public func sendTo( _ target: String , filePath: String , completionHandler: @escaping ( Data ) -> Void ) throws {
68+ public func sendTo( _ target: String , filePath: String , completionHandler: @escaping ( Data ) -> Void ) throws -> CancellableRequest {
6569
6670 var multipart = try Multipart ( targetUrl: target, encoding: . utf8)
6771
6872 multipart = try handle ( oldMultipart: multipart, files: [ filePath] )
6973
70- Multipart . finishMultipart ( multipart, completionHandler: completionHandler)
74+ return Multipart . finishMultipart ( multipart, completionHandler: completionHandler)
7175
7276 }
7377
0 commit comments