Skip to content

Commit

Permalink
feat(ios): Replace commands methods by RequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Jan 14, 2020
1 parent b2f7611 commit ebc2ccc
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ios/Example/GomobileIPFS/PeerCountUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PeerCountUpdater: NSObject {
if self.running && ViewController.ipfs!.isStarted() {
var peerCount: Int = 0
do {
let res = try ViewController.ipfs!.commandToDict("/swarm/peers")
let res = try ViewController.ipfs!.newRequest("/swarm/peers").sendToDict()
let peerList = res["Peers"] as? NSArray
peerCount = peerList?.count ?? 0
} catch let error {
Expand Down
4 changes: 2 additions & 2 deletions ios/Example/GomobileIPFS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ViewController: UIViewController {
ViewController.ipfs = try IPFS()
try ViewController.ipfs!.start()

let res = try ViewController.ipfs!.commandToDict("id")
let res = try ViewController.ipfs!.newRequest("id").sendToDict()
self.peerID = (res["ID"] as! String)
} catch let err as IPFSError {
error = err.localizedFullDescription
Expand Down Expand Up @@ -172,7 +172,7 @@ class ViewController: UIViewController {
let randomIndex = Int(arc4random_uniform(UInt32(self.xkcdList!.count)))
let randomEntry = self.xkcdList![randomIndex] as! [String: Any]
let cid = randomEntry["cid"] as! String
let fetchedData = try ViewController.ipfs!.command("/cat?arg=\(cid)");
let fetchedData = try ViewController.ipfs!.newRequest("cat").with(arg: cid).send()

title = "\(randomEntry["ep"] as! Int). \(randomEntry["name"] as! String)"
image = UIImage(data: fetchedData)!
Expand Down
49 changes: 15 additions & 34 deletions ios/GomobileIPFS/Classes/IPFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class IPFS: NSObject {

self.absSockPath = try IPFS.sockManager!.newSockPath()
#else // On simulator we can't create an UDS, see comment below
self.absSockPath = ""
self.absSockPath = ""
#endif


Expand All @@ -58,8 +58,8 @@ public class IPFS: NSObject {
}

public func getRepoPath() -> URL {
return self.absRepoURL
}
return self.absRepoURL
}

public func isStarted() -> Bool {
return self.node != nil
Expand Down Expand Up @@ -88,10 +88,10 @@ public class IPFS: NSObject {
throw IPFSError("UDS shell creation failed", err)
}
/*
** On iOS simulator, temporary directory's absolute path exceeds
** the length limit for Unix Domain Socket, since simulator is
** only used for debug, we can safely fallback on shell over TCP
*/
** On iOS simulator, temporary directory's absolute path exceeds
** the length limit for Unix Domain Socket, since simulator is
** only used for debug, we can safely fallback on shell over TCP
*/
#else
let maddr: String = try node.serve(onTCPPort: "0")
if let shell = IpfsNewShell(maddr, &err) {
Expand All @@ -114,35 +114,16 @@ public class IPFS: NSObject {
self.node = nil
}

public func restart() throws {
try self.stop()
try self.start()
}
public func restart() throws {
try self.stop()
try self.start()
}

public func command(_ command: String, body: Data? = nil) throws -> Data {
if !self.isStarted() {
throw IPFSError("node is not started")
}

do {
if let raw = try self.shell?.request(command, body: body) {
return raw
} else {
throw IPFSError("request to shell failed: empty response")
}
} catch let error as NSError {
throw IPFSError("request to shell failed", error)
public func newRequest(_ command: String) throws -> RequestBuilder {
guard let request = self.shell?.newRequest(command) else {
throw IPFSError("unable to get shell, is the node started?")
}
}

public func commandToDict(_ command: String, body: Data? = nil) throws -> [String: Any] {
let raw = try self.command(command, body: body)

do {
let json = try JSONSerialization.jsonObject(with: raw, options: [])
return json as! [String: Any]
} catch let error as NSError {
throw IPFSError("command response deserialization failed", error)
}
return RequestBuilder(reqb: request)
}
}
76 changes: 76 additions & 0 deletions ios/GomobileIPFS/Classes/RequestBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// RequestBuilder.swift
// GomobileIPFS
//
// Created by Guilhem Fanton on 14/01/2020.
//

import Foundation
import Ipfs

public enum RequestOption {
case Bool(Bool)
case String(String)
case Bytes(Data)
}

public enum RequestBody {
case String(String)
case Bytes(Data)
}

public class RequestBuilder {
private let reqb: IpfsRequestBuilder

internal init(reqb: IpfsRequestBuilder) {
self.reqb = reqb
}

public func with(arg: String) -> RequestBuilder {
self.reqb.argument(arg)
return self
}

public func with(option: String, val: RequestOption) -> RequestBuilder {
switch (val) {
case .Bool(let bool):
self.reqb.boolOptions(option, value: bool)
case .String(let string):
self.reqb.stringOptions(option, value: string)
case .Bytes(let data):
self.reqb.byteOptions(option, value: data)
}

return self
}

public func with(body: RequestBody) -> RequestBuilder {
switch (body) {
case .Bytes(let data):
self.reqb.bodyBytes(data)
case .String(let string):
self.reqb.bodyString(string)
}

return self
}

public func withHeader(key: String, val: String) -> RequestBuilder {
self.reqb.header(key, value: val)
return self
}

public func send() throws -> Data {
return try self.reqb.send()
}

public func sendToDict() throws -> [String: Any] {
let res = try self.reqb.send()
let json = try JSONSerialization.jsonObject(with: res, options: [])
return json as! [String: Any]
}

public func exec() throws {
try self.reqb.exec()
}
}

0 comments on commit ebc2ccc

Please sign in to comment.