Search in sources :

Example 11 with RestResponse

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());
    }
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) JSONObject(org.json.JSONObject) RestResponse(com.salesforce.androidsdk.rest.RestResponse) RestClient(com.salesforce.androidsdk.rest.RestClient) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with RestResponse

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;
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) CompositeResponse(com.salesforce.androidsdk.rest.CompositeResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RestResponse(com.salesforce.androidsdk.rest.RestResponse) CompositeSubResponse(com.salesforce.androidsdk.rest.CompositeResponse.CompositeSubResponse)

Example 13 with RestResponse

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;
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) JSONObject(org.json.JSONObject) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray)

Example 14 with RestResponse

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;
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) JSONObject(org.json.JSONObject) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray)

Example 15 with RestResponse

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;
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) JSONObject(org.json.JSONObject) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray)

Aggregations

RestResponse (com.salesforce.androidsdk.rest.RestResponse)30 RestRequest (com.salesforce.androidsdk.rest.RestRequest)26 JSONArray (org.json.JSONArray)14 JSONObject (org.json.JSONObject)14 RestClient (com.salesforce.androidsdk.rest.RestClient)6 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 AsyncRequestCallback (com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 JSONException (org.json.JSONException)3 NoNetworkException (com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)2 AccountInfoNotFoundException (com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException)2 RestClientCallback (com.salesforce.androidsdk.rest.ClientManager.RestClientCallback)2 URISyntaxException (java.net.URISyntaxException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Context (android.content.Context)1 Intent (android.content.Intent)1 ReactMethod (com.facebook.react.bridge.ReactMethod)1