Skip to content

Commit 4c0cbb7

Browse files
authored
Merge pull request #212 from ipfs-shipyard/feat/demos-2
sample app to demo usage of remote Pinning API
2 parents 05c314c + 5a5f68c commit 4c0cbb7

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ public String addService(String service, String endPoint, String key) throws IOE
244244
return retrieveString("pin/remote/service/add?arg=" + service + "&arg=" + endPoint + "&arg=" + key);
245245
}
246246

247-
public Map lsService(boolean stat) throws IOException {
248-
return retrieveMap("pin/remote/service/ls?stat=" + stat);
247+
public List<Map> lsService(boolean stat) throws IOException {
248+
return (List<Map>) retrieveMap("pin/remote/service/ls?stat=" + stat).get("RemoteServices");
249249
}
250250

251251
public String rmService(String service) throws IOException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.ipfs.api.demo;
2+
3+
import io.ipfs.api.IPFS;
4+
import io.ipfs.api.MerkleNode;
5+
import io.ipfs.api.NamedStreamable;
6+
import io.ipfs.multiaddr.MultiAddress;
7+
import io.ipfs.multihash.Multihash;
8+
9+
import java.io.IOException;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Optional;
13+
14+
/*
15+
This sample program demonstrates how to use the remote pinning API methods
16+
17+
rpc api - https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-pin-remote-add
18+
19+
setup:
20+
For demonstration purposes it uses a mock pinning service:
21+
- https://github.com/ipfs-shipyard/js-mock-ipfs-pinning-service
22+
Follow the instructions in the README.md file of the above repository for installation
23+
24+
Sample command to execute before running this program:
25+
npx mock-ipfs-pinning-service --port 3000 --token secret
26+
27+
Note: The above parameters are referenced in the program below
28+
*/
29+
public class UsageRemotePinningAPI {
30+
31+
public UsageRemotePinningAPI(IPFS ipfsClient) {
32+
try {
33+
run(ipfsClient);
34+
} catch (IOException ioe) {
35+
ioe.printStackTrace();
36+
}
37+
}
38+
private void run(IPFS ipfs) throws IOException {
39+
40+
// Add file to the local node
41+
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("file.txt", "test data".getBytes())).get(0);
42+
// Retrieve CID
43+
Multihash hash = file.hash;
44+
45+
//Add the service
46+
String serviceName = "mock";
47+
ipfs.pin.remote.rmService(serviceName); //clean up if necessary
48+
ipfs.pin.remote.addService(serviceName, "http://127.0.0.1:3000", "secret");
49+
50+
//List services
51+
List<Map> services = ipfs.pin.remote.lsService(true);
52+
for(Map service : services) {
53+
System.out.println(service);
54+
}
55+
56+
// Pin
57+
Map addHashResult = ipfs.pin.remote.add(serviceName, hash, Optional.empty(), true);
58+
System.out.println(addHashResult);
59+
60+
// List
61+
List<IPFS.PinStatus> statusList = List.of(IPFS.PinStatus.values()); // all statuses
62+
Map ls = ipfs.pin.remote.ls(serviceName, Optional.empty(), Optional.of(statusList));
63+
System.out.println(ls);
64+
65+
// Remove pin from remote pinning service
66+
List<IPFS.PinStatus> queued = List.of(IPFS.PinStatus.queued);
67+
ipfs.pin.remote.rm(serviceName, Optional.empty(), Optional.of(queued), Optional.of(List.of(hash)));
68+
69+
}
70+
71+
public static void main(String[] args) {
72+
IPFS ipfsClient = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
73+
new UsageRemotePinningAPI(ipfsClient);
74+
}
75+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public void remotePinTest() throws IOException {
330330
Multihash hash = file.hash;
331331
String service = "mock";
332332
String rmRemoteService = ipfs.pin.remote.rmService(service);
333-
Map lsRemoteService = ipfs.pin.remote.lsService(false);
333+
List<Map> lsRemoteService = ipfs.pin.remote.lsService(false);
334334
String endpoint = "http://127.0.0.1:3000";
335335
String key = "SET_VALUE_HERE";
336336
String added = ipfs.pin.remote.addService(service, endpoint, key);

0 commit comments

Comments
 (0)