From 2f4a4486136b65ed58dc745e61405ce13ba2e005 Mon Sep 17 00:00:00 2001 From: kevodwyer Date: Tue, 24 Jan 2023 12:35:23 +0000 Subject: [PATCH] review of recent API changes --- src/main/java/io/ipfs/api/IPFS.java | 44 ++++++++-- src/main/java/io/ipfs/api/WriteFilesArgs.java | 87 +++++++++++++++++++ src/test/java/io/ipfs/api/APITest.java | 22 +++-- 3 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 src/main/java/io/ipfs/api/WriteFilesArgs.java diff --git a/src/main/java/io/ipfs/api/IPFS.java b/src/main/java/io/ipfs/api/IPFS.java index c7e0020..6e0f000 100644 --- a/src/main/java/io/ipfs/api/IPFS.java +++ b/src/main/java/io/ipfs/api/IPFS.java @@ -662,6 +662,13 @@ public String chcid(String path) throws IOException { return retrieveString("files/chcid?args=" + arg); } + public String chcid(String path, Optional cidVersion, Optional hash) throws IOException { + String arg = URLEncoder.encode(path, "UTF-8"); + String cid = cidVersion.isPresent() ? "&cid-version=" + cidVersion.get() : ""; + String hashFunc = hash.isPresent() ? "&hash=" + hash.get() : ""; + return retrieveString("files/chcid?args=" + arg + cid + hashFunc); + } + public String cp(String source, String dest, boolean parents) throws IOException { return retrieveString("files/cp?arg=" + URLEncoder.encode(source, "UTF-8") + "&arg=" + URLEncoder.encode(dest, "UTF-8") + "&parents=" + parents); @@ -695,10 +702,11 @@ public String mkdir(String path, boolean parents) throws IOException { return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents); } - public String mkdir(String path, boolean parents, int cidVersion, Multihash hash) throws IOException { + public String mkdir(String path, boolean parents, Optional cidVersion, Optional hash) throws IOException { String arg = URLEncoder.encode(path, "UTF-8"); - return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents + "&cid-version=" + - cidVersion + "&hash=" + hash); + String cid = cidVersion.isPresent() ? "&cid-version=" + cidVersion.get() : ""; + String hashFunc = hash.isPresent() ? "&hash=" + hash.get() : ""; + return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents + cid + hashFunc); } public String mv(String source, String dest) throws IOException { @@ -725,7 +733,11 @@ public Map stat(String path) throws IOException { String arg = URLEncoder.encode(path, "UTF-8"); return retrieveMap("files/stat?arg=" + arg); } - + public Map stat(String path, Optional format, boolean withLocal) throws IOException { + String arg = URLEncoder.encode(path, "UTF-8"); + String formatStr = format.isPresent() ? "&format=" + format.get() : ""; + return retrieveMap("files/stat?arg=" + arg + formatStr + "&with-local=" + withLocal); + } public String write(String path, NamedStreamable uploadFile, boolean create, boolean parents) throws IOException { String arg = URLEncoder.encode(path, "UTF-8"); String rpcParams = "files/write?arg=" + arg + "&create=" + create + "&parents=" + parents; @@ -738,6 +750,19 @@ public String write(String path, NamedStreamable uploadFile, boolean create, boo } return m.finish(); } + + public String write(String path, NamedStreamable uploadFile, WriteFilesArgs args) throws IOException { + String arg = URLEncoder.encode(path, "UTF-8"); + String rpcParams = "files/write?arg=" + arg + "&" + args.toQueryString(); + URL target = new URL(protocol,host,port,apiVersion + rpcParams); + Multipart m = new Multipart(target.toString(),"UTF-8"); + if (uploadFile.isDirectory()) { + throw new IllegalArgumentException("Input must be a file"); + } else { + m.addFilePart("file", Paths.get(""), uploadFile); + } + return m.finish(); + } } public class FileStore { @@ -746,12 +771,12 @@ public Map dups() throws IOException { return retrieveMap("filestore/dups"); } - public Map ls() throws IOException { - return retrieveMap("filestore/ls"); + public Map ls(boolean fileOrder) throws IOException { + return retrieveMap("filestore/ls?file-order=" + fileOrder); } - public Map verify() throws IOException { - return retrieveMap("filestore/verify"); + public Map verify(boolean fileOrder) throws IOException { + return retrieveMap("filestore/verify?file-order=" + fileOrder); } } @@ -779,6 +804,9 @@ public String reprovide() throws IOException { public Map stat() throws IOException { return retrieveMap("bitswap/stat"); } + public Map stat(boolean verbose, boolean humanReadable) throws IOException { + return retrieveMap("bitswap/stat?verbose=" + verbose + "&human=" + humanReadable); + } public Map wantlist(Multihash peerId) throws IOException { return retrieveMap("bitswap/wantlist?peer=" + peerId); } diff --git a/src/main/java/io/ipfs/api/WriteFilesArgs.java b/src/main/java/io/ipfs/api/WriteFilesArgs.java new file mode 100644 index 0000000..32ec33b --- /dev/null +++ b/src/main/java/io/ipfs/api/WriteFilesArgs.java @@ -0,0 +1,87 @@ +package io.ipfs.api; + +import java.net.URLEncoder; +import java.util.*; +import java.util.stream.Collectors; + +/* +Example usage: + WriteFilesArgs args = WriteFilesArgs.Builder.newInstance() + .setCreate() + .setParents() + .build(); + */ +final class WriteFilesArgs { + + private final Map args = new HashMap<>(); + + public WriteFilesArgs(Builder builder) + { + args.putAll(builder.args); + } + @Override + public String toString() + { + List asList = args.entrySet() + .stream() + .sorted(Comparator.comparing(Map.Entry::getKey)) + .map(e -> e.getKey() + " = " + e.getValue()).collect(Collectors.toList()); + return Arrays.toString(asList.toArray()); + } + public String toQueryString() + { + StringBuilder sb = new StringBuilder(); + for (Map.Entry entry: args.entrySet()) { + sb.append("&").append(entry.getKey()) + .append("=") + .append(URLEncoder.encode(entry.getValue())); + } + return sb.length() > 0 ? sb.toString().substring(1) : sb.toString(); + } + public static class Builder { + private static final String TRUE = "true"; + private final Map args = new HashMap<>(); + private Builder() {} + public static Builder newInstance() + { + return new Builder(); + } + + public Builder setOffset(int offset) { + args.put("offset", String.valueOf(offset)); + return this; + } + public Builder setCreate() { + args.put("create", TRUE); + return this; + } + public Builder setParents() { + args.put("parents", TRUE); + return this; + } + public Builder setTruncate() { + args.put("truncate", TRUE); + return this; + } + public Builder setCount(int count) { + args.put("count", String.valueOf(count)); + return this; + } + public Builder setRawLeaves() { + args.put("raw-leaves", TRUE); + return this; + } + public Builder setCidVersion(int version) { + args.put("cid-version", String.valueOf(version)); + return this; + } + public Builder setHash(String hashFunction) { + args.put("hash", hashFunction); + return this; + } + public WriteFilesArgs build() + { + return new WriteFilesArgs(this); + } + } +} diff --git a/src/test/java/io/ipfs/api/APITest.java b/src/test/java/io/ipfs/api/APITest.java index 3723a75..ea3d5e9 100644 --- a/src/test/java/io/ipfs/api/APITest.java +++ b/src/test/java/io/ipfs/api/APITest.java @@ -241,6 +241,7 @@ public void filesTest() throws IOException { NamedStreamable ns = new NamedStreamable.ByteArrayWrapper(filename, contents.getBytes()); String res = ipfs.files.write(path, ns, true, true); Map stat = ipfs.files.stat( path); + Map stat2 = ipfs.files.stat( path, Optional.of(""), true); String readContents = new String(ipfs.files.read(path)); Assert.assertTrue("Should be equals", contents.equals(readContents)); res = ipfs.files.rm(path, false, false); @@ -255,6 +256,7 @@ public void filesTest() throws IOException { res = ipfs.files.mv(tempPath, "/" + tempFilename); stat = ipfs.files.stat("/" + tempFilename); Map lsMap = ipfs.files.ls("/"); + Map lsMap2 = ipfs.files.ls("/", true, false); String flushFolder = "/filesTest/f/l/u/s/h"; res = ipfs.files.mkdir(flushFolder, true); @@ -266,11 +268,18 @@ public void filesTest() throws IOException { String copyFromPath = copyFromFolder + "/" + copyFilename; String copyToPath = copyToFolder + "/" + copyFilename; NamedStreamable copyFile = new NamedStreamable.ByteArrayWrapper(copyFilename, "copy".getBytes()); - res = ipfs.files.write(copyFromPath, copyFile, true, true); + WriteFilesArgs args = WriteFilesArgs.Builder.newInstance() + .setCreate() + .setParents() + .build(); + res = ipfs.files.write(copyFromPath, copyFile, args); res = ipfs.files.cp(copyFromPath, copyToPath, true); stat = ipfs.files.stat(copyToPath); - String cid = ipfs.files.chcid(copyToPath); - ipfs.files.rm("/filesTest", true, true); + String cidRes = ipfs.files.chcid(copyToPath); + stat = ipfs.files.stat(copyToPath); + String cidV0Res = ipfs.files.chcid(copyToPath, Optional.of(0), Optional.empty()); + stat = ipfs.files.stat(copyToPath); + ipfs.files.rm("/filesTest", false, true); } @Test @@ -290,8 +299,8 @@ public void multibaseTest() throws IOException { @Ignore("Experimental feature not enabled by default") public void fileStoreTest() throws IOException { ipfs.fileStore.dups(); - ipfs.fileStore.ls(); - ipfs.fileStore.verify(); + Map res = ipfs.fileStore.ls(true); + ipfs.fileStore.verify(true); } @Test @@ -843,6 +852,7 @@ public void bitswapTest() throws IOException { Map want = ipfs.bitswap.wantlist(peers.get(0).id); //String reprovide = ipfs.bitswap.reprovide(); Map stat = ipfs.bitswap.stat(); + Map stat2 = ipfs.bitswap.stat(true, false); } @Test public void bootstrapTest() throws IOException { @@ -878,7 +888,7 @@ public void diagTest() throws IOException { ipfs.config.replace(new NamedStreamable.ByteArrayWrapper(JSONParser.toString(config).getBytes())); // Object log = ipfs.log(); Map sys = ipfs.diag.sys(); - List cmds = ipfs.diag.cmds(true); + List cmds = ipfs.diag.cmds(); String res = ipfs.diag.clearCmds(); List cmds2 = ipfs.diag.cmds(true); //res = ipfs.diag.profile();