Skip to content

Commit

Permalink
Merge pull request #212 from ipfs-shipyard/feat/demos-2
Browse files Browse the repository at this point in the history
sample app to demo usage of remote Pinning API
  • Loading branch information
kevodwyer committed Feb 7, 2023
2 parents 05c314c + 5a5f68c commit 4c0cbb7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/ipfs/api/IPFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ public String addService(String service, String endPoint, String key) throws IOE
return retrieveString("pin/remote/service/add?arg=" + service + "&arg=" + endPoint + "&arg=" + key);
}

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

public String rmService(String service) throws IOException {
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/io/ipfs/api/demo/UsageRemotePinningAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.ipfs.api.demo;

import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multiaddr.MultiAddress;
import io.ipfs.multihash.Multihash;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/*
This sample program demonstrates how to use the remote pinning API methods
rpc api - https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-pin-remote-add
setup:
For demonstration purposes it uses a mock pinning service:
- https://github.com/ipfs-shipyard/js-mock-ipfs-pinning-service
Follow the instructions in the README.md file of the above repository for installation
Sample command to execute before running this program:
npx mock-ipfs-pinning-service --port 3000 --token secret
Note: The above parameters are referenced in the program below
*/
public class UsageRemotePinningAPI {

public UsageRemotePinningAPI(IPFS ipfsClient) {
try {
run(ipfsClient);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void run(IPFS ipfs) throws IOException {

// Add file to the local node
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("file.txt", "test data".getBytes())).get(0);
// Retrieve CID
Multihash hash = file.hash;

//Add the service
String serviceName = "mock";
ipfs.pin.remote.rmService(serviceName); //clean up if necessary
ipfs.pin.remote.addService(serviceName, "http://127.0.0.1:3000", "secret");

//List services
List<Map> services = ipfs.pin.remote.lsService(true);
for(Map service : services) {
System.out.println(service);
}

// Pin
Map addHashResult = ipfs.pin.remote.add(serviceName, hash, Optional.empty(), true);
System.out.println(addHashResult);

// List
List<IPFS.PinStatus> statusList = List.of(IPFS.PinStatus.values()); // all statuses
Map ls = ipfs.pin.remote.ls(serviceName, Optional.empty(), Optional.of(statusList));
System.out.println(ls);

// Remove pin from remote pinning service
List<IPFS.PinStatus> queued = List.of(IPFS.PinStatus.queued);
ipfs.pin.remote.rm(serviceName, Optional.empty(), Optional.of(queued), Optional.of(List.of(hash)));

}

public static void main(String[] args) {
IPFS ipfsClient = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
new UsageRemotePinningAPI(ipfsClient);
}
}
2 changes: 1 addition & 1 deletion src/test/java/io/ipfs/api/APITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void remotePinTest() throws IOException {
Multihash hash = file.hash;
String service = "mock";
String rmRemoteService = ipfs.pin.remote.rmService(service);
Map lsRemoteService = ipfs.pin.remote.lsService(false);
List<Map> lsRemoteService = ipfs.pin.remote.lsService(false);
String endpoint = "http://127.0.0.1:3000";
String key = "SET_VALUE_HERE";
String added = ipfs.pin.remote.addService(service, endpoint, key);
Expand Down

0 comments on commit 4c0cbb7

Please sign in to comment.