Search in sources :

Example 26 with RestResponse

use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.

the class ManagerTestCase method createRecordsOnServerReturnFields.

/**
 * Helper methods to create "count" of test records
 * @param count
 * @param additionalFields
 * @return map of id to map of field name to field value for the created records
 * @throws Exception
 */
protected Map<String, Map<String, Object>> createRecordsOnServerReturnFields(int count, String objectType, Map<String, Object> additionalFields) throws Exception {
    List<Map<String, Object>> listFields = buildFieldsMapForRecords(count, objectType, additionalFields);
    // Prepare request
    List<RestRequest> requests = new ArrayList<>();
    for (Map<String, Object> fields : listFields) {
        requests.add(RestRequest.getRequestForCreate(apiVersion, objectType, fields));
    }
    final RestRequest batchRequest = RestRequest.getBatchRequest(apiVersion, false, requests);
    // Go to server
    RestResponse response = restClient.sendSync(batchRequest);
    Assert.assertTrue("Creates failed", response.isSuccess() && !response.asJSONObject().getBoolean("hasErrors"));
    Map<String, Map<String, Object>> idToFields = new HashMap<>();
    JSONArray results = response.asJSONObject().getJSONArray("results");
    for (int i = 0; i < results.length(); i++) {
        JSONObject result = results.getJSONObject(i);
        Assert.assertEquals("Status should be HTTP_CREATED", HttpURLConnection.HTTP_CREATED, result.getInt("statusCode"));
        String id = result.getJSONObject("result").getString(LID);
        Map<String, Object> fields = listFields.get(i);
        idToFields.put(id, fields);
    }
    return idToFields;
}
Also used : HashMap(java.util.HashMap) RestResponse(com.salesforce.androidsdk.rest.RestResponse) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) RestRequest(com.salesforce.androidsdk.rest.RestRequest) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with RestResponse

use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.

the class SyncManagerTestCase method checkServer.

/**
 * Check records on server
 * @param idToFields
 * @param sObjectType
 * @throws IOException
 * @throws JSONException
 */
protected void checkServer(Map<String, Map<String, Object>> idToFields, String sObjectType) throws IOException, JSONException {
    String[] fieldNames = idToFields.get(idToFields.keySet().toArray(new String[0])[0]).keySet().toArray(new String[0]);
    String soql = String.format("SELECT %s, %s FROM %s WHERE %s IN %s", Constants.ID, TextUtils.join(",", fieldNames), sObjectType, Constants.ID, makeInClause(idToFields.keySet()));
    RestRequest request = RestRequest.getRequestForQuery(ApiVersionStrings.getVersionNumber(targetContext), soql);
    RestResponse response = restClient.sendSync(request);
    JSONArray records = response.asJSONObject().getJSONArray(RECORDS);
    Assert.assertEquals("Wrong number of records", idToFields.size(), records.length());
    for (int i = 0; i < records.length(); i++) {
        JSONObject row = records.getJSONObject(i);
        Map<String, Object> expectedFields = idToFields.get(row.get(Constants.ID));
        for (String fieldName : fieldNames) {
            Assert.assertEquals("Wrong value for field: " + fieldName, expectedFields.get(fieldName), JSONObjectHelper.opt(row, fieldName));
        }
    }
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) JSONObject(org.json.JSONObject) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 28 with RestResponse

use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.

the class SalesforceDroidGapActivity method authenticate.

/**
 * Get a RestClient and refresh the auth token
 *
 * @param callbackContext when not null credentials/errors are sent through to callbackContext.success()/error()
 */
public void authenticate(final CallbackContext callbackContext) {
    SalesforceHybridLogger.i(TAG, "authenticate called");
    clientManager.getRestClient(this, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            if (client == null) {
                SalesforceHybridLogger.i(TAG, "authenticate callback triggered with null client");
                logout(null);
            } else {
                SalesforceHybridLogger.i(TAG, "authenticate callback triggered with actual client");
                SalesforceDroidGapActivity.this.client = client;
                /*
                     * Do a cheap REST call to refresh the access token if needed.
                     * If the login took place a while back (e.g. the already logged
                     * in application was restarted), then the returned session ID
                     * (access token) might be stale. This is not an issue if one
                     * uses exclusively RestClient for calling the server because
                     * it takes care of refreshing the access token when needed,
                     * but a stale session ID will cause the WebView to redirect
                     * to the web login.
                     */
                SalesforceDroidGapActivity.this.client.sendAsync(RestRequest.getRequestForUserInfo(), new AsyncRequestCallback() {

                    @Override
                    public void onSuccess(RestRequest request, RestResponse response) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                /*
                                     * The client instance being used here needs to be
                                     * refreshed, to ensure we use the new access token.
                                     */
                                SalesforceDroidGapActivity.this.client = SalesforceDroidGapActivity.this.clientManager.peekRestClient();
                                getAuthCredentials(callbackContext);
                            }
                        });
                    }

                    @Override
                    public void onError(Exception exception) {
                        if (callbackContext != null) {
                            callbackContext.error(exception.getMessage());
                        }
                    }
                });
            }
        }
    });
}
Also used : AsyncRequestCallback(com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback) RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) RestClient(com.salesforce.androidsdk.rest.RestClient) AccountInfoNotFoundException(com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException) NoNetworkException(com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)

Example 29 with RestResponse

use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.

the class MruSyncDownTarget method startFetch.

@Override
public JSONArray startFetch(SyncManager syncManager, long maxTimeStamp) throws IOException, JSONException {
    final RestRequest request = RestRequest.getRequestForMetadata(syncManager.apiVersion, objectType);
    final RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
    final List<String> recentItems = JSONObjectHelper.pluck(response.asJSONObject().getJSONArray(Constants.RECENT_ITEMS), Constants.ID);
    // Building SOQL query to get requested at.
    final String soql = SOQLBuilder.getInstanceWithFields(fieldlist).from(objectType).where(getIdFieldName() + " IN ('" + TextUtils.join("', '", recentItems) + "')").build();
    return startFetch(syncManager, maxTimeStamp, soql);
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse)

Example 30 with RestResponse

use of com.salesforce.androidsdk.rest.RestResponse in project SalesforceMobileSDK-Android by forcedotcom.

the class MruSyncDownTarget method startFetch.

private JSONArray startFetch(SyncManager syncManager, long maxTimeStamp, String queryRun) throws IOException, JSONException {
    final RestRequest request = RestRequest.getRequestForQuery(syncManager.apiVersion, queryRun);
    final RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
    final JSONObject responseJson = response.asJSONObject();
    final JSONArray records = responseJson.getJSONArray(Constants.RECORDS);
    // Recording total size
    totalSize = records.length();
    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