Skip to content

Commit e55846f

Browse files
committed
Update object.patch to IPFS 0.4.0
1 parent a2ddb51 commit e55846f

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,29 @@ public MerkleNode _new(Optional<String> template) throws IOException {
209209
public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, Optional<String> name, Optional<Multihash> target) throws IOException {
210210
if (!ObjectPatchTypes.contains(command))
211211
throw new IllegalStateException("Illegal Object.patch command type: "+command);
212-
String targetPath = "object/patch?arg=" + base.toBase58() + "&arg=" + command;
212+
String targetPath = "object/patch/"+command+"?arg=" + base.toBase58();
213213
if (name.isPresent())
214214
targetPath += "&arg=" + name.get();
215215
if (target.isPresent())
216216
targetPath += "&arg=" + target.get().toBase58();
217217

218218
switch (command) {
219219
case "add-link":
220-
if (!name.isPresent() || !target.isPresent())
220+
if (!target.isPresent())
221221
throw new IllegalStateException("add-link requires name and target!");
222-
return MerkleNode.fromJSON(retrieveMap(targetPath));
223222
case "rm-link":
224223
if (!name.isPresent())
225-
throw new IllegalStateException("rm-link requires name!");
224+
throw new IllegalStateException("link name is required!");
226225
return MerkleNode.fromJSON(retrieveMap(targetPath));
227226
case "set-data":
228-
if (!data.isPresent())
229-
throw new IllegalStateException("set-data requires data!");
230-
return MerkleNode.fromJSON(postMap(targetPath, data.get(), Collections.EMPTY_MAP));
231227
case "append-data":
232228
if (!data.isPresent())
233-
throw new IllegalStateException("append-data requires data!");
234-
return MerkleNode.fromJSON(postMap(targetPath, data.get(), Collections.EMPTY_MAP));
229+
throw new IllegalStateException("set-data requires data!");
230+
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
231+
m.addFilePart("file", new NamedStreamable.ByteArrayWrapper(data.get()));
232+
String res = m.finish();
233+
return MerkleNode.fromJSON(JSONParser.parse(res));
234+
235235
default:
236236
throw new IllegalStateException("Unimplemented");
237237
}
@@ -419,14 +419,18 @@ private static byte[] get(URL target) throws IOException {
419419
conn.setRequestMethod("GET");
420420
conn.setRequestProperty("Content-Type", "application/json");
421421

422-
InputStream in = conn.getInputStream();
423-
ByteArrayOutputStream resp = new ByteArrayOutputStream();
422+
try {
423+
InputStream in = conn.getInputStream();
424+
ByteArrayOutputStream resp = new ByteArrayOutputStream();
424425

425-
byte[] buf = new byte[4096];
426-
int r;
427-
while ((r=in.read(buf)) >= 0)
428-
resp.write(buf, 0, r);
429-
return resp.toByteArray();
426+
byte[] buf = new byte[4096];
427+
int r;
428+
while ((r = in.read(buf)) >= 0)
429+
resp.write(buf, 0, r);
430+
return resp.toByteArray();
431+
} catch (IOException e) {
432+
throw new RuntimeException("Trailer: " + conn.getHeaderFields().get("Trailer"), e);
433+
}
430434
}
431435

432436
private Map postMap(String path, byte[] body, Map<String, String> headers) throws IOException {

src/main/java/org/ipfs/api/Multipart.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,16 @@ public String finish() throws IOException {
9494
reader.close();
9595
httpConn.disconnect();
9696
} else {
97-
throw new IOException("Server returned status: " + status);
97+
try {
98+
BufferedReader reader = new BufferedReader(new InputStreamReader(
99+
httpConn.getInputStream()));
100+
String line;
101+
while ((line = reader.readLine()) != null) {
102+
b.append(line);
103+
}
104+
reader.close();
105+
} catch (Throwable t) {}
106+
throw new IOException("Server returned status: " + status + " with body: "+b.toString() + " and Trailer header: "+httpConn.getHeaderFields().get("Trailer"));
98107
}
99108

100109
return b.toString();

src/test/java/org/ipfs/api/Test.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ public void swarmTest() {
242242
Map disconnect = ipfs.swarm.disconnect(multiaddr);
243243
Map<String, Object> addrs = ipfs.swarm.addrs();
244244
if (addrs.size() > 0) {
245-
Map id = ipfs.id(addrs.keySet().stream().findAny().get());
246-
Map ping = ipfs.ping(addrs.keySet().stream().findAny().get());
245+
String target = addrs.keySet().stream().findAny().get();
246+
Map id = ipfs.id(target);
247+
Map ping = ipfs.ping(target);
247248
}
248249
List<MultiAddress> peers = ipfs.swarm.peers();
249250
System.out.println(peers);

0 commit comments

Comments
 (0)