use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class SalesforceNetworkPlugin method sendRequest.
/**
* Native implementation for "sendRequest" action.
*
* @param callbackContext Used when calling back into Javascript.
*/
protected void sendRequest(JSONArray args, final CallbackContext callbackContext) {
try {
final RestRequest request = prepareRestRequest(args);
final boolean returnBinary = ((JSONObject) args.get(0)).optBoolean(RETURN_BINARY, false);
final boolean doesNotRequireAuth = ((JSONObject) args.get(0)).optBoolean(DOES_NOT_REQUIRE_AUTHENTICATION, false);
// Sends the request.
final RestClient restClient = getRestClient(doesNotRequireAuth);
if (restClient == null) {
return;
}
restClient.sendAsync(request, new RestClient.AsyncRequestCallback() {
@Override
public void onSuccess(RestRequest request, RestResponse response) {
try {
// Not a 2xx status
if (!response.isSuccess()) {
final JSONObject responseObject = new JSONObject();
responseObject.put("headers", new JSONObject(response.getAllHeaders()));
responseObject.put("statusCode", response.getStatusCode());
responseObject.put("body", parsedResponse(response));
final JSONObject errorObject = new JSONObject();
errorObject.put("response", responseObject);
callbackContext.error(errorObject.toString());
} else // Binary response
if (returnBinary) {
JSONObject result = new JSONObject();
result.put(CONTENT_TYPE, response.getContentType());
result.put(ENCODED_BODY, Base64.encodeToString(response.asBytes(), Base64.DEFAULT));
callbackContext.success(result);
} else // Some response
if (response.asBytes().length > 0) {
final Object parsedResponse = parsedResponse(response);
if (parsedResponse instanceof JSONObject) {
callbackContext.success((JSONObject) parsedResponse);
} else if (parsedResponse instanceof JSONArray) {
callbackContext.success((JSONArray) parsedResponse);
} else {
callbackContext.success((String) parsedResponse);
}
} else // No response
{
callbackContext.success();
}
} catch (Exception e) {
SalesforceHybridLogger.e(TAG, "Error while parsing response", e);
onError(e);
}
}
@Override
public void onError(Exception exception) {
final JSONObject errorObject = new JSONObject();
try {
errorObject.put("error", exception.getMessage());
} catch (JSONException jsonException) {
SalesforceHybridLogger.e(TAG, "Error creating error object", jsonException);
}
callbackContext.error(errorObject.toString());
}
});
} catch (Exception exception) {
final JSONObject errorObject = new JSONObject();
try {
errorObject.put("error", exception.getMessage());
} catch (JSONException jsonException) {
SalesforceHybridLogger.e(TAG, "Error creating error object", jsonException);
}
callbackContext.error(errorObject.toString());
}
}
use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class CompositeRequestHelper method sendCompositeRequest.
/**
* Build and send composite request
* @param syncManager
* @param allOrNone
* @param refIdToRequests
* @return map of ref id to composite sub response
* @throws JSONException
* @throws IOException
*/
public static Map<String, CompositeSubResponse> sendCompositeRequest(SyncManager syncManager, boolean allOrNone, LinkedHashMap<String, RestRequest> refIdToRequests) throws JSONException, IOException {
RestRequest compositeRequest = RestRequest.getCompositeRequest(syncManager.apiVersion, allOrNone, refIdToRequests);
RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(compositeRequest);
if (!response.isSuccess()) {
throw new SyncManager.MobileSyncException("sendCompositeRequest:" + response.toString());
}
CompositeResponse compositeResponse = new CompositeResponse(response.asJSONObject());
Map<String, CompositeSubResponse> refIdToResponses = new HashMap<>();
for (CompositeSubResponse subResponse : compositeResponse.subResponses) {
refIdToResponses.put(subResponse.referenceId, subResponse);
}
return refIdToResponses;
}
use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class LayoutSyncDownTarget method startFetch.
@Override
public JSONArray startFetch(SyncManager syncManager, long maxTimeStamp) throws IOException, JSONException {
final RestRequest request = RestRequest.getRequestForObjectLayout(syncManager.apiVersion, objectAPIName, formFactor, layoutType, mode, recordTypeId);
final RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
final JSONObject responseJSON = response.asJSONObject();
if (responseJSON != null) {
responseJSON.put(Constants.ID, String.format(ID_FIELD_VALUE, objectAPIName, formFactor, layoutType, mode, recordTypeId));
}
final JSONArray records = new JSONArray();
records.put(response.asJSONObject());
// Recording total size.
totalSize = 1;
return records;
}
use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class MetadataSyncDownTarget method startFetch.
@Override
public JSONArray startFetch(SyncManager syncManager, long maxTimeStamp) throws IOException, JSONException {
final RestRequest request = RestRequest.getRequestForDescribe(syncManager.apiVersion, objectType);
final RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
final JSONObject responseJSON = response.asJSONObject();
if (responseJSON != null) {
responseJSON.put(Constants.ID, objectType);
}
final JSONArray records = new JSONArray();
records.put(response.asJSONObject());
// Recording total size.
totalSize = 1;
return records;
}
use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class SoqlSyncDownTarget method continueFetch.
@Override
public JSONArray continueFetch(SyncManager syncManager) throws IOException, JSONException {
if (nextRecordsUrl == null) {
return null;
}
RestRequest request = new RestRequest(RestRequest.RestMethod.GET, nextRecordsUrl);
RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
JSONObject responseJson = getResponseJson(response);
JSONArray records = getRecordsFromResponseJson(responseJson);
// Captures next records URL.
nextRecordsUrl = JSONObjectHelper.optString(responseJson, Constants.NEXT_RECORDS_URL);
return records;
}
Aggregations