mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Big refactoring, implemented SnapshotGetSavedVersRequest.
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request;
|
||||
|
||||
import com.ning.http.client.AsyncHttpClient;
|
||||
import com.ning.http.client.Realm;
|
||||
import com.ning.http.client.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public abstract class Request {
|
||||
|
||||
private final String url;
|
||||
|
||||
private Future<Response> response;
|
||||
private IOException exception;
|
||||
|
||||
public Request(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
protected abstract Realm buildRequestRealm();
|
||||
|
||||
public void request() {
|
||||
AsyncHttpClient client = new AsyncHttpClient();
|
||||
try {
|
||||
response = client.prepareGet(url).setRealm(buildRequestRealm()).execute();
|
||||
} catch (IOException e) {
|
||||
exception = e;
|
||||
}
|
||||
}
|
||||
|
||||
public String getResponse() throws IOException, ExecutionException, InterruptedException {
|
||||
if (exception != null) {
|
||||
throw exception;
|
||||
}
|
||||
return response.get().getResponseBody();
|
||||
}
|
||||
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetDocRequest extends SnapshotAPIRequest {
|
||||
|
||||
public static final String API_CALL = "";
|
||||
|
||||
public SnapshotGetDocRequest(String projectName) {
|
||||
super(projectName, API_CALL);
|
||||
}
|
||||
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetForVersionRequest extends SnapshotAPIRequest {
|
||||
|
||||
public static final String API_CALL = "/snapshots";
|
||||
|
||||
public SnapshotGetForVersionRequest(String projectName, int versionID) {
|
||||
super(projectName, API_CALL + "/" + versionID);
|
||||
}
|
||||
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest {
|
||||
|
||||
public static final String API_CALL = "/saved_vers";
|
||||
|
||||
public SnapshotGetSavedVersRequest(String projectName) {
|
||||
super(projectName, API_CALL);
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public interface JSONSource {
|
||||
|
||||
public abstract void fromJSON(JsonElement json);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.ning.http.client.AsyncCompletionHandler;
|
||||
import com.ning.http.client.AsyncHttpClient;
|
||||
import com.ning.http.client.Realm;
|
||||
import com.ning.http.client.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public abstract class Request<T extends Result> {
|
||||
|
||||
private final String url;
|
||||
|
||||
private Future<T> future;
|
||||
private Throwable throwable;
|
||||
|
||||
private boolean finished;
|
||||
|
||||
public Request(String url) {
|
||||
this.url = url;
|
||||
finished = false;
|
||||
}
|
||||
|
||||
protected abstract Realm buildRequestRealm();
|
||||
protected abstract T parseResponse(JsonElement json);
|
||||
|
||||
public void request() {
|
||||
AsyncHttpClient client = new AsyncHttpClient();
|
||||
try {
|
||||
future = client.prepareGet(url).setRealm(buildRequestRealm()).execute(new AsyncCompletionHandler<T>() {
|
||||
|
||||
@Override
|
||||
public T onCompleted(Response response) throws Exception {
|
||||
return parseResponse(new Gson().fromJson(response.getResponseBody(), JsonElement.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onThrowable(Throwable t) {
|
||||
throwable = t;
|
||||
}
|
||||
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throwable = e;
|
||||
}
|
||||
}
|
||||
|
||||
public T getResult() throws Throwable {
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
}
|
||||
return future.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public abstract class Result implements JSONSource {
|
||||
|
||||
private JsonElement json;
|
||||
private final Request request;
|
||||
|
||||
public Result(Request request, JsonElement json) {
|
||||
this.request = request;
|
||||
this.json = json;
|
||||
fromJSON(json);
|
||||
}
|
||||
|
||||
public Request getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request;
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
|
||||
|
||||
import com.ning.http.client.Realm;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public abstract class SnapshotAPIRequest extends Request {
|
||||
public abstract class SnapshotAPIRequest<T extends Result> extends Request<T> {
|
||||
|
||||
private static final String USERNAME = "staging";
|
||||
private static final String PASSWORD = "6kUfbv0R";
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetDocRequest extends SnapshotAPIRequest<SnapshotGetDocResult> {
|
||||
|
||||
public static final String API_CALL = "";
|
||||
|
||||
public SnapshotGetDocRequest(String projectName) {
|
||||
super(projectName, API_CALL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SnapshotGetDocResult parseResponse(JsonElement json) {
|
||||
return new SnapshotGetDocResult(this, json);
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetDocResult extends Result {
|
||||
|
||||
private int versionID;
|
||||
|
||||
public SnapshotGetDocResult(Request request, JsonElement json) {
|
||||
super(request, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
versionID = json.getAsJsonObject().get("latestVerId").getAsInt();
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGetForVersionResult> {
|
||||
|
||||
public static final String API_CALL = "/snapshots";
|
||||
|
||||
public SnapshotGetForVersionRequest(String projectName, int versionID) {
|
||||
super(projectName, API_CALL + "/" + versionID);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SnapshotGetForVersionResult parseResponse(JsonElement json) {
|
||||
return new SnapshotGetForVersionResult(this, json);
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetForVersionResult extends Result {
|
||||
|
||||
public SnapshotGetForVersionResult(Request request, JsonElement json) {
|
||||
super(request, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest<SnapshotGetSavedVersResult> {
|
||||
|
||||
public static final String API_CALL = "/saved_vers";
|
||||
|
||||
public SnapshotGetSavedVersRequest(String projectName) {
|
||||
super(projectName, API_CALL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SnapshotGetSavedVersResult parseResponse(JsonElement json) {
|
||||
return new SnapshotGetSavedVersResult(this, json);
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
|
||||
|
||||
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.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotGetSavedVersResult extends Result {
|
||||
|
||||
private List<SnapshotInfo> savedVers;
|
||||
|
||||
public SnapshotGetSavedVersResult(Request request, JsonElement json) {
|
||||
super(request, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
savedVers = new Gson().fromJson(json, LinkedList.class);
|
||||
}
|
||||
|
||||
public List<SnapshotInfo> getSavedVers() {
|
||||
return savedVers;
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class SnapshotInfo implements JSONSource {
|
||||
|
||||
private int versionId;
|
||||
private String comment;
|
||||
private WLUser user;
|
||||
private String createdAt;
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public class WLUser implements JSONSource {
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex.model;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.Request;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.SnapshotGetDocRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.SnapshotGetForVersionRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.SnapshotGetSavedVersRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
@@ -51,9 +48,9 @@ public class WLProject implements JSONModel {
|
||||
|
||||
boolean result = false;
|
||||
|
||||
ids.add(getLatestVersionID(getDoc.getResponse()));
|
||||
// ids.add(getLatestVersionID(getDoc.getResult()));
|
||||
|
||||
ids.addAll(getLatestVersionIDs(getSavedVers.getResponse()));
|
||||
// ids.addAll(getLatestVersionIDs(getSavedVers.getResult()));
|
||||
|
||||
List<Integer> idsToUpdate = new LinkedList<Integer>();
|
||||
|
||||
@@ -81,24 +78,4 @@ public class WLProject implements JSONModel {
|
||||
|
||||
}
|
||||
|
||||
private int getLatestVersionID(String response) {
|
||||
Gson gson = new Gson();
|
||||
JsonObject responseObject = gson.fromJson(response, JsonObject.class);
|
||||
return responseObject.get("latestVerId").getAsInt();
|
||||
}
|
||||
|
||||
private Collection<? extends Integer> getLatestVersionIDs(String response) {
|
||||
List<Integer> ids = new LinkedList<Integer>();
|
||||
Gson gson = new Gson();
|
||||
JsonArray responseArray = gson.fromJson(response, JsonArray.class);
|
||||
for (JsonElement elem : responseArray) {
|
||||
ids.add(getVersionID(elem.getAsJsonObject()));
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
private Integer getVersionID(JsonObject object) {
|
||||
return object.get("versionId").getAsInt();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user