Skip to content

Commit 61bd49b

Browse files
committed
add cid api
1 parent 77ef5cf commit 61bd49b

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public enum PinType {all, direct, indirect, recursive}
3535
public final Swarm swarm = new Swarm();
3636
public final Bootstrap bootstrap = new Bootstrap();
3737
public final Block block = new Block();
38+
39+
public final CidAPI cid = new CidAPI();
3840
public final Dag dag = new Dag();
3941
public final Diag diag = new Diag();
4042
public final Config config = new Config();
@@ -310,6 +312,32 @@ public void sub(String topic, Consumer<Map<String, Object>> results, Consumer<IO
310312
}
311313
}
312314

315+
public class CidAPI {
316+
public Map base32(Cid hash) throws IOException {
317+
return (Map)retrieveAndParse("cid/base32?arg=" + hash);
318+
}
319+
320+
public List<Map> bases(boolean prefix, boolean numeric) throws IOException {
321+
return (List)retrieveAndParse("cid/bases?prefix=" + prefix + "&numeric=" + numeric);
322+
}
323+
324+
public List<Map> codecs(boolean numeric, boolean supported) throws IOException {
325+
return (List)retrieveAndParse("cid/codecs?numeric=" + numeric + "&supported=" + supported);
326+
}
327+
328+
public Map format(Cid hash, Optional<String> f, Optional<String> v, Optional<String> mc, Optional<String> b) throws IOException {
329+
String fArg = f.isPresent() ? "&f=" + URLEncoder.encode(f.get()) : "";
330+
String vArg = v.isPresent() ? "&v=" + v.get() : "";
331+
String mcArg = mc.isPresent() ? "&mc=" + mc.get() : "";
332+
String bArg = b.isPresent() ? "&b=" + b.get() : "";
333+
return (Map)retrieveAndParse("cid/format?arg=" + hash + fArg + vArg + mcArg + bArg);
334+
}
335+
336+
public List<Map> hashes(boolean numeric, boolean supported) throws IOException {
337+
return (List)retrieveAndParse("cid/hashes?numeric=" + numeric + "&supported=" + supported);
338+
}
339+
340+
}
313341
/* 'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
314342
*/
315343
public class Block {
@@ -578,21 +606,30 @@ public List<MultiAddress> bootstrap() throws IOException {
578606
}
579607

580608
public class Bootstrap {
581-
public List<MultiAddress> list() throws IOException {
582-
return bootstrap();
583-
}
584609

585610
public List<MultiAddress> add(MultiAddress addr) throws IOException {
586611
return ((List<String>)retrieveMap("bootstrap/add?arg="+addr).get("Peers")).stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
587612
}
588613

614+
public List<MultiAddress> add() throws IOException {
615+
return ((List<String>)retrieveMap("bootstrap/add/default").get("Peers")).stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
616+
}
617+
618+
public List<MultiAddress> list() throws IOException {
619+
return ((List<String>)retrieveMap("bootstrap/list").get("Peers")).stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
620+
}
621+
589622
public List<MultiAddress> rm(MultiAddress addr) throws IOException {
590623
return rm(addr, false);
591624
}
592625

593626
public List<MultiAddress> rm(MultiAddress addr, boolean all) throws IOException {
594627
return ((List<String>)retrieveMap("bootstrap/rm?"+(all ? "all=true&":"")+"arg="+addr).get("Peers")).stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
595628
}
629+
630+
public List<MultiAddress> rmAll() throws IOException {
631+
return ((List<String>)retrieveMap("bootstrap/rm/all").get("Peers")).stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
632+
}
596633
}
597634

598635
/* ipfs swarm is a tool to manipulate the network swarm. The swarm is the

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.*;
88

99
import java.io.*;
10+
import java.net.URLEncoder;
1011
import java.nio.file.*;
1112
import java.util.*;
1213
import java.util.function.*;
@@ -730,6 +731,24 @@ public void bootstrapTest() throws IOException {
730731
List<MultiAddress> bootstrap = ipfs.bootstrap.list();
731732
List<MultiAddress> rm = ipfs.bootstrap.rm(bootstrap.get(0), false);
732733
List<MultiAddress> add = ipfs.bootstrap.add(bootstrap.get(0));
734+
List<MultiAddress> defaultPeers = ipfs.bootstrap.add();
735+
List<MultiAddress> peers = ipfs.bootstrap.list();
736+
}
737+
738+
@Test
739+
public void cidTest() throws IOException {
740+
List<Map> bases = ipfs.cid.bases(true, true);
741+
List<Map> codecs = ipfs.cid.codecs(true, true);
742+
Map stat = ipfs.files.stat("/");
743+
String rootFolderHash = (String)stat.get("Hash");
744+
Map base32 = ipfs.cid.base32(Cid.decode(rootFolderHash));
745+
Map format = ipfs.cid.format(Cid.decode(rootFolderHash),
746+
Optional.of("%s"), Optional.of("1"),
747+
Optional.empty(), Optional.empty());
748+
749+
List<Map> hashes = ipfs.cid.hashes(false, false);
750+
751+
System.currentTimeMillis();
733752
}
734753

735754
@Test

0 commit comments

Comments
 (0)