@@ -145,6 +145,22 @@ enum IpfsApiError : Error {
145145 case bootstrapError( String )
146146}
147147
148+ // map command names to string to avoid "stringly typed" lookups.
149+ enum IpfsCmdString : String {
150+ case Ref = " Ref "
151+ case Path = " Path "
152+ case Version = " Version "
153+ case Name = " Name "
154+ case Pinned = " Pinned "
155+ case Keys = " Keys "
156+ case Peers = " Peers "
157+ case ID = " ID "
158+ case Addrs = " Addrs "
159+ case Value = " Value "
160+ case Message = " Message "
161+
162+ }
163+
148164public class IpfsApi : IpfsApiClient {
149165
150166 public var baseUrl : String = " "
@@ -170,7 +186,6 @@ public class IpfsApi : IpfsApiClient {
170186 public let stats = Stats ( )
171187 public let config = Config ( )
172188 public let update = Update ( )
173-
174189
175190 public convenience init ( addr: Multiaddr ) throws {
176191 /// Get the host and port number from the Multiaddr
@@ -232,16 +247,13 @@ public class IpfsApi : IpfsApiClient {
232247
233248 public func add( _ filePaths: [ String ] , completionHandler: @escaping ( [ MerkleNode ] ) -> Void ) throws {
234249
235- try net. sendTo ( baseUrl+ " add?stream-channels=true " , content: filePaths) {
236- // try net.sendTo(baseUrl+"add?r", content: filePaths) {
250+ try net. sendTo ( baseUrl+ " add?s " , content: filePaths) {
237251 data in
238252 do {
239253 /// If there was no data fetched pass an empty dictionary and return.
240254 let fixedData = fixStreamJson ( data)
241255
242-
243256 let json = JsonType . parse ( try JSONSerialization . jsonObject ( with: fixedData, options: JSONSerialization . ReadingOptions. allowFragments) as AnyObject )
244- print ( json)
245257
246258 let res = try merkleNodesFromJson ( json)
247259 guard res. count > 0 else { throw IpfsApiError . jsonSerializationFailed }
@@ -321,7 +333,7 @@ public class IpfsApi : IpfsApiClient {
321333 var refs : [ Multihash ] = [ ]
322334
323335 for obj in results {
324- if let ref = obj. object ? [ " Ref " ] ? . string {
336+ if let ref = obj. object ? [ IpfsCmdString . Ref. rawValue ] ? . string {
325337 let mh = try fromB58String ( ref)
326338 refs. append ( mh)
327339 }
@@ -339,7 +351,7 @@ public class IpfsApi : IpfsApiClient {
339351 try fetchJson ( " dns?arg= " + domain) {
340352 result in
341353
342- guard let path = result. object ? [ " Path " ] ? . string else { throw IpfsApiError . resultMissingData ( " No Path found " ) }
354+ guard let path = result. object ? [ IpfsCmdString . Path. rawValue ] ? . string else { throw IpfsApiError . resultMissingData ( " No Path found " ) }
343355 completionHandler ( path)
344356 }
345357 }
@@ -377,7 +389,7 @@ public class IpfsApi : IpfsApiClient {
377389 public func version( _ completionHandler: @escaping ( String ) -> Void ) throws {
378390 try fetchJson ( " version " ) {
379391 json in
380- let version = json. object ? [ " Version " ] ? . string ?? " "
392+ let version = json. object ? [ IpfsCmdString . Version. rawValue ] ? . string ?? " "
381393 completionHandler ( version)
382394 }
383395 }
0 commit comments