Skip to content

add builder for arguments to ipfs.add #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/main/java/io/ipfs/api/AddArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package io.ipfs.api;

import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/*
Example usage:
AddArgs args = AddArgs.Builder.newInstance()
.setInline()
.setCidVersion(1)
.build();
*/
final class AddArgs {

private final Map<String, String> args = new HashMap<>();

public AddArgs(Builder builder)
{
args.putAll(builder.args);
}
@Override
public String toString()
{
List<String> 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<String, String> 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<String, String> args = new HashMap<>();
private Builder() {}
public static Builder newInstance()
{
return new Builder();
}
public Builder setQuiet() {
args.put("quiet", TRUE);
return this;
}
public Builder setQuieter() {
args.put("quieter", TRUE);
return this;
}
public Builder setSilent() {
args.put("silent", TRUE);
return this;
}
public Builder setTrickle() {
args.put("trickle", TRUE);
return this;
}
public Builder setOnlyHash() {
args.put("only-hash", TRUE);
return this;
}
public Builder setWrapWithDirectory() {
args.put("wrap-with-directory", TRUE);
return this;
}
public Builder setChunker(String chunker) {
args.put("chunker", chunker);
return this;
}
public Builder setRawLeaves() {
args.put("raw-leaves", TRUE);
return this;
}
public Builder setNocopy() {
args.put("nocopy", TRUE);
return this;
}
public Builder setFscache() {
args.put("fscache", 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 Builder setInline() {
args.put("inline", TRUE);
return this;
}
public Builder setInlineLimit(int maxBlockSize) {
args.put("inline-limit", String.valueOf(maxBlockSize));
return this;
}
public Builder setPin() {
args.put("pin", TRUE);
return this;
}
public Builder setToFiles(String path) {
args.put("to-files", path);
return this;
}
public AddArgs build()
{
return new AddArgs(this);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/ipfs/api/IPFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean h
.collect(Collectors.toList());
}

public List<MerkleNode> add(NamedStreamable file, AddArgs args) throws IOException {
return add(Collections.singletonList(file), args);
}

public List<MerkleNode> add(List<NamedStreamable> files, AddArgs args) throws IOException {
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion + "add?stream-channels=true&"+ args.toQueryString(), "UTF-8");
for (NamedStreamable file: files) {
if (file.isDirectory()) {
m.addSubtree(Paths.get(""), file);
} else
m.addFilePart("file", Paths.get(""), file);
};
String res = m.finish();
return JSONParser.parseStream(res).stream()
.map(x -> MerkleNode.fromJSON((Map<String, Object>) x))
.collect(Collectors.toList());
}

public List<MerkleNode> ls(Multihash hash) throws IOException {
Map reply = retrieveMap("ls?arg=" + hash);
return ((List<Object>) reply.get("Objects"))
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/io/ipfs/api/APITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,18 @@ public void testTimeoutOK() throws IOException {
ipfs.cat(Multihash.fromBase58("Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a"));
}

@Test
public void addArgsTest() {
AddArgs args = AddArgs.Builder.newInstance()
.setInline()
.setCidVersion(1)
.build();
String res = args.toString();
Assert.assertTrue("args toString() format", res.equals("[cid-version = 1, inline = true]"));
String queryStr = args.toQueryString();
Assert.assertTrue("args toQueryString() format", queryStr.equals("inline=true&cid-version=1"));
}

// this api is disabled until deployment over IPFS is enabled
public void updateTest() throws IOException {
Object check = ipfs.update.check();
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/ipfs/api/SimpleAddTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public void testSingle() throws Exception {
Assert.assertEquals(cids.get("index.html"), tree.get(0).hash.toBase58());
}

@Test
public void testAddArgs() throws Exception {
Path path = Paths.get("src/test/resources/html/index.html");
NamedStreamable file = new FileWrapper(path.toFile());
AddArgs args = AddArgs.Builder.newInstance()
.setInline()
.setCidVersion(1)
.build();
List<MerkleNode> tree = ipfs.add(file, args);

Assert.assertEquals(1, tree.size());
Assert.assertEquals("index.html", tree.get(0).name.get());
}

@Test
public void testSingleWrapped() throws Exception {

Expand Down