Allow RestApi calls to receive text from handlers
Wrap the result in a NativeString, and naively cast it to whatever the callback is asking for (which is not particularly less safe than what we do for JSON). Change-Id: I1807ce33544964533f8d2a88c45576c18f4394ec
This commit is contained in:
committed by
Edwin Kempin
parent
49ef37b99b
commit
e4563e0b97
@@ -124,29 +124,31 @@ public class RestApi {
|
||||
}
|
||||
|
||||
} else if (200 <= status && status < 300) {
|
||||
if (!isJsonBody(res)) {
|
||||
T data;
|
||||
if (isTextBody(res)) {
|
||||
data = NativeString.wrap(res.getText()).cast();
|
||||
} else if (isJsonBody(res)) {
|
||||
try {
|
||||
// javac generics bug
|
||||
data = RestApi.<T>cast(parseJson(res));
|
||||
} catch (JSONException e) {
|
||||
if (!background) {
|
||||
RpcStatus.INSTANCE.onRpcComplete();
|
||||
}
|
||||
cb.onFailure(new StatusCodeException(SC_BAD_RESPONSE,
|
||||
"Invalid JSON: " + e.getMessage()));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (!background) {
|
||||
RpcStatus.INSTANCE.onRpcComplete();
|
||||
}
|
||||
cb.onFailure(new StatusCodeException(SC_BAD_RESPONSE, "Expected "
|
||||
+ JSON_TYPE + "; received Content-Type: "
|
||||
+ JSON_TYPE + " or " + TEXT_TYPE + "; received Content-Type: "
|
||||
+ res.getHeader("Content-Type")));
|
||||
return;
|
||||
}
|
||||
|
||||
T data;
|
||||
try {
|
||||
// javac generics bug
|
||||
data = RestApi.<T>cast(parseJson(res));
|
||||
} catch (JSONException e) {
|
||||
if (!background) {
|
||||
RpcStatus.INSTANCE.onRpcComplete();
|
||||
}
|
||||
cb.onFailure(new StatusCodeException(SC_BAD_RESPONSE,
|
||||
"Invalid JSON: " + e.getMessage()));
|
||||
return;
|
||||
}
|
||||
|
||||
cb.onSuccess(data);
|
||||
if (!background) {
|
||||
RpcStatus.INSTANCE.onRpcComplete();
|
||||
|
||||
Reference in New Issue
Block a user