Skip to content

Commit 9b74653

Browse files
authored
Merge pull request #205 from ipfs-shipyard/feat/add-args-handling
add builder for arguments to ipfs.add
2 parents b88ac41 + 1db4494 commit 9b74653

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
AddArgs args = AddArgs.Builder.newInstance()
10+
.setInline()
11+
.setCidVersion(1)
12+
.build();
13+
*/
14+
final class AddArgs {
15+
16+
private final Map<String, String> args = new HashMap<>();
17+
18+
public AddArgs(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+
public Builder setQuiet() {
50+
args.put("quiet", TRUE);
51+
return this;
52+
}
53+
public Builder setQuieter() {
54+
args.put("quieter", TRUE);
55+
return this;
56+
}
57+
public Builder setSilent() {
58+
args.put("silent", TRUE);
59+
return this;
60+
}
61+
public Builder setTrickle() {
62+
args.put("trickle", TRUE);
63+
return this;
64+
}
65+
public Builder setOnlyHash() {
66+
args.put("only-hash", TRUE);
67+
return this;
68+
}
69+
public Builder setWrapWithDirectory() {
70+
args.put("wrap-with-directory", TRUE);
71+
return this;
72+
}
73+
public Builder setChunker(String chunker) {
74+
args.put("chunker", chunker);
75+
return this;
76+
}
77+
public Builder setRawLeaves() {
78+
args.put("raw-leaves", TRUE);
79+
return this;
80+
}
81+
public Builder setNocopy() {
82+
args.put("nocopy", TRUE);
83+
return this;
84+
}
85+
public Builder setFscache() {
86+
args.put("fscache", TRUE);
87+
return this;
88+
}
89+
public Builder setCidVersion(int version) {
90+
args.put("cid-version", String.valueOf(version));
91+
return this;
92+
}
93+
public Builder setHash(String hashFunction) {
94+
args.put("hash", hashFunction);
95+
return this;
96+
}
97+
public Builder setInline() {
98+
args.put("inline", TRUE);
99+
return this;
100+
}
101+
public Builder setInlineLimit(int maxBlockSize) {
102+
args.put("inline-limit", String.valueOf(maxBlockSize));
103+
return this;
104+
}
105+
public Builder setPin() {
106+
args.put("pin", TRUE);
107+
return this;
108+
}
109+
public Builder setToFiles(String path) {
110+
args.put("to-files", path);
111+
return this;
112+
}
113+
public AddArgs build()
114+
{
115+
return new AddArgs(this);
116+
}
117+
}
118+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean h
134134
.collect(Collectors.toList());
135135
}
136136

137+
public List<MerkleNode> add(NamedStreamable file, AddArgs args) throws IOException {
138+
return add(Collections.singletonList(file), args);
139+
}
140+
141+
public List<MerkleNode> add(List<NamedStreamable> files, AddArgs args) throws IOException {
142+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion + "add?stream-channels=true&"+ args.toQueryString(), "UTF-8");
143+
for (NamedStreamable file: files) {
144+
if (file.isDirectory()) {
145+
m.addSubtree(Paths.get(""), file);
146+
} else
147+
m.addFilePart("file", Paths.get(""), file);
148+
};
149+
String res = m.finish();
150+
return JSONParser.parseStream(res).stream()
151+
.map(x -> MerkleNode.fromJSON((Map<String, Object>) x))
152+
.collect(Collectors.toList());
153+
}
154+
137155
public List<MerkleNode> ls(Multihash hash) throws IOException {
138156
Map reply = retrieveMap("ls?arg=" + hash);
139157
return ((List<Object>) reply.get("Objects"))

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,18 @@ public void testTimeoutOK() throws IOException {
908908
ipfs.cat(Multihash.fromBase58("Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a"));
909909
}
910910

911+
@Test
912+
public void addArgsTest() {
913+
AddArgs args = AddArgs.Builder.newInstance()
914+
.setInline()
915+
.setCidVersion(1)
916+
.build();
917+
String res = args.toString();
918+
Assert.assertTrue("args toString() format", res.equals("[cid-version = 1, inline = true]"));
919+
String queryStr = args.toQueryString();
920+
Assert.assertTrue("args toQueryString() format", queryStr.equals("inline=true&cid-version=1"));
921+
}
922+
911923
// this api is disabled until deployment over IPFS is enabled
912924
public void updateTest() throws IOException {
913925
Object check = ipfs.update.check();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public void testSingle() throws Exception {
4343
Assert.assertEquals(cids.get("index.html"), tree.get(0).hash.toBase58());
4444
}
4545

46+
@Test
47+
public void testAddArgs() throws Exception {
48+
Path path = Paths.get("src/test/resources/html/index.html");
49+
NamedStreamable file = new FileWrapper(path.toFile());
50+
AddArgs args = AddArgs.Builder.newInstance()
51+
.setInline()
52+
.setCidVersion(1)
53+
.build();
54+
List<MerkleNode> tree = ipfs.add(file, args);
55+
56+
Assert.assertEquals(1, tree.size());
57+
Assert.assertEquals("index.html", tree.get(0).name.get());
58+
}
59+
4660
@Test
4761
public void testSingleWrapped() throws Exception {
4862

0 commit comments

Comments
 (0)