Support JSON based POST, PUT, DELETE in client
Change-Id: I54a1f8471331a4b111ee4da8faffe4f15a2a9d96
This commit is contained in:
@@ -21,11 +21,9 @@ import com.google.gwtjsonrpc.common.AsyncCallback;
|
||||
/** Capabilities the caller has from {@code /accounts/self/capabilities}. */
|
||||
public class AccountCapabilities extends JavaScriptObject {
|
||||
public static void all(AsyncCallback<AccountCapabilities> cb, String... filter) {
|
||||
RestApi api = new RestApi("/accounts/self/capabilities");
|
||||
for (String name : filter) {
|
||||
api.addParameter("q", name);
|
||||
}
|
||||
api.send(cb);
|
||||
new RestApi("/accounts/self/capabilities")
|
||||
.addParameter("q", filter)
|
||||
.get(cb);
|
||||
}
|
||||
|
||||
protected AccountCapabilities() {
|
||||
|
||||
@@ -35,7 +35,7 @@ public class ChangeList extends NativeList<ChangeInfo> {
|
||||
call.addParameterRaw("q", KeyUtil.encode(q));
|
||||
}
|
||||
addOptions(call, ListChangesOption.LABELS);
|
||||
call.send(callback);
|
||||
call.get(callback);
|
||||
}
|
||||
|
||||
public static void prev(String query,
|
||||
@@ -49,7 +49,7 @@ public class ChangeList extends NativeList<ChangeInfo> {
|
||||
if (!PagedSingleListScreen.MIN_SORTKEY.equals(sortkey)) {
|
||||
call.addParameter("P", sortkey);
|
||||
}
|
||||
call.send(callback);
|
||||
call.get(callback);
|
||||
}
|
||||
|
||||
public static void next(String query,
|
||||
@@ -63,7 +63,7 @@ public class ChangeList extends NativeList<ChangeInfo> {
|
||||
if (!PagedSingleListScreen.MAX_SORTKEY.equals(sortkey)) {
|
||||
call.addParameter("N", sortkey);
|
||||
}
|
||||
call.send(callback);
|
||||
call.get(callback);
|
||||
}
|
||||
|
||||
private static void addOptions(
|
||||
|
||||
@@ -25,14 +25,14 @@ public class DashboardMap extends NativeMap<DashboardInfo> {
|
||||
public static void allOnProject(Project.NameKey project,
|
||||
AsyncCallback<DashboardMap> callback) {
|
||||
new RestApi("/dashboards/project/" + URL.encode(project.get()).replaceAll("[?]", "%3F"))
|
||||
.send(NativeMap.copyKeysIntoChildren(callback));
|
||||
.get(NativeMap.copyKeysIntoChildren(callback));
|
||||
}
|
||||
|
||||
public static void projectDefault(Project.NameKey project,
|
||||
AsyncCallback<DashboardMap> callback) {
|
||||
new RestApi("/dashboards/project/" + URL.encode(project.get()).replaceAll("[?]", "%3F"))
|
||||
.addParameterTrue("default")
|
||||
.send(NativeMap.copyKeysIntoChildren(callback));
|
||||
.get(NativeMap.copyKeysIntoChildren(callback));
|
||||
}
|
||||
|
||||
protected DashboardMap() {
|
||||
|
||||
@@ -21,8 +21,9 @@ import com.google.gwtjsonrpc.common.AsyncCallback;
|
||||
/** Plugins available from {@code /plugins/}. */
|
||||
public class PluginMap extends NativeMap<PluginInfo> {
|
||||
public static void all(AsyncCallback<PluginMap> callback) {
|
||||
new RestApi("/plugins/").addParameterTrue("all")
|
||||
.send(NativeMap.copyKeysIntoChildren(callback));
|
||||
new RestApi("/plugins/")
|
||||
.addParameterTrue("all")
|
||||
.get(NativeMap.copyKeysIntoChildren(callback));
|
||||
}
|
||||
|
||||
protected PluginMap() {
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ProjectMap extends NativeMap<ProjectInfo> {
|
||||
.addParameterRaw("type", "ALL")
|
||||
.addParameterTrue("all")
|
||||
.addParameterTrue("d") // description
|
||||
.send(NativeMap.copyKeysIntoChildren(callback));
|
||||
.get(NativeMap.copyKeysIntoChildren(callback));
|
||||
}
|
||||
|
||||
public static void permissions(AsyncCallback<ProjectMap> callback) {
|
||||
@@ -34,7 +34,7 @@ public class ProjectMap extends NativeMap<ProjectInfo> {
|
||||
.addParameterRaw("type", "PERMISSIONS")
|
||||
.addParameterTrue("all")
|
||||
.addParameterTrue("d") // description
|
||||
.send(NativeMap.copyKeysIntoChildren(callback));
|
||||
.get(NativeMap.copyKeysIntoChildren(callback));
|
||||
}
|
||||
|
||||
public static void parentCandidates(AsyncCallback<ProjectMap> callback) {
|
||||
@@ -42,7 +42,7 @@ public class ProjectMap extends NativeMap<ProjectInfo> {
|
||||
.addParameterRaw("type", "PARENT_CANDIDATES")
|
||||
.addParameterTrue("all")
|
||||
.addParameterTrue("d") // description
|
||||
.send(NativeMap.copyKeysIntoChildren(callback));
|
||||
.get(NativeMap.copyKeysIntoChildren(callback));
|
||||
}
|
||||
|
||||
public static void suggest(String prefix, int limit, AsyncCallback<ProjectMap> cb) {
|
||||
@@ -50,7 +50,7 @@ public class ProjectMap extends NativeMap<ProjectInfo> {
|
||||
.addParameterRaw("type", "ALL")
|
||||
.addParameter("n", limit)
|
||||
.addParameterTrue("d") // description
|
||||
.send(NativeMap.copyKeysIntoChildren(cb));
|
||||
.get(NativeMap.copyKeysIntoChildren(cb));
|
||||
}
|
||||
|
||||
protected ProjectMap() {
|
||||
|
||||
@@ -14,16 +14,23 @@
|
||||
|
||||
package com.google.gerrit.client.rpc;
|
||||
|
||||
import static com.google.gwt.http.client.RequestBuilder.DELETE;
|
||||
import static com.google.gwt.http.client.RequestBuilder.GET;
|
||||
import static com.google.gwt.http.client.RequestBuilder.POST;
|
||||
import static com.google.gwt.http.client.RequestBuilder.PUT;
|
||||
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.RpcStatus;
|
||||
import com.google.gwt.core.client.GWT;
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.http.client.Request;
|
||||
import com.google.gwt.http.client.RequestBuilder;
|
||||
import com.google.gwt.http.client.RequestBuilder.Method;
|
||||
import com.google.gwt.http.client.RequestCallback;
|
||||
import com.google.gwt.http.client.RequestException;
|
||||
import com.google.gwt.http.client.Response;
|
||||
import com.google.gwt.http.client.URL;
|
||||
import com.google.gwt.json.client.JSONObject;
|
||||
import com.google.gwt.user.client.rpc.StatusCodeException;
|
||||
import com.google.gwtjsonrpc.client.RemoteJsonException;
|
||||
import com.google.gwtjsonrpc.client.ServerUnavailableException;
|
||||
@@ -105,6 +112,8 @@ public class RestApi {
|
||||
|
||||
private StringBuilder url;
|
||||
private boolean hasQueryParams;
|
||||
private String contentType;
|
||||
private String contentData;
|
||||
|
||||
/**
|
||||
* Initialize a new API call.
|
||||
@@ -129,6 +138,13 @@ public class RestApi {
|
||||
return addParameterRaw(name, URL.encodeQueryString(value));
|
||||
}
|
||||
|
||||
public RestApi addParameter(String name, String... value) {
|
||||
for (String val : value) {
|
||||
addParameter(name, val);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public RestApi addParameterTrue(String name) {
|
||||
return addParameterRaw(name, null);
|
||||
}
|
||||
@@ -159,16 +175,53 @@ public class RestApi {
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T extends JavaScriptObject> void send(final AsyncCallback<T> cb) {
|
||||
RequestBuilder req = new RequestBuilder(RequestBuilder.GET, url.toString());
|
||||
public RestApi data(JavaScriptObject obj) {
|
||||
return data(new JSONObject(obj));
|
||||
}
|
||||
|
||||
public RestApi data(JSONObject obj) {
|
||||
contentType = JsonConstants.JSON_REQ_CT;
|
||||
contentData = obj.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
public RestApi data(String data) {
|
||||
contentType = "text/plain; charset=utf-8";
|
||||
contentData = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public <T extends JavaScriptObject> void get(AsyncCallback<T> cb) {
|
||||
send(GET, cb);
|
||||
}
|
||||
|
||||
public <T extends JavaScriptObject> void put(AsyncCallback<T> cb) {
|
||||
send(PUT, cb);
|
||||
}
|
||||
|
||||
public <T extends JavaScriptObject> void delete(AsyncCallback<T> cb) {
|
||||
send(DELETE, cb);
|
||||
}
|
||||
|
||||
public <T extends JavaScriptObject> void post(AsyncCallback<T> cb) {
|
||||
send(POST, cb);
|
||||
}
|
||||
|
||||
public <T extends JavaScriptObject> void send(
|
||||
Method method,
|
||||
final AsyncCallback<T> cb) {
|
||||
RequestBuilder req = new RequestBuilder(method, url.toString());
|
||||
req.setHeader("Accept", JsonConstants.JSON_TYPE);
|
||||
if (Gerrit.getAccessToken() != null) {
|
||||
req.setHeader("Authorization", "OAuth " + Gerrit.getAccessToken());
|
||||
}
|
||||
req.setCallback(new MyRequestCallback<T>(cb));
|
||||
if (contentData != null) {
|
||||
req.setHeader("Content-Type", contentType);
|
||||
}
|
||||
try {
|
||||
RpcStatus.INSTANCE.onRpcStart();
|
||||
req.send();
|
||||
req.sendRequest(contentData, new MyRequestCallback<T>(cb));
|
||||
} catch (RequestException e) {
|
||||
RpcStatus.INSTANCE.onRpcComplete();
|
||||
cb.onFailure(e);
|
||||
|
||||
Reference in New Issue
Block a user