Skip to content

Commit 29a77ac

Browse files
authored
Merge pull request #206 from ipfs-shipyard/feat/audit-api-changes
review of recent API changes
2 parents 9b74653 + 2f4a448 commit 29a77ac

File tree

3 files changed

+139
-14
lines changed

3 files changed

+139
-14
lines changed

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,13 @@ public String chcid(String path) throws IOException {
662662
return retrieveString("files/chcid?args=" + arg);
663663
}
664664

665+
public String chcid(String path, Optional<Integer> cidVersion, Optional<String> hash) throws IOException {
666+
String arg = URLEncoder.encode(path, "UTF-8");
667+
String cid = cidVersion.isPresent() ? "&cid-version=" + cidVersion.get() : "";
668+
String hashFunc = hash.isPresent() ? "&hash=" + hash.get() : "";
669+
return retrieveString("files/chcid?args=" + arg + cid + hashFunc);
670+
}
671+
665672
public String cp(String source, String dest, boolean parents) throws IOException {
666673
return retrieveString("files/cp?arg=" + URLEncoder.encode(source, "UTF-8") + "&arg=" +
667674
URLEncoder.encode(dest, "UTF-8") + "&parents=" + parents);
@@ -695,10 +702,11 @@ public String mkdir(String path, boolean parents) throws IOException {
695702
return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents);
696703
}
697704

698-
public String mkdir(String path, boolean parents, int cidVersion, Multihash hash) throws IOException {
705+
public String mkdir(String path, boolean parents, Optional<Integer> cidVersion, Optional<String> hash) throws IOException {
699706
String arg = URLEncoder.encode(path, "UTF-8");
700-
return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents + "&cid-version=" +
701-
cidVersion + "&hash=" + hash);
707+
String cid = cidVersion.isPresent() ? "&cid-version=" + cidVersion.get() : "";
708+
String hashFunc = hash.isPresent() ? "&hash=" + hash.get() : "";
709+
return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents + cid + hashFunc);
702710
}
703711

704712
public String mv(String source, String dest) throws IOException {
@@ -725,7 +733,11 @@ public Map stat(String path) throws IOException {
725733
String arg = URLEncoder.encode(path, "UTF-8");
726734
return retrieveMap("files/stat?arg=" + arg);
727735
}
728-
736+
public Map stat(String path, Optional<String> format, boolean withLocal) throws IOException {
737+
String arg = URLEncoder.encode(path, "UTF-8");
738+
String formatStr = format.isPresent() ? "&format=" + format.get() : "";
739+
return retrieveMap("files/stat?arg=" + arg + formatStr + "&with-local=" + withLocal);
740+
}
729741
public String write(String path, NamedStreamable uploadFile, boolean create, boolean parents) throws IOException {
730742
String arg = URLEncoder.encode(path, "UTF-8");
731743
String rpcParams = "files/write?arg=" + arg + "&create=" + create + "&parents=" + parents;
@@ -738,6 +750,19 @@ public String write(String path, NamedStreamable uploadFile, boolean create, boo
738750
}
739751
return m.finish();
740752
}
753+
754+
public String write(String path, NamedStreamable uploadFile, WriteFilesArgs args) throws IOException {
755+
String arg = URLEncoder.encode(path, "UTF-8");
756+
String rpcParams = "files/write?arg=" + arg + "&" + args.toQueryString();
757+
URL target = new URL(protocol,host,port,apiVersion + rpcParams);
758+
Multipart m = new Multipart(target.toString(),"UTF-8");
759+
if (uploadFile.isDirectory()) {
760+
throw new IllegalArgumentException("Input must be a file");
761+
} else {
762+
m.addFilePart("file", Paths.get(""), uploadFile);
763+
}
764+
return m.finish();
765+
}
741766
}
742767

743768
public class FileStore {
@@ -746,12 +771,12 @@ public Map dups() throws IOException {
746771
return retrieveMap("filestore/dups");
747772
}
748773

749-
public Map ls() throws IOException {
750-
return retrieveMap("filestore/ls");
774+
public Map ls(boolean fileOrder) throws IOException {
775+
return retrieveMap("filestore/ls?file-order=" + fileOrder);
751776
}
752777

753-
public Map verify() throws IOException {
754-
return retrieveMap("filestore/verify");
778+
public Map verify(boolean fileOrder) throws IOException {
779+
return retrieveMap("filestore/verify?file-order=" + fileOrder);
755780
}
756781
}
757782

@@ -779,6 +804,9 @@ public String reprovide() throws IOException {
779804
public Map stat() throws IOException {
780805
return retrieveMap("bitswap/stat");
781806
}
807+
public Map stat(boolean verbose, boolean humanReadable) throws IOException {
808+
return retrieveMap("bitswap/stat?verbose=" + verbose + "&human=" + humanReadable);
809+
}
782810
public Map wantlist(Multihash peerId) throws IOException {
783811
return retrieveMap("bitswap/wantlist?peer=" + peerId);
784812
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.ipfs.api;
2+
3+
import java.net.URLEncoder;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
6+
7+
/*
8+
Example usage:
9+
WriteFilesArgs args = WriteFilesArgs.Builder.newInstance()
10+
.setCreate()
11+
.setParents()
12+
.build();
13+
*/
14+
final class WriteFilesArgs {
15+
16+
private final Map<String, String> args = new HashMap<>();
17+
18+
public WriteFilesArgs(Builder builder)
19+
{
20+
args.putAll(builder.args);
21+
}
22+
@Override
23+
public String toString()
24+
{
25+
List<String> asList = args.entrySet()
26+
.stream()
27+
.sorted(Comparator.comparing(Map.Entry::getKey))
28+
.map(e -> e.getKey() + " = " + e.getValue()).collect(Collectors.toList());
29+
return Arrays.toString(asList.toArray());
30+
}
31+
public String toQueryString()
32+
{
33+
StringBuilder sb = new StringBuilder();
34+
for (Map.Entry<String, String> entry: args.entrySet()) {
35+
sb.append("&").append(entry.getKey())
36+
.append("=")
37+
.append(URLEncoder.encode(entry.getValue()));
38+
}
39+
return sb.length() > 0 ? sb.toString().substring(1) : sb.toString();
40+
}
41+
public static class Builder {
42+
private static final String TRUE = "true";
43+
private final Map<String, String> args = new HashMap<>();
44+
private Builder() {}
45+
public static Builder newInstance()
46+
{
47+
return new Builder();
48+
}
49+
50+
public Builder setOffset(int offset) {
51+
args.put("offset", String.valueOf(offset));
52+
return this;
53+
}
54+
public Builder setCreate() {
55+
args.put("create", TRUE);
56+
return this;
57+
}
58+
public Builder setParents() {
59+
args.put("parents", TRUE);
60+
return this;
61+
}
62+
public Builder setTruncate() {
63+
args.put("truncate", TRUE);
64+
return this;
65+
}
66+
public Builder setCount(int count) {
67+
args.put("count", String.valueOf(count));
68+
return this;
69+
}
70+
public Builder setRawLeaves() {
71+
args.put("raw-leaves", TRUE);
72+
return this;
73+
}
74+
public Builder setCidVersion(int version) {
75+
args.put("cid-version", String.valueOf(version));
76+
return this;
77+
}
78+
public Builder setHash(String hashFunction) {
79+
args.put("hash", hashFunction);
80+
return this;
81+
}
82+
public WriteFilesArgs build()
83+
{
84+
return new WriteFilesArgs(this);
85+
}
86+
}
87+
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public void filesTest() throws IOException {
241241
NamedStreamable ns = new NamedStreamable.ByteArrayWrapper(filename, contents.getBytes());
242242
String res = ipfs.files.write(path, ns, true, true);
243243
Map stat = ipfs.files.stat( path);
244+
Map stat2 = ipfs.files.stat( path, Optional.of("<cumulsize>"), true);
244245
String readContents = new String(ipfs.files.read(path));
245246
Assert.assertTrue("Should be equals", contents.equals(readContents));
246247
res = ipfs.files.rm(path, false, false);
@@ -255,6 +256,7 @@ public void filesTest() throws IOException {
255256
res = ipfs.files.mv(tempPath, "/" + tempFilename);
256257
stat = ipfs.files.stat("/" + tempFilename);
257258
Map lsMap = ipfs.files.ls("/");
259+
Map lsMap2 = ipfs.files.ls("/", true, false);
258260

259261
String flushFolder = "/filesTest/f/l/u/s/h";
260262
res = ipfs.files.mkdir(flushFolder, true);
@@ -266,11 +268,18 @@ public void filesTest() throws IOException {
266268
String copyFromPath = copyFromFolder + "/" + copyFilename;
267269
String copyToPath = copyToFolder + "/" + copyFilename;
268270
NamedStreamable copyFile = new NamedStreamable.ByteArrayWrapper(copyFilename, "copy".getBytes());
269-
res = ipfs.files.write(copyFromPath, copyFile, true, true);
271+
WriteFilesArgs args = WriteFilesArgs.Builder.newInstance()
272+
.setCreate()
273+
.setParents()
274+
.build();
275+
res = ipfs.files.write(copyFromPath, copyFile, args);
270276
res = ipfs.files.cp(copyFromPath, copyToPath, true);
271277
stat = ipfs.files.stat(copyToPath);
272-
String cid = ipfs.files.chcid(copyToPath);
273-
ipfs.files.rm("/filesTest", true, true);
278+
String cidRes = ipfs.files.chcid(copyToPath);
279+
stat = ipfs.files.stat(copyToPath);
280+
String cidV0Res = ipfs.files.chcid(copyToPath, Optional.of(0), Optional.empty());
281+
stat = ipfs.files.stat(copyToPath);
282+
ipfs.files.rm("/filesTest", false, true);
274283
}
275284

276285
@Test
@@ -290,8 +299,8 @@ public void multibaseTest() throws IOException {
290299
@Ignore("Experimental feature not enabled by default")
291300
public void fileStoreTest() throws IOException {
292301
ipfs.fileStore.dups();
293-
ipfs.fileStore.ls();
294-
ipfs.fileStore.verify();
302+
Map res = ipfs.fileStore.ls(true);
303+
ipfs.fileStore.verify(true);
295304
}
296305

297306
@Test
@@ -843,6 +852,7 @@ public void bitswapTest() throws IOException {
843852
Map want = ipfs.bitswap.wantlist(peers.get(0).id);
844853
//String reprovide = ipfs.bitswap.reprovide();
845854
Map stat = ipfs.bitswap.stat();
855+
Map stat2 = ipfs.bitswap.stat(true, false);
846856
}
847857
@Test
848858
public void bootstrapTest() throws IOException {
@@ -878,7 +888,7 @@ public void diagTest() throws IOException {
878888
ipfs.config.replace(new NamedStreamable.ByteArrayWrapper(JSONParser.toString(config).getBytes()));
879889
// Object log = ipfs.log();
880890
Map sys = ipfs.diag.sys();
881-
List<Map> cmds = ipfs.diag.cmds(true);
891+
List<Map> cmds = ipfs.diag.cmds();
882892
String res = ipfs.diag.clearCmds();
883893
List<Map> cmds2 = ipfs.diag.cmds(true);
884894
//res = ipfs.diag.profile();

0 commit comments

Comments
 (0)