Skip to content

review of recent API changes #206

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 1 commit 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
44 changes: 36 additions & 8 deletions src/main/java/io/ipfs/api/IPFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ public String chcid(String path) throws IOException {
return retrieveString("files/chcid?args=" + arg);
}

public String chcid(String path, Optional<Integer> cidVersion, Optional<String> 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);
Expand Down Expand Up @@ -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<Integer> cidVersion, Optional<String> 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 {
Expand All @@ -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<String> 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;
Expand All @@ -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 {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/io/ipfs/api/WriteFilesArgs.java
Original file line number Diff line number Diff line change
@@ -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<String, String> args = new HashMap<>();

public WriteFilesArgs(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 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);
}
}
}
22 changes: 16 additions & 6 deletions src/test/java/io/ipfs/api/APITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("<cumulsize>"), true);
String readContents = new String(ipfs.files.read(path));
Assert.assertTrue("Should be equals", contents.equals(readContents));
res = ipfs.files.rm(path, false, false);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Map> cmds = ipfs.diag.cmds(true);
List<Map> cmds = ipfs.diag.cmds();
String res = ipfs.diag.clearCmds();
List<Map> cmds2 = ipfs.diag.cmds(true);
//res = ipfs.diag.profile();
Expand Down