@@ -52,7 +52,7 @@ extension IpfsApiClient {
52
52
53
53
func fetchStreamJson( _ path: String ,
54
54
updateHandler: @escaping ( Data , URLSessionDataTask ) throws -> Bool ,
55
- completionHandler: @escaping ( AnyObject ) throws -> Void ) throws {
55
+ completionHandler: @escaping ( AnyObject ) throws -> Void ) throws -> CancellableRequest {
56
56
/// We need to use the passed in completionHandler
57
57
try net. streamFrom ( baseUrl + path, updateHandler: updateHandler, completionHandler: completionHandler)
58
58
}
@@ -69,9 +69,10 @@ extension IpfsApiClient {
69
69
// }
70
70
// stream.close()
71
71
72
-
73
- func fetchJson( _ path: String , completionHandler: @escaping ( JsonType ) throws -> Void ) throws {
74
- try fetchData ( path) {
72
+
73
+ @discardableResult
74
+ func fetchJson( _ path: String , completionHandler: @escaping ( JsonType ) throws -> Void ) throws -> CancellableRequest {
75
+ return try fetchData ( path) {
75
76
( data: Data ) in
76
77
77
78
/// If there was no data fetched pass an empty dictionary and return.
@@ -99,13 +100,14 @@ extension IpfsApiClient {
99
100
try completionHandler ( JsonType . parse ( json as AnyObject ) )
100
101
}
101
102
}
102
-
103
- func fetchData ( _ path : String , completionHandler : @escaping ( Data ) throws -> Void ) throws {
104
-
103
+
104
+ @ discardableResult
105
+ func fetchData ( _ path : String , completionHandler : @escaping ( Data ) throws -> Void ) throws -> CancellableRequest {
105
106
try net. receiveFrom ( baseUrl + path, completionHandler: completionHandler)
106
107
}
107
-
108
- func fetchBytes( _ path: String , completionHandler: @escaping ( [ UInt8 ] ) throws -> Void ) throws {
108
+
109
+ @discardableResult
110
+ func fetchBytes( _ path: String , completionHandler: @escaping ( [ UInt8 ] ) throws -> Void ) throws -> CancellableRequest {
109
111
try fetchData ( path) {
110
112
( data: Data ) in
111
113
@@ -239,9 +241,9 @@ public class IpfsApi : IpfsApiClient {
239
241
240
242
241
243
/// base commands
242
-
243
- public func add ( _ filePath : String , completionHandler : @escaping ( [ MerkleNode ] ) -> Void ) throws {
244
-
244
+
245
+ @ discardableResult
246
+ public func add ( _ filePath : String , completionHandler : @escaping ( [ MerkleNode ] ) -> Void ) throws -> CancellableRequest {
245
247
try net. sendTo ( baseUrl+ " add?s " , filePath: filePath) {
246
248
data in
247
249
do {
@@ -265,9 +267,9 @@ public class IpfsApi : IpfsApiClient {
265
267
}
266
268
267
269
// Store binary data
268
-
269
- public func add ( _ fileData : Data , completionHandler : @escaping ( [ MerkleNode ] ) -> Void ) throws {
270
-
270
+
271
+ @ discardableResult
272
+ public func add ( _ fileData : Data , completionHandler : @escaping ( [ MerkleNode ] ) -> Void ) throws -> CancellableRequest {
271
273
try net. sendTo ( baseUrl+ " add?stream-channels=true " , content: fileData) {
272
274
data in
273
275
do {
@@ -291,9 +293,9 @@ public class IpfsApi : IpfsApiClient {
291
293
}
292
294
}
293
295
}
294
-
295
- public func ls ( _ hash : Multihash , completionHandler : @escaping ( [ MerkleNode ] ) -> Void ) throws {
296
-
296
+
297
+ @ discardableResult
298
+ public func ls ( _ hash : Multihash , completionHandler : @escaping ( [ MerkleNode ] ) -> Void ) throws -> CancellableRequest {
297
299
try fetchJson ( " ls/ \( b58String ( hash) ) " ) {
298
300
json in
299
301
@@ -310,17 +312,18 @@ public class IpfsApi : IpfsApiClient {
310
312
}
311
313
}
312
314
313
- public func cat( _ hash: Multihash , completionHandler: @escaping ( [ UInt8 ] ) -> Void ) throws {
315
+ @discardableResult
316
+ public func cat( _ hash: Multihash , completionHandler: @escaping ( [ UInt8 ] ) -> Void ) throws -> CancellableRequest {
314
317
try fetchBytes ( " cat/ \( b58String ( hash) ) " , completionHandler: completionHandler)
315
318
}
316
-
317
- public func get( _ hash: Multihash , completionHandler: @escaping ( [ UInt8 ] ) -> Void ) throws {
319
+
320
+ @discardableResult
321
+ public func get( _ hash: Multihash , completionHandler: @escaping ( [ UInt8 ] ) -> Void ) throws -> CancellableRequest {
318
322
try self . cat ( hash, completionHandler: completionHandler)
319
323
}
320
324
321
-
322
- public func refs( _ hash: Multihash , recursive: Bool , completionHandler: @escaping ( [ Multihash ] ) -> Void ) throws {
323
-
325
+ @discardableResult
326
+ public func refs( _ hash: Multihash , recursive: Bool , completionHandler: @escaping ( [ Multihash ] ) -> Void ) throws -> CancellableRequest {
324
327
try fetchJson ( " refs?arg= " + b58String( hash) + " &r= \( recursive) " ) {
325
328
result in
326
329
guard let results = result. array else { throw IpfsApiError . unexpectedReturnType }
@@ -338,20 +341,23 @@ public class IpfsApi : IpfsApiClient {
338
341
}
339
342
}
340
343
341
- public func resolve( _ scheme: String , hash: Multihash , recursive: Bool , completionHandler: @escaping ( JsonType ) -> Void ) throws {
344
+ @discardableResult
345
+ public func resolve( _ scheme: String , hash: Multihash , recursive: Bool , completionHandler: @escaping ( JsonType ) -> Void ) throws -> CancellableRequest {
342
346
try fetchJson ( " resolve?arg=/ \( scheme) / \( b58String ( hash) ) &r= \( recursive) " , completionHandler: completionHandler)
343
347
}
344
-
345
- public func dns( _ domain: String , completionHandler: @escaping ( String ) -> Void ) throws {
348
+
349
+ @discardableResult
350
+ public func dns( _ domain: String , completionHandler: @escaping ( String ) -> Void ) throws -> CancellableRequest {
346
351
try fetchJson ( " dns?arg= " + domain) {
347
352
result in
348
353
349
354
guard let path = result. object ? [ IpfsCmdString . Path. rawValue] ? . string else { throw IpfsApiError . resultMissingData ( " No Path found " ) }
350
355
completionHandler ( path)
351
356
}
352
357
}
353
-
354
- public func mount( _ ipfsRootPath: String = " /ipfs " , ipnsRootPath: String = " /ipns " , completionHandler: @escaping ( JsonType ) -> Void ) throws {
358
+
359
+ @discardableResult
360
+ public func mount( _ ipfsRootPath: String = " /ipfs " , ipnsRootPath: String = " /ipns " , completionHandler: @escaping ( JsonType ) -> Void ) throws -> CancellableRequest {
355
361
356
362
let fileManager = FileManager . default
357
363
@@ -363,25 +369,27 @@ public class IpfsApi : IpfsApiClient {
363
369
try fileManager. createDirectory ( atPath: ipnsRootPath, withIntermediateDirectories: false , attributes: nil )
364
370
}
365
371
366
- try fetchJson ( " mount?arg= " + ipfsRootPath + " &arg= " + ipnsRootPath, completionHandler: completionHandler)
372
+ return try fetchJson ( " mount?arg= " + ipfsRootPath + " &arg= " + ipnsRootPath, completionHandler: completionHandler)
367
373
}
368
374
369
375
/** ping is a tool to test sending data to other nodes.
370
376
It finds nodes via the routing system, send pings, wait for pongs,
371
377
and prints out round- trip latency information. */
372
- public func ping( _ target: String , completionHandler: @escaping ( JsonType ) -> Void ) throws {
378
+ @discardableResult
379
+ public func ping( _ target: String , completionHandler: @escaping ( JsonType ) -> Void ) throws -> CancellableRequest {
373
380
try fetchJson ( " ping/ " + target, completionHandler: completionHandler)
374
381
}
375
382
376
-
377
- public func id( _ target: String ? = nil , completionHandler: @escaping ( JsonType ) -> Void ) throws {
383
+ @ discardableResult
384
+ public func id( _ target: String ? = nil , completionHandler: @escaping ( JsonType ) -> Void ) throws -> CancellableRequest {
378
385
var request = " id "
379
386
if target != nil { request += " / \( target!) " }
380
387
381
- try fetchJson ( request, completionHandler: completionHandler)
388
+ return try fetchJson ( request, completionHandler: completionHandler)
382
389
}
383
-
384
- public func version( _ completionHandler: @escaping ( String ) -> Void ) throws {
390
+
391
+ @discardableResult
392
+ public func version( _ completionHandler: @escaping ( String ) -> Void ) throws -> CancellableRequest {
385
393
try fetchJson ( " version " ) {
386
394
json in
387
395
let version = json. object ? [ IpfsCmdString . Version. rawValue] ? . string ?? " "
@@ -390,19 +398,21 @@ public class IpfsApi : IpfsApiClient {
390
398
}
391
399
392
400
/** List all available commands. */
393
- public func commands( _ showOptions: Bool = false , completionHandler: @escaping ( JsonType ) -> Void ) throws {
401
+ @discardableResult
402
+ public func commands( _ showOptions: Bool = false , completionHandler: @escaping ( JsonType ) -> Void ) throws -> CancellableRequest {
394
403
395
404
var request = " commands " //+ (showOptions ? "?flags=true&" : "")
396
405
if showOptions { request += " ?flags=true& " }
397
406
398
- try fetchJson ( request, completionHandler: completionHandler)
407
+ return try fetchJson ( request, completionHandler: completionHandler)
399
408
}
400
409
401
410
/** This method should take both a completion handler and an update handler.
402
411
Since the log tail won't stop until interrupted, the update handler
403
412
should return false when it wants the updates to stop.
404
413
*/
405
- public func log( _ updateHandler: ( Data ) throws -> Bool , completionHandler: @escaping ( [ [ String : AnyObject ] ] ) -> Void ) throws {
414
+ @discardableResult
415
+ public func log( _ updateHandler: ( Data ) throws -> Bool , completionHandler: @escaping ( [ [ String : AnyObject ] ] ) -> Void ) throws -> CancellableRequest {
406
416
407
417
/// Two test closures to be passed to the fetchStreamJson as parameters.
408
418
let comp = { ( result: AnyObject ) -> Void in
@@ -426,16 +436,17 @@ public class IpfsApi : IpfsApiClient {
426
436
return true
427
437
}
428
438
429
- try fetchStreamJson ( " log/tail " , updateHandler: update, completionHandler: comp)
439
+ return try fetchStreamJson ( " log/tail " , updateHandler: update, completionHandler: comp)
430
440
}
431
441
}
432
442
433
443
434
444
435
445
/** Show or edit the list of bootstrap peers */
436
446
extension IpfsApiClient {
437
-
438
- public func bootstrap( _ completionHandler: @escaping ( [ Multiaddr ] ) -> Void ) throws {
447
+
448
+ @discardableResult
449
+ public func bootstrap( _ completionHandler: @escaping ( [ Multiaddr ] ) -> Void ) throws -> CancellableRequest {
439
450
try bootstrap. list ( completionHandler)
440
451
}
441
452
}
0 commit comments