Skip to content

Commit 267a208

Browse files
committed
improve stat() calls in API
1 parent 2684b2c commit 267a208

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/main/java/io/ipfs/api/IPFS.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ public Multihash ls() throws IOException {
397397
/*public String migrate(boolean allowDowngrade) throws IOException {
398398
return retrieveString("repo/migrate?allow-downgrade=" + allowDowngrade);
399399
}*/
400-
public Map stat(boolean sizeOnly, boolean humanReadable) throws IOException {
401-
return retrieveMap("repo/stat?size-only=" + sizeOnly + "&human=" + humanReadable);
400+
public RepoStat stat(boolean sizeOnly) throws IOException {
401+
return RepoStat.fromJson(retrieveAndParse("repo/stat?size-only=" + sizeOnly));
402402
}
403403
public Map verify() throws IOException {
404404
return retrieveMap("repo/verify");
@@ -812,8 +812,8 @@ public String reprovide() throws IOException {
812812
public Map stat() throws IOException {
813813
return retrieveMap("bitswap/stat");
814814
}
815-
public Map stat(boolean verbose, boolean humanReadable) throws IOException {
816-
return retrieveMap("bitswap/stat?verbose=" + verbose + "&human=" + humanReadable);
815+
public Map stat(boolean verbose) throws IOException {
816+
return retrieveMap("bitswap/stat?verbose=" + verbose);
817817
}
818818
public Map wantlist(Multihash peerId) throws IOException {
819819
return retrieveMap("bitswap/wantlist?peer=" + peerId);
@@ -856,6 +856,9 @@ public List<MultiAddress> rmAll() throws IOException {
856856
public class Swarm {
857857
public List<Peer> peers() throws IOException {
858858
Map m = retrieveMap("swarm/peers?stream-channels=true");
859+
if (m.get("Peers") == null) {
860+
return Collections.emptyList();
861+
}
859862
return ((List<Object>)m.get("Peers")).stream()
860863
.flatMap(json -> {
861864
try {
@@ -981,8 +984,8 @@ public Map id() throws IOException {
981984
}
982985

983986
public class Stats {
984-
public Map bitswap(boolean verbose, boolean humanReadable) throws IOException {
985-
return retrieveMap("stats/bitswap?verbose=" + verbose + "&human=" + humanReadable);
987+
public Map bitswap(boolean verbose) throws IOException {
988+
return retrieveMap("stats/bitswap?verbose=" + verbose);
986989
}
987990
public Map bw() throws IOException {
988991
return retrieveMap("stats/bw");
@@ -993,8 +996,8 @@ public Map dht() throws IOException {
993996
public Map provide() throws IOException {
994997
return retrieveMap("stats/provide");
995998
}
996-
public Map repo(boolean sizeOnly, boolean humanReadable) throws IOException {
997-
return retrieveMap("stats/repo?size-only=" + sizeOnly + "&human=" + humanReadable);
999+
public RepoStat repo(boolean sizeOnly) throws IOException {
1000+
return RepoStat.fromJson(retrieveAndParse("stats/repo?size-only=" + sizeOnly));
9981001
}
9991002
}
10001003

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.ipfs.api;
2+
3+
import java.util.Map;
4+
5+
public class RepoStat {
6+
7+
public final long RepoSize;
8+
public final long StorageMax;
9+
public final long NumObjects;
10+
public final String RepoPath;
11+
public final String Version;
12+
13+
public RepoStat(long repoSize, long storageMax, long numObjects, String repoPath, String version ) {
14+
this.RepoSize = repoSize;
15+
this.StorageMax = storageMax;
16+
this.NumObjects = numObjects;
17+
this.RepoPath = repoPath;
18+
this.Version = version;
19+
}
20+
public static RepoStat fromJson(Object rawjson) {
21+
Map json = (Map)rawjson;
22+
long repoSize = Long.parseLong(json.get("RepoSize").toString());
23+
long storageMax = Long.parseLong(json.get("StorageMax").toString());
24+
long numObjects = Long.parseLong(json.get("NumObjects").toString());
25+
String repoPath = (String)json.get("RepoPath");
26+
String version = (String)json.get("Version");
27+
28+
return new RepoStat(repoSize, storageMax, numObjects, repoPath, version);
29+
}
30+
}

src/test/java/io/ipfs/api/APITest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ public void repoTest() throws IOException {
719719
ipfs.repo.gc();
720720
Multihash res = ipfs.repo.ls();
721721
//String migration = ipfs.repo.migrate(false);
722-
Map stat = ipfs.repo.stat(false, true);
722+
RepoStat stat = ipfs.repo.stat(false);
723+
RepoStat stat2 = ipfs.repo.stat(true);
723724
Map verify = ipfs.repo.verify();
724725
Map version = ipfs.repo.version();
725726
}
@@ -765,11 +766,11 @@ public void localId() throws Exception {
765766
@Test
766767
public void statsTest() throws IOException {
767768
Map stats = ipfs.stats.bw();
768-
Map bitswap = ipfs.stats.bitswap(true, true);
769+
Map bitswap = ipfs.stats.bitswap(true);
769770
Map dht = ipfs.stats.dht();
770771
//{"Message":"can only return stats if Experimental.AcceleratedDHTClient is enabled","Code":0,"Type":"error"}
771772
//requires Map provide = ipfs.stats.provide();
772-
Map repo = ipfs.stats.repo(false, true);
773+
RepoStat repo = ipfs.stats.repo(false);
773774
}
774775

775776
public void resolveTest() throws IOException {
@@ -859,7 +860,7 @@ public void bitswapTest() throws IOException {
859860
Map want = ipfs.bitswap.wantlist(peers.get(0).id);
860861
//String reprovide = ipfs.bitswap.reprovide();
861862
Map stat = ipfs.bitswap.stat();
862-
Map stat2 = ipfs.bitswap.stat(true, false);
863+
Map stat2 = ipfs.bitswap.stat(true);
863864
}
864865
@Test
865866
public void bootstrapTest() throws IOException {

0 commit comments

Comments
 (0)