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,17 +124,10 @@ public class RestApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if (200 <= status && status < 300) {
|
} else if (200 <= status && status < 300) {
|
||||||
if (!isJsonBody(res)) {
|
|
||||||
if (!background) {
|
|
||||||
RpcStatus.INSTANCE.onRpcComplete();
|
|
||||||
}
|
|
||||||
cb.onFailure(new StatusCodeException(SC_BAD_RESPONSE, "Expected "
|
|
||||||
+ JSON_TYPE + "; received Content-Type: "
|
|
||||||
+ res.getHeader("Content-Type")));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
T data;
|
T data;
|
||||||
|
if (isTextBody(res)) {
|
||||||
|
data = NativeString.wrap(res.getText()).cast();
|
||||||
|
} else if (isJsonBody(res)) {
|
||||||
try {
|
try {
|
||||||
// javac generics bug
|
// javac generics bug
|
||||||
data = RestApi.<T>cast(parseJson(res));
|
data = RestApi.<T>cast(parseJson(res));
|
||||||
@@ -146,6 +139,15 @@ public class RestApi {
|
|||||||
"Invalid JSON: " + e.getMessage()));
|
"Invalid JSON: " + e.getMessage()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (!background) {
|
||||||
|
RpcStatus.INSTANCE.onRpcComplete();
|
||||||
|
}
|
||||||
|
cb.onFailure(new StatusCodeException(SC_BAD_RESPONSE, "Expected "
|
||||||
|
+ JSON_TYPE + " or " + TEXT_TYPE + "; received Content-Type: "
|
||||||
|
+ res.getHeader("Content-Type")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
cb.onSuccess(data);
|
cb.onSuccess(data);
|
||||||
if (!background) {
|
if (!background) {
|
||||||
|
|||||||
Reference in New Issue
Block a user