Skip to content

Commit

Permalink
feat(android): Replace command method by RequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Jan 14, 2020
1 parent c9d2b5a commit 3d19c65
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ protected String doInBackground(Void... v) {
String cid = randomEntry.getString("cid");
String title = randomEntry.getInt("ep") + ". " + randomEntry.getString("name");

fetchedData = ipfs.command("/cat?arg=" + cid);
fetchedData = ipfs.newRequest("cat")
.withArgument(cid)
.send();

return title;
} catch (Exception err) {
Expand Down
13 changes: 6 additions & 7 deletions android/app/src/main/java/ipfs/gomobile/example/PeerCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ private void updatePeerCount() {
try {
IPFS ipfs = activity.getIpfs();

ArrayList<JSONObject> jsonList = ipfs.commandToJSONList(
"/swarm/peers"
+ "?verbose=false"
+ "&streams=false"
+ "&latency=false"
+ "&direction=false"
);
ArrayList<JSONObject> jsonList = ipfs.newRequest("swarm/peers")
.withOption("verbose", false)
.withOption("streams", false)
.withOption("latency", false)
.withOption("direction", false)
.sendToJSONList();

JSONArray peerList = jsonList.get(0).getJSONArray("Peers");
final int count = peerList.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected String doInBackground(Void... v) {
IPFS ipfs = new IPFS(activity.getApplicationContext());
ipfs.start();

ArrayList<JSONObject> jsonList = ipfs.commandToJSONList("/id");
ArrayList<JSONObject> jsonList = ipfs.newRequest("/id").sendToJSONList();

activity.setIpfs(ipfs);
return jsonList.get(0).getString("ID");
Expand Down
51 changes: 12 additions & 39 deletions android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public final class IPFS {
private Shell shell;

public IPFS(@NonNull Context context)
throws ConfigCreationException, RepoInitException, SockManagerException {
throws ConfigCreationException, RepoInitException, SockManagerException {
this(context, defaultRepoPath, true);
}

public IPFS(@NonNull Context context, @NonNull String repoPath)
throws ConfigCreationException, RepoInitException, SockManagerException {
throws ConfigCreationException, RepoInitException, SockManagerException {
this(context, repoPath, true);
}

public IPFS(@NonNull Context context, @NonNull String repoPath, boolean internalStorage)
throws ConfigCreationException, RepoInitException, SockManagerException {
throws ConfigCreationException, RepoInitException, SockManagerException {
if (internalStorage) {
absRepoPath = context.getFilesDir().getAbsolutePath() + repoPath;
} else {
Expand Down Expand Up @@ -94,12 +94,12 @@ synchronized public String getRepoPath() {
return absRepoPath;
}

synchronized public boolean isStarted() {
return node != null;
}
synchronized public boolean isStarted() {
return node != null;
}

synchronized public void start()
throws NodeStartException, RepoOpenException, ShellInitException {
throws NodeStartException, RepoOpenException, ShellInitException {
if (isStarted()) {
throw new NodeStartException("Node already started");
}
Expand Down Expand Up @@ -139,47 +139,20 @@ synchronized public void stop() throws NodeStopException {
}

synchronized public void restart()
throws NodeStartException, RepoOpenException, ShellInitException, NodeStopException {
throws NodeStartException, RepoOpenException, ShellInitException, NodeStopException {
stop();
start();
}

public byte[] command(String command) throws ShellRequestException {
return this.command(command, null);
}

public byte[] command(String command, byte[] body)
throws ShellRequestException {
if (!isStarted()) {
public RequestBuilder newRequest(String command) throws ShellRequestException {
if (!this.isStarted()) {
throw new ShellRequestException("Shell request failed: node isn't started");
}

try {
return shell.request(command, body);
} catch (Exception err) {
throw new ShellRequestException("Shell request failed", err);
}
}

public ArrayList<JSONObject> commandToJSONList(String command)
throws ShellRequestException, JSONException {
return this.commandToJSONList(command, null);
ipfs.RequestBuilder reqb = this.shell.newRequest(command);
return new RequestBuilder(reqb);
}

public ArrayList<JSONObject> commandToJSONList(String command, byte[] body)
throws ShellRequestException, JSONException {
String raw = new String(this.command(command, body));
ArrayList<JSONObject> jsonList = new ArrayList<>();
Scanner scanner = new Scanner(raw);

while(scanner.hasNextLine()) {
jsonList.add(new JSONObject(scanner.nextLine()));
}

return jsonList;
}


public class ConfigCreationException extends Exception {
ConfigCreationException(String message, Throwable err) { super(message, err); }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ipfs.gomobile.android;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Scanner;

import androidx.annotation.NonNull;

public class RequestBuilder {

private ipfs.RequestBuilder reqb;

RequestBuilder(@NonNull ipfs.RequestBuilder reqb) {
this.reqb = reqb;
}

public byte[] send() throws RequestBuilderException {
try {
return this.reqb.send();
} catch (Exception err) {
throw new RequestBuilderException("failed to send request", err);
}
}

public ArrayList<JSONObject> sendToJSONList() throws RequestBuilderException, JSONException {
String raw = new String(this.send());

ArrayList<JSONObject> jsonList = new ArrayList<>();
Scanner scanner = new Scanner(raw);
while (scanner.hasNextLine()) {
jsonList.add(new JSONObject(scanner.nextLine()));
}

return jsonList;
}

public void exec() throws RequestBuilderException {
try {
this.reqb.exec();
} catch (Exception err) {
throw new RequestBuilderException("failed to send request", err);
}
}

// Arguments
public RequestBuilder withArgument(String arg) {
this.reqb.argument(arg);
return this;
}

// Options
public RequestBuilder withOption(String option, boolean val) {
this.reqb.boolOptions(option, val);
return this;
}
public RequestBuilder withOption(String option, String val) {
this.reqb.stringOptions(option, val);
return this;
}
public RequestBuilder withOption(String option, byte[] val) {
this.reqb.byteOptions(option, val);
return this;
}

// Body
public RequestBuilder withBody(String body) {
this.reqb.bodyString(body);
return this;
}
public RequestBuilder withBody(byte[] body) {
this.reqb.bodyBytes(body);
return this;
}

// Headers
public RequestBuilder withHeader(String key, String val) {
this.reqb.header(key, val);
return this;
}

public class RequestBuilderException extends Exception {
RequestBuilderException(String message) { super(message); }
RequestBuilderException(String message, Throwable err) { super(message, err); }
}
}
4 changes: 2 additions & 2 deletions ios/GomobileIPFS/Classes/RequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public class RequestBuilder {
return self
}

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

Expand Down

0 comments on commit 3d19c65

Please sign in to comment.