Skip to content

Commit 7ba2bc6

Browse files
committed
add items to multibase api
1 parent 5fa392e commit 7ba2bc6

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public enum PinType {all, direct, indirect, recursive}
3030
private final int readTimeoutMillis;
3131
public final Key key = new Key();
3232
public final Log log = new Log();
33+
public final MultibaseAPI multibase = new MultibaseAPI();
3334
public final Pin pin = new Pin();
3435
public final Repo repo = new Repo();
3536
public final IPFSObject object = new IPFSObject();
@@ -262,6 +263,55 @@ public Map ls() throws IOException {
262263
return retrieveMap("log/ls");
263264
}
264265
}
266+
public class MultibaseAPI {
267+
public String decode(NamedStreamable encoded_file) {
268+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version +
269+
"multibase/decode", "UTF-8");
270+
try {
271+
if (encoded_file.isDirectory()) {
272+
throw new IllegalArgumentException("encoded_file must be a file");
273+
} else {
274+
m.addFilePart("file", Paths.get(""), encoded_file);
275+
return m.finish();
276+
}
277+
} catch (IOException e) {
278+
throw new RuntimeException(e.getMessage(), e);
279+
}
280+
}
281+
public String encode(Optional<String> encoding, NamedStreamable file) {
282+
String b = encoding.map(f -> "?b=" + f).orElse("?b=base64url");
283+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version +
284+
"multibase/encode" + b, "UTF-8");
285+
try {
286+
if (file.isDirectory()) {
287+
throw new IllegalArgumentException("Input must be a file");
288+
} else {
289+
m.addFilePart("file", Paths.get(""), file);
290+
return m.finish();
291+
}
292+
} catch (IOException e) {
293+
throw new RuntimeException(e.getMessage(), e);
294+
}
295+
}
296+
public List<Map> list(boolean prefix, boolean numeric) throws IOException {
297+
return (List)retrieveAndParse("multibase/list?prefix=" + prefix + "&numeric=" + numeric);
298+
}
299+
public String transcode(Optional<String> encoding, NamedStreamable file) {
300+
String b = encoding.map(f -> "?b=" + f).orElse("?b=base64url");
301+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version +
302+
"multibase/transcode" + b, "UTF-8");
303+
try {
304+
if (file.isDirectory()) {
305+
throw new IllegalArgumentException("Input must be a file");
306+
} else {
307+
m.addFilePart("file", Paths.get(""), file);
308+
return m.finish();
309+
}
310+
} catch (IOException e) {
311+
throw new RuntimeException(e.getMessage(), e);
312+
}
313+
}
314+
}
265315

266316
/* 'ipfs repo' is a plumbing command used to manipulate the repo.
267317
*/

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void log() throws IOException {
7676
Map levelResult = ipfs.log.level("all", "info");
7777
Assert.assertTrue("Log level", ((String)levelResult.get("Message")).startsWith("Changed log level"));
7878
}
79-
79+
8080
@Test
8181
public void ipldNode() {
8282
Function<Stream<Pair<String, CborObject>>, CborObject.CborMap> map =
@@ -271,6 +271,19 @@ public void filesTest() throws IOException {
271271
ipfs.files.rm("/filesTest", true, true);
272272
}
273273

274+
@Test
275+
public void multibaseTest() throws IOException {
276+
List<Map> encodings = ipfs.multibase.list(true, false);
277+
Assert.assertTrue("multibase/list works", !encodings.isEmpty());
278+
String encoded = ipfs.multibase.encode(Optional.empty(), new NamedStreamable.ByteArrayWrapper("hello".getBytes()));
279+
Assert.assertTrue("multibase/encode works", encoded.equals("uaGVsbG8"));
280+
String decoded = ipfs.multibase.decode(new NamedStreamable.ByteArrayWrapper(encoded.getBytes()));
281+
Assert.assertTrue("multibase/decode works", decoded.equals("hello"));
282+
String input = "f68656c6c6f";
283+
String transcode = ipfs.multibase.transcode(Optional.of("base64url"), new NamedStreamable.ByteArrayWrapper(input.getBytes()));
284+
Assert.assertTrue("multibase/transcode works", transcode.equals(encoded));
285+
}
286+
274287
@Test
275288
@Ignore("Experimental feature not enabled by default")
276289
public void fileStoreTest() throws IOException {

0 commit comments

Comments
 (0)