Skip to content

Commit 92d0ec0

Browse files
committed
add remote pin api functions
1 parent 7d220ac commit 92d0ec0

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class IPFS {
1717

1818
public static final Version MIN_VERSION = Version.parse("0.4.11");
1919
public enum PinType {all, direct, indirect, recursive}
20+
public enum PinStatus {queued, pinning, pinned, failed}
2021
public List<String> ObjectTemplates = Arrays.asList("unixfs-dir");
2122
public List<String> ObjectPatchTypes = Arrays.asList("add-link", "rm-link", "set-data", "append-data");
2223
private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 10_000;
@@ -206,7 +207,10 @@ public List<Multihash> add(Multihash hash) throws IOException {
206207
.map(x -> Cid.decode((String) x))
207208
.collect(Collectors.toList());
208209
}
209-
210+
public Map addRemote(String service, Multihash hash, Optional<String> name, boolean background) throws IOException {
211+
String nameArg = name.isPresent() ? "&name=" + name.get() : "";
212+
return retrieveMap("pin/remote/add?arg=" + hash + "&service=" + service + nameArg + "&background=" + background);
213+
}
210214
public Map<Multihash, Object> ls() throws IOException {
211215
return ls(PinType.direct);
212216
}
@@ -217,6 +221,13 @@ public Map<Multihash, Object> ls(PinType type) throws IOException {
217221
.collect(Collectors.toMap(x -> Cid.decode(x.getKey()), x-> x.getValue()));
218222
}
219223

224+
public Map lsRemote(String service, Optional<String> name, Optional<List<PinStatus>> statusList) throws IOException {
225+
String nameArg = name.isPresent() ? "&name=" + name.get() : "";
226+
String statusArg = statusList.isPresent() ? statusList.get().stream().
227+
map(p -> "&status=" + p).collect(Collectors.joining()) : "";
228+
return retrieveMap("pin/remote/ls?service=" + service + nameArg + statusArg);
229+
}
230+
220231
public List<Multihash> rm(Multihash hash) throws IOException {
221232
return rm(hash, true);
222233
}
@@ -226,12 +237,36 @@ public List<Multihash> rm(Multihash hash, boolean recursive) throws IOException
226237
return ((List<Object>) json.get("Pins")).stream().map(x -> Cid.decode((String) x)).collect(Collectors.toList());
227238
}
228239

240+
public String rmRemote(String service, Optional<String> name, Optional<List<PinStatus>> statusList, Optional<List<Multihash>> cidList) throws IOException {
241+
String nameArg = name.isPresent() ? "&name=" + name.get() : "";
242+
String statusArg = statusList.isPresent() ? statusList.get().stream().
243+
map(p -> "&status=" + p).collect(Collectors.joining()) : "";
244+
String cidArg = cidList.isPresent() ? cidList.get().stream().
245+
map(p -> "&cid=" + p.toBase58()).collect(Collectors.joining()) : "";
246+
return retrieveString("pin/remote/rm?service=" + service + nameArg + statusArg + cidArg);
247+
}
248+
249+
public String addRemoteService(String service, String endPoint, String key) throws IOException {
250+
return retrieveString("pin/remote/service/add?arg=" + service + "&arg=" + endPoint + "&arg=" + key);
251+
}
252+
253+
public Map lsRemoteService(boolean stat) throws IOException {
254+
return retrieveMap("pin/remote/service/ls?stat=" + stat);
255+
}
256+
257+
public String rmRemoteService(String service) throws IOException {
258+
return retrieveString("pin/remote/service/rm?arg=" + service);
259+
}
260+
229261
public List<Multihash> update(Multihash existing, Multihash modified, boolean unpin) throws IOException {
230262
return ((List<Object>)((Map)retrieveAndParse("pin/update?stream-channels=true&arg=" + existing + "&arg=" + modified + "&unpin=" + unpin)).get("Pins"))
231263
.stream()
232264
.map(x -> Cid.decode((String) x))
233265
.collect(Collectors.toList());
234266
}
267+
public Map verify(boolean verbose, boolean quite) throws IOException {
268+
return retrieveMap("pin/verify?verbose=" + verbose + "&quite=" + quite);
269+
}
235270
}
236271

237272
/* 'ipfs key' is a command for dealing with IPNS keys.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ public void pinTest() throws IOException {
314314
Assert.assertTrue("Pinning works", pinned && stillPinned);
315315
}
316316

317+
@Test
318+
@Ignore
319+
public void remotePinTest() throws IOException {
320+
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("test data".getBytes())).get(0);
321+
Multihash hash = file.hash;
322+
String service = "mock";
323+
String rmRemoteService = ipfs.pin.rmRemoteService(service);
324+
Map lsRemoteService = ipfs.pin.lsRemoteService(false);
325+
String endpoint = "http://127.0.0.1:3000";
326+
String key = "SET_VALUE_HERE";
327+
String added = ipfs.pin.addRemoteService(service, endpoint, key);
328+
lsRemoteService = ipfs.pin.lsRemoteService(false);
329+
Map addHash = ipfs.pin.addRemote(service, hash, Optional.empty(), true);
330+
Map lsRemote = ipfs.pin.lsRemote(service, Optional.empty(), Optional.of(List.of(IPFS.PinStatus.values())));
331+
String rmRemote = ipfs.pin.rmRemote(service, Optional.empty(), Optional.of(List.of(IPFS.PinStatus.queued)), Optional.of(List.of(hash)));
332+
lsRemote = ipfs.pin.lsRemote(service, Optional.empty(), Optional.of(List.of(IPFS.PinStatus.values())));
333+
}
334+
317335
@Test
318336
public void pinUpdate() throws IOException {
319337
MerkleNode child1 = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes())).get(0);

0 commit comments

Comments
 (0)