11package io .ipfs .api ;
22
3+ import io .ipfs .cid .*;
34import io .ipfs .multihash .Multihash ;
45import io .ipfs .multiaddr .MultiAddress ;
56
67import java .io .*;
78import java .net .*;
89import java .util .*;
9- import java .util .function .*;
1010import java .util .stream .*;
1111
1212public class IPFS {
1313
14- public static final String MIN_VERSION = "0.4.3 " ;
14+ public static final String MIN_VERSION = "0.4.5 " ;
1515 public enum PinType {all , direct , indirect , recursive }
1616 public List <String > ObjectTemplates = Arrays .asList ("unixfs-dir" );
1717 public List <String > ObjectPatchTypes = Arrays .asList ("add-link" , "rm-link" , "set-data" , "append-data" );
@@ -25,6 +25,7 @@ public enum PinType {all, direct, indirect, recursive}
2525 public final Swarm swarm = new Swarm ();
2626 public final Bootstrap bootstrap = new Bootstrap ();
2727 public final Block block = new Block ();
28+ public final Dag dag = new Dag ();
2829 public final Diag diag = new Diag ();
2930 public final Config config = new Config ();
3031 public final Refs refs = new Refs ();
@@ -138,7 +139,7 @@ public List<Multihash> local() throws IOException {
138139 String jsonStream = new String (retrieve ("refs/local" ));
139140 return JSONParser .parseStream (jsonStream ).stream ()
140141 .map (m -> (String ) (((Map ) m ).get ("Ref" )))
141- .map (Multihash :: fromBase58 )
142+ .map (Cid :: decode )
142143 .collect (Collectors .toList ());
143144 }
144145 }
@@ -149,7 +150,7 @@ public class Pin {
149150 public List <Multihash > add (Multihash hash ) throws IOException {
150151 return ((List <Object >)((Map )retrieveAndParse ("pin/add?stream-channels=true&arg=" + hash )).get ("Pins" ))
151152 .stream ()
152- .map (x -> Multihash . fromBase58 ((String )x ))
153+ .map (x -> Cid . decode ((String )x ))
153154 .collect (Collectors .toList ());
154155 }
155156
@@ -160,7 +161,7 @@ public Map<Multihash, Object> ls() throws IOException {
160161 public Map <Multihash , Object > ls (PinType type ) throws IOException {
161162 return ((Map <String , Object >)(((Map )retrieveAndParse ("pin/ls?stream-channels=true&t=" +type .name ())).get ("Keys" ))).entrySet ()
162163 .stream ()
163- .collect (Collectors .toMap (x -> Multihash . fromBase58 (x .getKey ()), x -> x .getValue ()));
164+ .collect (Collectors .toMap (x -> Cid . decode (x .getKey ()), x -> x .getValue ()));
164165 }
165166
166167 public List <Multihash > rm (Multihash hash ) throws IOException {
@@ -169,7 +170,7 @@ public List<Multihash> rm(Multihash hash) throws IOException {
169170
170171 public List <Multihash > rm (Multihash hash , boolean recursive ) throws IOException {
171172 Map json = retrieveMap ("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + hash );
172- return ((List <Object >) json .get ("Pins" )).stream ().map (x -> Multihash . fromBase58 ((String ) x )).collect (Collectors .toList ());
173+ return ((List <Object >) json .get ("Pins" )).stream ().map (x -> Cid . decode ((String ) x )).collect (Collectors .toList ());
173174 }
174175 }
175176
@@ -189,7 +190,12 @@ public byte[] get(Multihash hash) throws IOException {
189190 }
190191
191192 public List <MerkleNode > put (List <byte []> data ) throws IOException {
192- Multipart m = new Multipart ("http://" + host + ":" + port + version +"block/put?stream-channels=true" , "UTF-8" );
193+ return put (data , Optional .empty ());
194+ }
195+
196+ public List <MerkleNode > put (List <byte []> data , Optional <String > format ) throws IOException {
197+ String fmt = format .map (f -> "&format=" + f ).orElse ("" );
198+ Multipart m = new Multipart ("http://" + host + ":" + port + version +"block/put?stream-channels=true" + fmt , "UTF-8" );
193199 for (byte [] f : data )
194200 m .addFilePart ("file" , new NamedStreamable .ByteArrayWrapper (f ));
195201 String res = m .finish ();
@@ -352,9 +358,9 @@ public List<MultiAddress> rm(MultiAddress addr, boolean all) throws IOException
352358 ipfs peers in the internet.
353359 */
354360 public class Swarm {
355- public List <MultiAddress > peers () throws IOException {
361+ public List <Peer > peers () throws IOException {
356362 Map m = retrieveMap ("swarm/peers?stream-channels=true" );
357- return ((List <Object >)m .get ("Strings " )).stream ().map (x -> new MultiAddress (( String ) x ) ).collect (Collectors .toList ());
363+ return ((List <Object >)m .get ("Peers " )).stream ().map (Peer :: fromJSON ).collect (Collectors .toList ());
358364 }
359365
360366 public Map addrs () throws IOException {
@@ -373,6 +379,33 @@ public Map disconnect(String multiAddr) throws IOException {
373379 }
374380 }
375381
382+ public class Dag {
383+ public byte [] get (Cid cid ) throws IOException {
384+ return retrieve ("block/get?stream-channels=true&arg=" + cid );
385+ }
386+
387+ public MerkleNode put (byte [] object ) throws IOException {
388+ return put ("json" , object , "cbor" );
389+ }
390+
391+ public MerkleNode put (String inputFormat , byte [] object ) throws IOException {
392+ return put (inputFormat , object , "cbor" );
393+ }
394+
395+ public MerkleNode put (byte [] object , String outputFormat ) throws IOException {
396+ return put ("json" , object , outputFormat );
397+ }
398+
399+ public MerkleNode put (String inputFormat , byte [] object , String outputFormat ) throws IOException {
400+ block .put (Arrays .asList (object ));
401+ String prefix = "http://" + host + ":" + port + version ;
402+ Multipart m = new Multipart (prefix + "block/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat , "UTF-8" );
403+ m .addFilePart ("file" , new NamedStreamable .ByteArrayWrapper (object ));
404+ String res = m .finish ();
405+ return MerkleNode .fromJSON (JSONParser .parse (res ));
406+ }
407+ }
408+
376409 public class Diag {
377410 public String net () throws IOException {
378411 return new String (retrieve ("diag/net?stream-channels=true" ));
0 commit comments