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;
}
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));
}
}
}
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());
}
}
});
}
}
});
}
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);
}
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;
}
Aggregations