Skip to content

Commit 506671c

Browse files
committed
cleanup
1 parent b19fd58 commit 506671c

File tree

3 files changed

+35
-46
lines changed

3 files changed

+35
-46
lines changed

src/org/ipfs/IPFS.java

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public List<MerkleNode> add(List<NamedStreamable> files) throws IOException {
4343
}
4444

4545
public List<MerkleNode> ls(MerkleNode merkleObject) throws IOException {
46-
Map res = (Map) retrieveAndParse("ls/" + merkleObject.hash);
46+
Map res = retrieveMap("ls/" + merkleObject.hash);
4747
return ((List<Object>) res.get("Objects")).stream().map(x -> MerkleNode.fromJSON((Map) x)).collect(Collectors.toList());
4848
}
4949

@@ -56,28 +56,26 @@ public byte[] get(MerkleNode merkleObject) throws IOException {
5656
}
5757

5858
public Map refs(String hash, boolean recursive) throws IOException {
59-
Map res = (Map) retrieveAndParse("refs?arg=" + hash +"&r="+recursive);
60-
return res;
59+
return retrieveMap("refs?arg=" + hash +"&r="+recursive);
6160
}
6261

6362
public Map resolve(String scheme, String hash, boolean recursive) throws IOException {
64-
Map res = (Map) retrieveAndParse("resolve?arg=/" + scheme+"/"+hash +"&r="+recursive);
65-
return res;
63+
return retrieveMap("resolve?arg=/" + scheme+"/"+hash +"&r="+recursive);
6664
}
6765

6866

6967
public String dns(String domain) throws IOException {
70-
Map res = (Map) retrieveAndParse("dns?arg=" + domain);
68+
Map res = retrieveMap("dns?arg=" + domain);
7169
return (String)res.get("Path");
7270
}
7371

74-
7572
public Map mount(java.io.File ipfsRoot, java.io.File ipnsRoot) throws IOException {
7673
if (ipfsRoot != null && !ipfsRoot.exists())
7774
ipfsRoot.mkdirs();
7875
if (ipnsRoot != null && !ipnsRoot.exists())
7976
ipnsRoot.mkdirs();
80-
return (Map)retrieveAndParse("mount?" + (ipfsRoot != null ? ipfsRoot.getPath() : "./ipfs" ) + (ipnsRoot != null ? ipnsRoot.getPath() : "./ipns" ));
77+
return (Map)retrieveAndParse("mount?arg=" + (ipfsRoot != null ? ipfsRoot.getPath() : "/ipfs" ) + "&arg=" +
78+
(ipnsRoot != null ? ipnsRoot.getPath() : "/ipns" ));
8179
}
8280

8381
// level 2 commands
@@ -87,7 +85,7 @@ public List<String> local() throws IOException {
8785
}
8886
}
8987

90-
/* Pinning an object ensure a local copy of it is kept.
88+
/* Pinning an object ensures a local copy of it is kept.
9189
*/
9290
class Pin {
9391
public Object add(MerkleNode merkleObject) throws IOException {
@@ -99,7 +97,7 @@ public Object ls() throws IOException {
9997
}
10098

10199
public List<MerkleNode> rm(MerkleNode merkleObject, boolean recursive) throws IOException {
102-
Map json = (Map) retrieveAndParse("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + merkleObject.hash);
100+
Map json = retrieveMap("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + merkleObject.hash);
103101
return ((List<Object>) json.get("Pinned")).stream().map(x -> new MerkleNode((String) x)).collect(Collectors.toList());
104102
}
105103
}
@@ -127,8 +125,8 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
127125
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).collect(Collectors.toList());
128126
}
129127

130-
public Map<String, Object> stat(MerkleNode merkleObject) throws IOException {
131-
return (Map)retrieveAndParse("block/stat?stream-channels=true&arg=" + merkleObject.hash);
128+
public Map stat(MerkleNode merkleObject) throws IOException {
129+
return retrieveMap("block/stat?stream-channels=true&arg=" + merkleObject.hash);
132130
}
133131
}
134132

@@ -144,18 +142,18 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
144142
}
145143

146144
public MerkleNode get(MerkleNode merkleObject) throws IOException {
147-
Map json = (Map)retrieveAndParse("object/get?stream-channels=true&arg=" + merkleObject.hash);
145+
Map json = retrieveMap("object/get?stream-channels=true&arg=" + merkleObject.hash);
148146
json.put("Hash", merkleObject.hash.toBase58());
149147
return MerkleNode.fromJSON(json);
150148
}
151149

152150
public MerkleNode links(MerkleNode merkleObject) throws IOException {
153-
Map json = (Map)retrieveAndParse("object/links?stream-channels=true&arg=" + merkleObject.hash);
151+
Map json = retrieveMap("object/links?stream-channels=true&arg=" + merkleObject.hash);
154152
return MerkleNode.fromJSON(json);
155153
}
156154

157155
public Map<String, Object> stat(MerkleNode merkleObject) throws IOException {
158-
return (Map)retrieveAndParse("object/stat?stream-channels=true&arg=" + merkleObject.hash);
156+
return retrieveMap("object/stat?stream-channels=true&arg=" + merkleObject.hash);
159157
}
160158

161159
public byte[] data(MerkleNode merkleObject) throws IOException {
@@ -171,8 +169,7 @@ public Map publish(MerkleNode node) throws IOException {
171169
}
172170

173171
public Map publish(Optional<String> id, MerkleNode node) throws IOException {
174-
Map res = (Map) retrieveAndParse("name/publish?arg=" + (id.isPresent() ? id+"&arg=" : "") + "/ipfs/"+node.hash);
175-
return res;
172+
return retrieveMap("name/publish?arg=" + (id.isPresent() ? id+"&arg=" : "") + "/ipfs/"+node.hash);
176173
}
177174

178175
public String resolve(String addr) throws IOException {
@@ -183,41 +180,36 @@ public String resolve(String addr) throws IOException {
183180

184181
class DHT {
185182
public Map findprovs(MerkleNode node) throws IOException {
186-
Map res = (Map) retrieveAndParse("dht/findprovs?arg=" + node.hash);
187-
return res;
183+
return retrieveMap("dht/findprovs?arg=" + node.hash);
188184
}
189185

190186
public Map query(NodeAddress addr) throws IOException {
191-
Map res = (Map) retrieveAndParse("dht/query?arg=" + addr.address);
192-
return res;
187+
return retrieveMap("dht/query?arg=" + addr.address);
193188
}
194189

195190
public Map findpeer(NodeAddress addr) throws IOException {
196-
Map res = (Map) retrieveAndParse("dht/findpeer?arg=" + addr.address);
197-
return res;
191+
return retrieveMap("dht/findpeer?arg=" + addr.address);
198192
}
199193

200194
public Map get(MerkleNode node) throws IOException {
201-
Map res = (Map) retrieveAndParse("dht/get?arg=" + node.hash);
202-
return res;
195+
return retrieveMap("dht/get?arg=" + node.hash);
203196
}
204197

205198
public Map put(String key, String value) throws IOException {
206-
Map res = (Map) retrieveAndParse("dht/put?arg=" + key + "&arg="+value);
207-
return res;
199+
return retrieveMap("dht/put?arg=" + key + "&arg="+value);
208200
}
209201
}
210202

211203
class File {
212204
public Map ls(String path) throws IOException {
213-
return (Map)retrieveAndParse("file/ls?arg=" +path);
205+
return retrieveMap("file/ls?arg=" + path);
214206
}
215207
}
216208

217209
// Network commands
218210

219211
public Map bootstrap() throws IOException {
220-
return (Map)retrieveAndParse("bootstrap/");
212+
return retrieveMap("bootstrap/");
221213
}
222214

223215
/* ipfs swarm is a tool to manipulate the network swarm. The swarm is the
@@ -226,12 +218,12 @@ public Map bootstrap() throws IOException {
226218
*/
227219
class Swarm {
228220
public List<NodeAddress> peers() throws IOException {
229-
Map m = (Map)retrieveAndParse("swarm/peers?stream-channels=true");
221+
Map m = retrieveMap("swarm/peers?stream-channels=true");
230222
return ((List<Object>)m.get("Strings")).stream().map(x -> new NodeAddress((String)x)).collect(Collectors.toList());
231223
}
232224

233-
public Map<String, Object> addrs() throws IOException {
234-
Map m = (Map)retrieveAndParse("swarm/addrs?stream-channels=true");
225+
public Map addrs() throws IOException {
226+
Map m = retrieveMap("swarm/addrs?stream-channels=true");
235227
return (Map<String, Object>)m.get("Addrs");
236228
}
237229

@@ -245,16 +237,16 @@ public String net() throws IOException {
245237
}
246238

247239
public Map ping(String target) throws IOException {
248-
return (Map)retrieveAndParse("ping/"+target.toString());
240+
return retrieveMap("ping/" + target.toString());
249241
}
250242

251243
public Map id(String target) throws IOException {
252-
return (Map)retrieveAndParse("id/"+target.toString());
244+
return retrieveMap("id/" + target.toString());
253245
}
254246

255247
class Stats {
256248
public Map bw() throws IOException {
257-
return (Map)retrieveAndParse("stats/bw");
249+
return retrieveMap("stats/bw");
258250
}
259251
}
260252

@@ -265,11 +257,11 @@ public String version() throws IOException {
265257
}
266258

267259
public Map commands() throws IOException {
268-
return (Map)retrieveAndParse("commands");
260+
return retrieveMap("commands");
269261
}
270262

271263
public Map log() throws IOException {
272-
return (Map)retrieveAndParse("log/tail");
264+
return retrieveMap("log/tail");
273265
}
274266

275267
class Config {
@@ -289,7 +281,7 @@ public String get(String key) throws IOException {
289281
}
290282

291283
public Map set(String key, String value) throws IOException {
292-
return (Map)retrieveAndParse("config?arg="+key+"&arg="+value);
284+
return retrieveMap("config?arg=" + key + "&arg=" + value);
293285
}
294286
}
295287

@@ -307,6 +299,10 @@ public Object log() throws IOException {
307299
}
308300
}
309301

302+
private Map retrieveMap(String path) throws IOException {
303+
return (Map)retrieveAndParse(path);
304+
}
305+
310306
private Object retrieveAndParse(String path) throws IOException {
311307
byte[] res = retrieve(path);
312308
return JSONParser.parse(new String(res));
@@ -317,12 +313,6 @@ private byte[] retrieve(String path) throws IOException {
317313
return IPFS.get(target);
318314
}
319315

320-
private Object retrieveAndParsePost(String path, byte[] body) throws IOException {
321-
URL target = new URL("http", host, port, version + path);
322-
byte[] res = post(target, body, Collections.EMPTY_MAP);
323-
return JSONParser.parse(new String(res));
324-
}
325-
326316
private static byte[] get(URL target) throws IOException {
327317
HttpURLConnection conn = (HttpURLConnection) target.openConnection();
328318
conn.setRequestMethod("GET");

src/org/ipfs/MerkleNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.stream.*;
55

66
public class MerkleNode {
7-
public final Optional<String> name;
87
public final Multihash hash;
8+
public final Optional<String> name;
99
public final Optional<Integer> size;
1010
public final Optional<Integer> type;
1111
public final List<MerkleNode> links;

test/org/ipfs/Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public void dnsTest() {
129129
}
130130
}
131131

132-
@org.junit.Test
133132
public void mountTest() {
134133
try {
135134
Map mount = ipfs.mount(null, null);

0 commit comments

Comments
 (0)