Added data structures for SnapshotGetForVersionRequest.

This commit is contained in:
Winston Li
2014-11-07 12:08:28 +00:00
parent 14feaa9fcb
commit 0f4e99361f
7 changed files with 180 additions and 7 deletions

View File

@@ -2,6 +2,8 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
import com.google.gson.JsonElement;
import java.io.IOException;
/**
* Created by Winston on 06/11/14.
*/

View File

@@ -0,0 +1,47 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotData implements JSONSource {
public static final String JSON_KEY_SRCS = "srcs";
public static final String JSON_KEY_ATTS = "atts";
private List<WLFile> srcs;
private List<WLFile> atts;
public SnapshotData(JsonElement json) {
srcs = new LinkedList<WLFile>();
atts = new LinkedList<WLFile>();
fromJSON(json);
}
@Override
public void fromJSON(JsonElement json) {
populateSrcs(json.getAsJsonObject().get(JSON_KEY_SRCS).getAsJsonArray());
populateAtts(json.getAsJsonObject().get(JSON_KEY_ATTS).getAsJsonArray());
}
private void populateSrcs(JsonArray jsonArray) {
for (JsonElement json : jsonArray) {
srcs.add(new WLFile(json));
}
System.out.println(srcs);
}
private void populateAtts(JsonArray jsonArray) {
for (JsonElement json : jsonArray) {
atts.add(new WLAttachment(json));
}
}
}

View File

@@ -1,21 +1,28 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetForVersionResult extends Result {
private SnapshotData snapshotData;
public SnapshotGetForVersionResult(Request request, JsonElement json) {
super(request, json);
}
@Override
public void fromJSON(JsonElement json) {
snapshotData = new SnapshotData(json);
}
}

View File

@@ -0,0 +1,57 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.ning.http.client.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Created by Winston on 06/11/14.
*/
public class WLAttachment extends WLFile {
private Future<byte[]> future;
public WLAttachment(JsonElement json) {
super(json);
}
@Override
public byte[] getContents() throws ExecutionException, InterruptedException {
return future.get();
}
@Override
protected void getContentsFromJSON(JsonArray jsonArray) {
try {
fetchContents(jsonArray.get(0).getAsString());
} catch (IOException e) {
throw new RuntimeException();
}
}
private void fetchContents(String url) throws IOException {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
future = asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
bytes.write(bodyPart.getBodyPartBytes());
return STATE.CONTINUE;
}
@Override
public byte[] onCompleted(Response response) throws Exception {
return bytes.toByteArray();
}
});
}
}

View File

@@ -0,0 +1,46 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/**
* Created by Winston on 06/11/14.
*/
public class WLFile implements JSONSource {
protected byte[] contents;
private String path;
public WLFile(JsonElement json) {
fromJSON(json);
}
@Override
public void fromJSON(JsonElement json) {
JsonArray jsonArray = json.getAsJsonArray();
getContentsFromJSON(jsonArray);
getPathFromJSON(jsonArray);
}
public byte[] getContents() throws ExecutionException, InterruptedException {
return contents;
}
public String getPath() {
return path;
}
protected void getContentsFromJSON(JsonArray jsonArray) {
contents = jsonArray.get(0).getAsString().getBytes();
}
protected void getPathFromJSON(JsonArray jsonArray) {
path = jsonArray.get(1).getAsString();
}
}

View File

@@ -6,16 +6,27 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotInfo implements JSONSource {
public class SnapshotInfo {
private int versionId;
private String comment;
private WLUser user;
private String createdAt;
@Override
public void fromJSON(JsonElement json) {
public int getVersionId() {
return versionId;
}
public String getComment() {
return comment;
}
public WLUser getUser() {
return user;
}
public String getCreatedAt() {
return createdAt;
}
}

View File

@@ -6,14 +6,17 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
/**
* Created by Winston on 06/11/14.
*/
public class WLUser implements JSONSource {
public class WLUser {
private String name;
private String email;
@Override
public void fromJSON(JsonElement json) {
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}