Skip to content

Commit 9116d57

Browse files
committed
add more api methods
1 parent 1e498ff commit 9116d57

File tree

2 files changed

+116
-20
lines changed

2 files changed

+116
-20
lines changed

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

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum PinType {all, direct, indirect, recursive}
2525
public final String host;
2626
public final int port;
2727
public final String protocol;
28-
private final String version;
28+
private final String apiVersion;
2929
private final int connectTimeoutMillis;
3030
private final int readTimeoutMillis;
3131
public final Key key = new Key();
@@ -36,6 +36,7 @@ public enum PinType {all, direct, indirect, recursive}
3636
public final IPFSObject object = new IPFSObject();
3737
public final Swarm swarm = new Swarm();
3838
public final Bootstrap bootstrap = new Bootstrap();
39+
public final Bitswap bitswap = new Bitswap();
3940
public final Block block = new Block();
4041
public final CidAPI cid = new CidAPI();
4142
public final Dag dag = new Dag();
@@ -50,6 +51,7 @@ public enum PinType {all, direct, indirect, recursive}
5051
public final Stats stats = new Stats();
5152
public final Name name = new Name();
5253
public final Pubsub pubsub = new Pubsub();
54+
public final VersionAPI version = new VersionAPI();
5355

5456
public IPFS(String host, int port) {
5557
this(host, port, "/api/v0/", false);
@@ -81,7 +83,7 @@ public IPFS(String host, int port, String version, int connectTimeoutMillis, int
8183
this.protocol = "http";
8284
}
8385

84-
this.version = version;
86+
this.apiVersion = version;
8587
// Check IPFS is sufficiently recent
8688
try {
8789
Version detected = Version.parse(version());
@@ -98,7 +100,7 @@ public IPFS(String host, int port, String version, int connectTimeoutMillis, int
98100
* @return current IPFS object with configured timeout
99101
*/
100102
public IPFS timeout(int timeout) {
101-
return new IPFS(host, port, version, timeout, timeout, protocol.equals("https"));
103+
return new IPFS(host, port, apiVersion, timeout, timeout, protocol.equals("https"));
102104
}
103105

104106
public String shutdown() throws IOException {
@@ -118,7 +120,7 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
118120
}
119121

120122
public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean hashOnly) throws IOException {
121-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?stream-channels=true&w="+wrap + "&n="+hashOnly, "UTF-8");
123+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion + "add?stream-channels=true&w="+wrap + "&n="+hashOnly, "UTF-8");
122124
for (NamedStreamable file: files) {
123125
if (file.isDirectory()) {
124126
m.addSubtree(Paths.get(""), file);
@@ -269,7 +271,7 @@ public Map ls() throws IOException {
269271

270272
public class MultibaseAPI {
271273
public String decode(NamedStreamable encoded_file) {
272-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version +
274+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion +
273275
"multibase/decode", "UTF-8");
274276
try {
275277
if (encoded_file.isDirectory()) {
@@ -284,7 +286,7 @@ public String decode(NamedStreamable encoded_file) {
284286
}
285287
public String encode(Optional<String> encoding, NamedStreamable file) {
286288
String b = encoding.map(f -> "?b=" + f).orElse("?b=base64url");
287-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version +
289+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion +
288290
"multibase/encode" + b, "UTF-8");
289291
try {
290292
if (file.isDirectory()) {
@@ -302,7 +304,7 @@ public List<Map> list(boolean prefix, boolean numeric) throws IOException {
302304
}
303305
public String transcode(Optional<String> encoding, NamedStreamable file) {
304306
String b = encoding.map(f -> "?b=" + f).orElse("?b=base64url");
305-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version +
307+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion +
306308
"multibase/transcode" + b, "UTF-8");
307309
try {
308310
if (file.isDirectory()) {
@@ -341,6 +343,13 @@ public Map version() throws IOException {
341343
}
342344
}
343345

346+
347+
public class VersionAPI {
348+
public Map versionDeps() throws IOException {
349+
return retrieveMap("version/deps");
350+
}
351+
}
352+
344353
public class Pubsub {
345354
public Object ls() throws IOException {
346355
return retrieveAndParse("pubsub/ls");
@@ -363,7 +372,7 @@ public Object peers(String topic) throws IOException {
363372
*/
364373
public void pub(String topic, String data) {
365374
String encodedTopic = Multibase.encode(Multibase.Base.Base64Url, topic.getBytes());
366-
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"pubsub/pub?arg=" + encodedTopic, "UTF-8");
375+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + apiVersion+"pubsub/pub?arg=" + encodedTopic, "UTF-8");
367376
try {
368377
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.getBytes()));
369378
String res = m.finish();
@@ -445,7 +454,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
445454

446455
public MerkleNode put(byte[] data, Optional<String> format) throws IOException {
447456
String fmt = format.map(f -> "&format=" + f).orElse("");
448-
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"block/put?stream-channels=true" + fmt, "UTF-8");
457+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + apiVersion+"block/put?stream-channels=true" + fmt, "UTF-8");
449458
try {
450459
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data));
451460
String res = m.finish();
@@ -465,7 +474,7 @@ public Map stat(Multihash hash) throws IOException {
465474
public class IPFSObject {
466475
@Deprecated
467476
public List<MerkleNode> put(List<byte[]> data) throws IOException {
468-
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/put?stream-channels=true", "UTF-8");
477+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + apiVersion+"object/put?stream-channels=true", "UTF-8");
469478
for (byte[] f : data)
470479
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
471480
String res = m.finish();
@@ -475,7 +484,7 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
475484
public List<MerkleNode> put(String encoding, List<byte[]> data) throws IOException {
476485
if (!"json".equals(encoding) && !"protobuf".equals(encoding))
477486
throw new IllegalArgumentException("Encoding must be json or protobuf");
478-
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
487+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + apiVersion+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
479488
for (byte[] f : data)
480489
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
481490
String res = m.finish();
@@ -529,7 +538,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
529538
case "append-data":
530539
if (!data.isPresent())
531540
throw new IllegalStateException("set-data requires data!");
532-
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
541+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + apiVersion+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
533542
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.get()));
534543
String res = m.finish();
535544
return MerkleNode.fromJSON(JSONParser.parse(res));
@@ -665,7 +674,7 @@ public Map stat(String path) throws IOException {
665674
public String write(String path, NamedStreamable uploadFile, boolean create, boolean parents) throws IOException {
666675
String arg = URLEncoder.encode(path, "UTF-8");
667676
String rpcParams = "files/write?arg=" + arg + "&create=" + create + "&parents=" + parents;
668-
URL target = new URL(protocol,host,port,version + rpcParams);
677+
URL target = new URL(protocol,host,port,apiVersion + rpcParams);
669678
Multipart m = new Multipart(target.toString(),"UTF-8");
670679
if (uploadFile.isDirectory()) {
671680
throw new IllegalArgumentException("Input must be a file");
@@ -704,6 +713,21 @@ public List<MultiAddress> bootstrap() throws IOException {
704713
}).collect(Collectors.toList());
705714
}
706715

716+
public class Bitswap {
717+
public Map ledger(Multihash peerId) throws IOException {
718+
return retrieveMap("bitswap/ledger?arg="+peerId);
719+
}
720+
721+
public String reprovide() throws IOException {
722+
return retrieveString("bitswap/reprovide");
723+
}
724+
public Map stat() throws IOException {
725+
return retrieveMap("bitswap/stat");
726+
}
727+
public Map wantlist(Multihash peerId) throws IOException {
728+
return retrieveMap("bitswap/wantlist?peer=" + peerId);
729+
}
730+
}
707731
public class Bootstrap {
708732

709733
public List<MultiAddress> add(MultiAddress addr) throws IOException {
@@ -762,7 +786,12 @@ public Map<Multihash, List<MultiAddress>> addrs() throws IOException {
762786
.map(MultiAddress::new)
763787
.collect(Collectors.toList())));
764788
}
765-
789+
public Map listenAddrs() throws IOException {
790+
return retrieveMap("swarm/addrs/listen");
791+
}
792+
public Map localAddrs(boolean showPeerId) throws IOException {
793+
return retrieveMap("swarm/addrs/local?id=" + showPeerId);
794+
}
766795
public Map connect(MultiAddress multiAddr) throws IOException {
767796
Map m = retrieveMap("swarm/connect?arg="+multiAddr);
768797
return m;
@@ -772,6 +801,24 @@ public Map disconnect(MultiAddress multiAddr) throws IOException {
772801
Map m = retrieveMap("swarm/disconnect?arg="+multiAddr);
773802
return m;
774803
}
804+
public Map filters() throws IOException {
805+
return retrieveMap("swarm/filters");
806+
}
807+
public Map filtersAdd(String multiAddrFilter) throws IOException {
808+
return retrieveMap("swarm/filters/add?arg="+multiAddrFilter);
809+
}
810+
public Map filtersRm(String multiAddrFilter) throws IOException {
811+
return retrieveMap("swarm/filters/rm?arg="+multiAddrFilter);
812+
}
813+
public Map peeringLs() throws IOException {
814+
return retrieveMap("swarm/peering/ls");
815+
}
816+
public Map peeringAdd(MultiAddress multiAddr) throws IOException {
817+
return retrieveMap("swarm/peering/add?arg="+multiAddr);
818+
}
819+
public Map peeringRm(Multihash multiAddr) throws IOException {
820+
return retrieveMap("swarm/peering/rm?arg="+multiAddr);
821+
}
775822
}
776823

777824
public class Dag {
@@ -792,7 +839,7 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
792839
}
793840

794841
public MerkleNode put(String inputFormat, byte[] object, String outputFormat) throws IOException {
795-
String prefix = protocol + "://" + host + ":" + port + version;
842+
String prefix = protocol + "://" + host + ":" + port + apiVersion;
796843
Multipart m = new Multipart(prefix + "dag/put/?stream-channels=true&input-codec=" + inputFormat + "&store-codec=" + outputFormat, "UTF-8");
797844
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(object));
798845
String res = m.finish();
@@ -886,7 +933,7 @@ public Map profileApply(String profile, boolean dryRun) throws IOException {
886933
}
887934

888935
public void replace(NamedStreamable file) throws IOException {
889-
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"config/replace?stream-channels=true", "UTF-8");
936+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + apiVersion+"config/replace?stream-channels=true", "UTF-8");
890937
m.addFilePart("file", Paths.get(""), file);
891938
String res = m.finish();
892939
}
@@ -957,12 +1004,12 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
9571004
}
9581005

9591006
private String retrieveString(String path) throws IOException {
960-
URL target = new URL(protocol, host, port, version + path);
1007+
URL target = new URL(protocol, host, port, apiVersion + path);
9611008
return new String(IPFS.get(target, connectTimeoutMillis, readTimeoutMillis));
9621009
}
9631010

9641011
private byte[] retrieve(String path) throws IOException {
965-
URL target = new URL(protocol, host, port, version + path);
1012+
URL target = new URL(protocol, host, port, apiVersion + path);
9661013
return IPFS.get(target, connectTimeoutMillis, readTimeoutMillis);
9671014
}
9681015

@@ -1055,7 +1102,7 @@ private List<Object> getAndParseStream(String path) throws IOException {
10551102
}
10561103

10571104
private InputStream retrieveStream(String path) throws IOException {
1058-
URL target = new URL(protocol, host, port, version + path);
1105+
URL target = new URL(protocol, host, port, apiVersion + path);
10591106
return IPFS.getStream(target, connectTimeoutMillis, readTimeoutMillis);
10601107
}
10611108

@@ -1069,7 +1116,7 @@ private static InputStream getStream(URL target, int connectTimeoutMillis, int r
10691116
}
10701117

10711118
private Map postMap(String path, byte[] body, Map<String, String> headers) throws IOException {
1072-
URL target = new URL(protocol, host, port, version + path);
1119+
URL target = new URL(protocol, host, port, apiVersion + path);
10731120
return (Map) JSONParser.parse(new String(post(target, body, headers, connectTimeoutMillis, readTimeoutMillis)));
10741121
}
10751122

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.ipfs.api.cbor.*;
44
import io.ipfs.cid.*;
5+
import io.ipfs.multibase.Multibase;
56
import io.ipfs.multihash.Multihash;
67
import io.ipfs.multiaddr.MultiAddress;
78
import org.junit.*;
@@ -777,6 +778,54 @@ public void swarmTest() throws IOException {
777778
List<Peer> peers = ipfs.swarm.peers();
778779
}
779780

781+
@Test
782+
public void versionTest() throws IOException {
783+
Map listenAddrs = ipfs.version.versionDeps();
784+
System.currentTimeMillis();
785+
}
786+
787+
@Test
788+
public void swarmTestFilters() throws IOException {
789+
Map listenAddrs = ipfs.swarm.listenAddrs();
790+
Map localAddrs = ipfs.swarm.localAddrs(true);
791+
String multiAddrFilter = "/ip4/192.168.0.0/ipcidr/16";
792+
Map rm = ipfs.swarm.filtersRm(multiAddrFilter);
793+
Map filters = ipfs.swarm.filters();
794+
List<String> filtersList = (List<String>)filters.get("Strings");
795+
Assert.assertTrue("Filters empty", filtersList == null);
796+
797+
Map added = ipfs.swarm.filtersAdd(multiAddrFilter);
798+
filters = ipfs.swarm.filters();
799+
filtersList = (List<String>)filters.get("Strings");
800+
Assert.assertTrue("Filters NOT empty", !filtersList.isEmpty());
801+
rm = ipfs.swarm.filtersRm(multiAddrFilter);
802+
}
803+
804+
@Test
805+
@Ignore
806+
public void swarmTestPeering() throws IOException {
807+
String id = "INSERT_VAL_HERE";
808+
Multihash hash = Multihash.fromBase58(id);
809+
String peer = "/ip6/::1/tcp/4001/p2p/" + id;
810+
MultiAddress ma = new MultiAddress(peer);
811+
Map addPeering = ipfs.swarm.peeringAdd(ma);
812+
Map lsPeering = ipfs.swarm.peeringLs();
813+
List<String> peeringList = (List<String>)lsPeering.get("Peers");
814+
Assert.assertTrue("Filters not empty", !peeringList.isEmpty());
815+
Map rmPeering = ipfs.swarm.peeringRm(hash);
816+
lsPeering = ipfs.swarm.peeringLs();
817+
peeringList = (List<String>)lsPeering.get("Peers");
818+
Assert.assertTrue("Filters empty", peeringList.isEmpty());
819+
}
820+
821+
@Test
822+
public void bitswapTest() throws IOException {
823+
List<Peer> peers = ipfs.swarm.peers();
824+
Map ledger = ipfs.bitswap.ledger(peers.get(0).id);
825+
Map want = ipfs.bitswap.wantlist(peers.get(0).id);
826+
//String reprovide = ipfs.bitswap.reprovide();
827+
Map stat = ipfs.bitswap.stat();
828+
}
780829
@Test
781830
public void bootstrapTest() throws IOException {
782831
List<MultiAddress> bootstrap = ipfs.bootstrap.list();

0 commit comments

Comments
 (0)