Skip to content

Commit 73c7193

Browse files
author
Gregoire Jeanmart
committed
Support for SSL API endpoint such as Infura (https://ipfs.infura.io/)
1 parent 2d07776 commit 73c7193

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public enum PinType {all, direct, indirect, recursive}
2121

2222
public final String host;
2323
public final int port;
24+
public final String protocol;
2425
private final String version;
2526
public final Key key = new Key();
2627
public final Pin pin = new Pin();
@@ -39,22 +40,42 @@ public enum PinType {all, direct, indirect, recursive}
3940
public final Stats stats = new Stats();
4041
public final Name name = new Name();
4142
public final Pubsub pubsub = new Pubsub();
42-
43+
44+
4345
public IPFS(String host, int port) {
4446
this(host, port, "/api/v0/");
4547
}
4648

49+
public IPFS(String host, int port, boolean ssl) {
50+
this(host, port, "/api/v0/", ssl);
51+
}
52+
4753
public IPFS(String multiaddr) {
4854
this(new MultiAddress(multiaddr));
4955
}
5056

5157
public IPFS(MultiAddress addr) {
5258
this(addr.getHost(), addr.getTCPPort(), "/api/v0/");
5359
}
60+
61+
public IPFS(MultiAddress addr, boolean ssl) {
62+
this(addr.getHost(), addr.getTCPPort(), "/api/v0/", ssl);
63+
}
5464

5565
public IPFS(String host, int port, String version) {
66+
this(host, port, version, false);
67+
}
68+
69+
public IPFS(String host, int port, String version, boolean ssl) {
5670
this.host = host;
5771
this.port = port;
72+
73+
if(ssl) {
74+
this.protocol = "https";
75+
} else {
76+
this.protocol = "http";
77+
}
78+
5879
this.version = version;
5980
// Check IPFS is sufficiently recent
6081
try {
@@ -65,7 +86,7 @@ public IPFS(String host, int port, String version) {
6586
throw new RuntimeException(e);
6687
}
6788
}
68-
89+
6990
public List<MerkleNode> add(NamedStreamable file) throws IOException {
7091
return add(file, false);
7192
}
@@ -79,7 +100,7 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
79100
}
80101

81102
public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean hashOnly) throws IOException {
82-
Multipart m = new Multipart("http://" + host + ":" + port + version + "add?w="+wrap + "&n="+hashOnly, "UTF-8");
103+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?w="+wrap + "&n="+hashOnly, "UTF-8");
83104
for (NamedStreamable file: files) {
84105
if (file.isDirectory()) {
85106
m.addSubtree(Paths.get(""), file);
@@ -286,7 +307,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
286307

287308
public MerkleNode put(byte[] data, Optional<String> format) {
288309
String fmt = format.map(f -> "&format=" + f).orElse("");
289-
Multipart m = new Multipart("http://" + host + ":" + port + version+"block/put?stream-channels=true" + fmt, "UTF-8");
310+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"block/put?stream-channels=true" + fmt, "UTF-8");
290311
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data));
291312
String res = m.finish();
292313
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).findFirst().get();
@@ -301,7 +322,7 @@ public Map stat(Multihash hash) throws IOException {
301322
*/
302323
public class IPFSObject {
303324
public List<MerkleNode> put(List<byte[]> data) throws IOException {
304-
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/put?stream-channels=true", "UTF-8");
325+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/put?stream-channels=true", "UTF-8");
305326
for (byte[] f : data)
306327
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
307328
String res = m.finish();
@@ -311,7 +332,7 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
311332
public List<MerkleNode> put(String encoding, List<byte[]> data) throws IOException {
312333
if (!"json".equals(encoding) && !"protobuf".equals(encoding))
313334
throw new IllegalArgumentException("Encoding must be json or protobuf");
314-
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
335+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
315336
for (byte[] f : data)
316337
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
317338
String res = m.finish();
@@ -365,7 +386,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
365386
case "append-data":
366387
if (!data.isPresent())
367388
throw new IllegalStateException("set-data requires data!");
368-
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
389+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
369390
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.get()));
370391
String res = m.finish();
371392
return MerkleNode.fromJSON(JSONParser.parse(res));
@@ -488,7 +509,7 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
488509

489510
public MerkleNode put(String inputFormat, byte[] object, String outputFormat) throws IOException {
490511
block.put(Arrays.asList(object));
491-
String prefix = "http://" + host + ":" + port + version;
512+
String prefix = protocol +"://" + host + ":" + port + version;
492513
Multipart m = new Multipart(prefix + "block/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8");
493514
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(object));
494515
String res = m.finish();
@@ -544,7 +565,7 @@ public Map show() throws IOException {
544565
}
545566

546567
public void replace(NamedStreamable file) throws IOException {
547-
Multipart m = new Multipart("http://" + host + ":" + port + version+"config/replace?stream-channels=true", "UTF-8");
568+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"config/replace?stream-channels=true", "UTF-8");
548569
m.addFilePart("file", Paths.get(""), file);
549570
String res = m.finish();
550571
}
@@ -605,7 +626,7 @@ private void retrieveAndParseStream(String path, Consumer<Object> results) throw
605626
}
606627

607628
private byte[] retrieve(String path) throws IOException {
608-
URL target = new URL("http", host, port, version + path);
629+
URL target = new URL(protocol, host, port, version + path);
609630
return IPFS.get(target);
610631
}
611632

@@ -666,7 +687,7 @@ private static InputStream getStream(URL target) throws IOException {
666687
}
667688

668689
private Map postMap(String path, byte[] body, Map<String, String> headers) throws IOException {
669-
URL target = new URL("http", host, port, version + path);
690+
URL target = new URL(protocol, host, port, version + path);
670691
return (Map) JSONParser.parse(new String(post(target, body, headers)));
671692
}
672693

0 commit comments

Comments
 (0)