@@ -17,6 +17,7 @@ public class IPFS {
17
17
18
18
public static final Version MIN_VERSION = Version .parse ("0.4.11" );
19
19
public enum PinType {all , direct , indirect , recursive }
20
+ public enum PinStatus {queued , pinning , pinned , failed }
20
21
public List <String > ObjectTemplates = Arrays .asList ("unixfs-dir" );
21
22
public List <String > ObjectPatchTypes = Arrays .asList ("add-link" , "rm-link" , "set-data" , "append-data" );
22
23
private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 10_000 ;
@@ -25,7 +26,7 @@ public enum PinType {all, direct, indirect, recursive}
25
26
public final String host ;
26
27
public final int port ;
27
28
public final String protocol ;
28
- private final String version ;
29
+ private final String apiVersion ;
29
30
private final int connectTimeoutMillis ;
30
31
private final int readTimeoutMillis ;
31
32
public final Key key = new Key ();
@@ -36,6 +37,7 @@ public enum PinType {all, direct, indirect, recursive}
36
37
public final IPFSObject object = new IPFSObject ();
37
38
public final Swarm swarm = new Swarm ();
38
39
public final Bootstrap bootstrap = new Bootstrap ();
40
+ public final Bitswap bitswap = new Bitswap ();
39
41
public final Block block = new Block ();
40
42
public final CidAPI cid = new CidAPI ();
41
43
public final Dag dag = new Dag ();
@@ -50,6 +52,7 @@ public enum PinType {all, direct, indirect, recursive}
50
52
public final Stats stats = new Stats ();
51
53
public final Name name = new Name ();
52
54
public final Pubsub pubsub = new Pubsub ();
55
+ public final VersionAPI version = new VersionAPI ();
53
56
54
57
public IPFS (String host , int port ) {
55
58
this (host , port , "/api/v0/" , false );
@@ -81,7 +84,7 @@ public IPFS(String host, int port, String version, int connectTimeoutMillis, int
81
84
this .protocol = "http" ;
82
85
}
83
86
84
- this .version = version ;
87
+ this .apiVersion = version ;
85
88
// Check IPFS is sufficiently recent
86
89
try {
87
90
Version detected = Version .parse (version ());
@@ -98,7 +101,7 @@ public IPFS(String host, int port, String version, int connectTimeoutMillis, int
98
101
* @return current IPFS object with configured timeout
99
102
*/
100
103
public IPFS timeout (int timeout ) {
101
- return new IPFS (host , port , version , timeout , timeout , protocol .equals ("https" ));
104
+ return new IPFS (host , port , apiVersion , timeout , timeout , protocol .equals ("https" ));
102
105
}
103
106
104
107
public String shutdown () throws IOException {
@@ -118,7 +121,7 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
118
121
}
119
122
120
123
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" );
124
+ Multipart m = new Multipart (protocol + "://" + host + ":" + port + apiVersion + "add?stream-channels=true&w=" +wrap + "&n=" +hashOnly , "UTF-8" );
122
125
for (NamedStreamable file : files ) {
123
126
if (file .isDirectory ()) {
124
127
m .addSubtree (Paths .get ("" ), file );
@@ -204,7 +207,10 @@ public List<Multihash> add(Multihash hash) throws IOException {
204
207
.map (x -> Cid .decode ((String ) x ))
205
208
.collect (Collectors .toList ());
206
209
}
207
-
210
+ public Map addRemote (String service , Multihash hash , Optional <String > name , boolean background ) throws IOException {
211
+ String nameArg = name .isPresent () ? "&name=" + name .get () : "" ;
212
+ return retrieveMap ("pin/remote/add?arg=" + hash + "&service=" + service + nameArg + "&background=" + background );
213
+ }
208
214
public Map <Multihash , Object > ls () throws IOException {
209
215
return ls (PinType .direct );
210
216
}
@@ -215,6 +221,13 @@ public Map<Multihash, Object> ls(PinType type) throws IOException {
215
221
.collect (Collectors .toMap (x -> Cid .decode (x .getKey ()), x -> x .getValue ()));
216
222
}
217
223
224
+ public Map lsRemote (String service , Optional <String > name , Optional <List <PinStatus >> statusList ) throws IOException {
225
+ String nameArg = name .isPresent () ? "&name=" + name .get () : "" ;
226
+ String statusArg = statusList .isPresent () ? statusList .get ().stream ().
227
+ map (p -> "&status=" + p ).collect (Collectors .joining ()) : "" ;
228
+ return retrieveMap ("pin/remote/ls?service=" + service + nameArg + statusArg );
229
+ }
230
+
218
231
public List <Multihash > rm (Multihash hash ) throws IOException {
219
232
return rm (hash , true );
220
233
}
@@ -224,12 +237,36 @@ public List<Multihash> rm(Multihash hash, boolean recursive) throws IOException
224
237
return ((List <Object >) json .get ("Pins" )).stream ().map (x -> Cid .decode ((String ) x )).collect (Collectors .toList ());
225
238
}
226
239
240
+ public String rmRemote (String service , Optional <String > name , Optional <List <PinStatus >> statusList , Optional <List <Multihash >> cidList ) throws IOException {
241
+ String nameArg = name .isPresent () ? "&name=" + name .get () : "" ;
242
+ String statusArg = statusList .isPresent () ? statusList .get ().stream ().
243
+ map (p -> "&status=" + p ).collect (Collectors .joining ()) : "" ;
244
+ String cidArg = cidList .isPresent () ? cidList .get ().stream ().
245
+ map (p -> "&cid=" + p .toBase58 ()).collect (Collectors .joining ()) : "" ;
246
+ return retrieveString ("pin/remote/rm?service=" + service + nameArg + statusArg + cidArg );
247
+ }
248
+
249
+ public String addRemoteService (String service , String endPoint , String key ) throws IOException {
250
+ return retrieveString ("pin/remote/service/add?arg=" + service + "&arg=" + endPoint + "&arg=" + key );
251
+ }
252
+
253
+ public Map lsRemoteService (boolean stat ) throws IOException {
254
+ return retrieveMap ("pin/remote/service/ls?stat=" + stat );
255
+ }
256
+
257
+ public String rmRemoteService (String service ) throws IOException {
258
+ return retrieveString ("pin/remote/service/rm?arg=" + service );
259
+ }
260
+
227
261
public List <Multihash > update (Multihash existing , Multihash modified , boolean unpin ) throws IOException {
228
262
return ((List <Object >)((Map )retrieveAndParse ("pin/update?stream-channels=true&arg=" + existing + "&arg=" + modified + "&unpin=" + unpin )).get ("Pins" ))
229
263
.stream ()
230
264
.map (x -> Cid .decode ((String ) x ))
231
265
.collect (Collectors .toList ());
232
266
}
267
+ public Map verify (boolean verbose , boolean quite ) throws IOException {
268
+ return retrieveMap ("pin/verify?verbose=" + verbose + "&quite=" + quite );
269
+ }
233
270
}
234
271
235
272
/* 'ipfs key' is a command for dealing with IPNS keys.
@@ -269,7 +306,7 @@ public Map ls() throws IOException {
269
306
270
307
public class MultibaseAPI {
271
308
public String decode (NamedStreamable encoded_file ) {
272
- Multipart m = new Multipart (protocol + "://" + host + ":" + port + version +
309
+ Multipart m = new Multipart (protocol + "://" + host + ":" + port + apiVersion +
273
310
"multibase/decode" , "UTF-8" );
274
311
try {
275
312
if (encoded_file .isDirectory ()) {
@@ -284,7 +321,7 @@ public String decode(NamedStreamable encoded_file) {
284
321
}
285
322
public String encode (Optional <String > encoding , NamedStreamable file ) {
286
323
String b = encoding .map (f -> "?b=" + f ).orElse ("?b=base64url" );
287
- Multipart m = new Multipart (protocol + "://" + host + ":" + port + version +
324
+ Multipart m = new Multipart (protocol + "://" + host + ":" + port + apiVersion +
288
325
"multibase/encode" + b , "UTF-8" );
289
326
try {
290
327
if (file .isDirectory ()) {
@@ -302,7 +339,7 @@ public List<Map> list(boolean prefix, boolean numeric) throws IOException {
302
339
}
303
340
public String transcode (Optional <String > encoding , NamedStreamable file ) {
304
341
String b = encoding .map (f -> "?b=" + f ).orElse ("?b=base64url" );
305
- Multipart m = new Multipart (protocol + "://" + host + ":" + port + version +
342
+ Multipart m = new Multipart (protocol + "://" + host + ":" + port + apiVersion +
306
343
"multibase/transcode" + b , "UTF-8" );
307
344
try {
308
345
if (file .isDirectory ()) {
@@ -341,6 +378,13 @@ public Map version() throws IOException {
341
378
}
342
379
}
343
380
381
+
382
+ public class VersionAPI {
383
+ public Map versionDeps () throws IOException {
384
+ return retrieveMap ("version/deps" );
385
+ }
386
+ }
387
+
344
388
public class Pubsub {
345
389
public Object ls () throws IOException {
346
390
return retrieveAndParse ("pubsub/ls" );
@@ -363,7 +407,7 @@ public Object peers(String topic) throws IOException {
363
407
*/
364
408
public void pub (String topic , String data ) {
365
409
String encodedTopic = Multibase .encode (Multibase .Base .Base64Url , topic .getBytes ());
366
- Multipart m = new Multipart (protocol +"://" + host + ":" + port + version +"pubsub/pub?arg=" + encodedTopic , "UTF-8" );
410
+ Multipart m = new Multipart (protocol +"://" + host + ":" + port + apiVersion +"pubsub/pub?arg=" + encodedTopic , "UTF-8" );
367
411
try {
368
412
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (data .getBytes ()));
369
413
String res = m .finish ();
@@ -445,7 +489,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
445
489
446
490
public MerkleNode put (byte [] data , Optional <String > format ) throws IOException {
447
491
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" );
492
+ Multipart m = new Multipart (protocol +"://" + host + ":" + port + apiVersion +"block/put?stream-channels=true" + fmt , "UTF-8" );
449
493
try {
450
494
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (data ));
451
495
String res = m .finish ();
@@ -465,7 +509,7 @@ public Map stat(Multihash hash) throws IOException {
465
509
public class IPFSObject {
466
510
@ Deprecated
467
511
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" );
512
+ Multipart m = new Multipart (protocol +"://" + host + ":" + port + apiVersion +"object/put?stream-channels=true" , "UTF-8" );
469
513
for (byte [] f : data )
470
514
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (f ));
471
515
String res = m .finish ();
@@ -475,7 +519,7 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
475
519
public List <MerkleNode > put (String encoding , List <byte []> data ) throws IOException {
476
520
if (!"json" .equals (encoding ) && !"protobuf" .equals (encoding ))
477
521
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" );
522
+ Multipart m = new Multipart (protocol +"://" + host + ":" + port + apiVersion +"object/put?stream-channels=true&encoding=" +encoding , "UTF-8" );
479
523
for (byte [] f : data )
480
524
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (f ));
481
525
String res = m .finish ();
@@ -529,7 +573,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
529
573
case "append-data" :
530
574
if (!data .isPresent ())
531
575
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" );
576
+ Multipart m = new Multipart (protocol +"://" + host + ":" + port + apiVersion +"object/patch/" +command +"?arg=" +base .toBase58 ()+"&stream-channels=true" , "UTF-8" );
533
577
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (data .get ()));
534
578
String res = m .finish ();
535
579
return MerkleNode .fromJSON (JSONParser .parse (res ));
@@ -665,7 +709,7 @@ public Map stat(String path) throws IOException {
665
709
public String write (String path , NamedStreamable uploadFile , boolean create , boolean parents ) throws IOException {
666
710
String arg = URLEncoder .encode (path , "UTF-8" );
667
711
String rpcParams = "files/write?arg=" + arg + "&create=" + create + "&parents=" + parents ;
668
- URL target = new URL (protocol ,host ,port ,version + rpcParams );
712
+ URL target = new URL (protocol ,host ,port ,apiVersion + rpcParams );
669
713
Multipart m = new Multipart (target .toString (),"UTF-8" );
670
714
if (uploadFile .isDirectory ()) {
671
715
throw new IllegalArgumentException ("Input must be a file" );
@@ -704,6 +748,21 @@ public List<MultiAddress> bootstrap() throws IOException {
704
748
}).collect (Collectors .toList ());
705
749
}
706
750
751
+ public class Bitswap {
752
+ public Map ledger (Multihash peerId ) throws IOException {
753
+ return retrieveMap ("bitswap/ledger?arg=" +peerId );
754
+ }
755
+
756
+ public String reprovide () throws IOException {
757
+ return retrieveString ("bitswap/reprovide" );
758
+ }
759
+ public Map stat () throws IOException {
760
+ return retrieveMap ("bitswap/stat" );
761
+ }
762
+ public Map wantlist (Multihash peerId ) throws IOException {
763
+ return retrieveMap ("bitswap/wantlist?peer=" + peerId );
764
+ }
765
+ }
707
766
public class Bootstrap {
708
767
709
768
public List <MultiAddress > add (MultiAddress addr ) throws IOException {
@@ -762,7 +821,12 @@ public Map<Multihash, List<MultiAddress>> addrs() throws IOException {
762
821
.map (MultiAddress ::new )
763
822
.collect (Collectors .toList ())));
764
823
}
765
-
824
+ public Map listenAddrs () throws IOException {
825
+ return retrieveMap ("swarm/addrs/listen" );
826
+ }
827
+ public Map localAddrs (boolean showPeerId ) throws IOException {
828
+ return retrieveMap ("swarm/addrs/local?id=" + showPeerId );
829
+ }
766
830
public Map connect (MultiAddress multiAddr ) throws IOException {
767
831
Map m = retrieveMap ("swarm/connect?arg=" +multiAddr );
768
832
return m ;
@@ -772,6 +836,24 @@ public Map disconnect(MultiAddress multiAddr) throws IOException {
772
836
Map m = retrieveMap ("swarm/disconnect?arg=" +multiAddr );
773
837
return m ;
774
838
}
839
+ public Map filters () throws IOException {
840
+ return retrieveMap ("swarm/filters" );
841
+ }
842
+ public Map filtersAdd (String multiAddrFilter ) throws IOException {
843
+ return retrieveMap ("swarm/filters/add?arg=" +multiAddrFilter );
844
+ }
845
+ public Map filtersRm (String multiAddrFilter ) throws IOException {
846
+ return retrieveMap ("swarm/filters/rm?arg=" +multiAddrFilter );
847
+ }
848
+ public Map peeringLs () throws IOException {
849
+ return retrieveMap ("swarm/peering/ls" );
850
+ }
851
+ public Map peeringAdd (MultiAddress multiAddr ) throws IOException {
852
+ return retrieveMap ("swarm/peering/add?arg=" +multiAddr );
853
+ }
854
+ public Map peeringRm (Multihash multiAddr ) throws IOException {
855
+ return retrieveMap ("swarm/peering/rm?arg=" +multiAddr );
856
+ }
775
857
}
776
858
777
859
public class Dag {
@@ -792,7 +874,7 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
792
874
}
793
875
794
876
public MerkleNode put (String inputFormat , byte [] object , String outputFormat ) throws IOException {
795
- String prefix = protocol + "://" + host + ":" + port + version ;
877
+ String prefix = protocol + "://" + host + ":" + port + apiVersion ;
796
878
Multipart m = new Multipart (prefix + "dag/put/?stream-channels=true&input-codec=" + inputFormat + "&store-codec=" + outputFormat , "UTF-8" );
797
879
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (object ));
798
880
String res = m .finish ();
@@ -821,6 +903,10 @@ public String clearCmds() throws IOException {
821
903
return retrieveString ("diag/cmds/clear" );
822
904
}
823
905
906
+ public String profile () throws IOException {
907
+ return retrieveString ("diag/profile" );
908
+ }
909
+
824
910
public Map sys () throws IOException {
825
911
return retrieveMap ("diag/sys?stream-channels=true" );
826
912
}
@@ -886,7 +972,7 @@ public Map profileApply(String profile, boolean dryRun) throws IOException {
886
972
}
887
973
888
974
public void replace (NamedStreamable file ) throws IOException {
889
- Multipart m = new Multipart (protocol +"://" + host + ":" + port + version +"config/replace?stream-channels=true" , "UTF-8" );
975
+ Multipart m = new Multipart (protocol +"://" + host + ":" + port + apiVersion +"config/replace?stream-channels=true" , "UTF-8" );
890
976
m .addFilePart ("file" , Paths .get ("" ), file );
891
977
String res = m .finish ();
892
978
}
@@ -957,12 +1043,12 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
957
1043
}
958
1044
959
1045
private String retrieveString (String path ) throws IOException {
960
- URL target = new URL (protocol , host , port , version + path );
1046
+ URL target = new URL (protocol , host , port , apiVersion + path );
961
1047
return new String (IPFS .get (target , connectTimeoutMillis , readTimeoutMillis ));
962
1048
}
963
1049
964
1050
private byte [] retrieve (String path ) throws IOException {
965
- URL target = new URL (protocol , host , port , version + path );
1051
+ URL target = new URL (protocol , host , port , apiVersion + path );
966
1052
return IPFS .get (target , connectTimeoutMillis , readTimeoutMillis );
967
1053
}
968
1054
@@ -1055,7 +1141,7 @@ private List<Object> getAndParseStream(String path) throws IOException {
1055
1141
}
1056
1142
1057
1143
private InputStream retrieveStream (String path ) throws IOException {
1058
- URL target = new URL (protocol , host , port , version + path );
1144
+ URL target = new URL (protocol , host , port , apiVersion + path );
1059
1145
return IPFS .getStream (target , connectTimeoutMillis , readTimeoutMillis );
1060
1146
}
1061
1147
@@ -1069,7 +1155,7 @@ private static InputStream getStream(URL target, int connectTimeoutMillis, int r
1069
1155
}
1070
1156
1071
1157
private Map postMap (String path , byte [] body , Map <String , String > headers ) throws IOException {
1072
- URL target = new URL (protocol , host , port , version + path );
1158
+ URL target = new URL (protocol , host , port , apiVersion + path );
1073
1159
return (Map ) JSONParser .parse (new String (post (target , body , headers , connectTimeoutMillis , readTimeoutMillis )));
1074
1160
}
1075
1161
0 commit comments