Search in sources :

Example 21 with RestResponse

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

the class SyncUpTarget method fetchLastModifiedDate.

/**
 * Fetch last modified date for a given record
 * @param syncManager
 * @param record
 * @return
 */
protected RecordModDate fetchLastModifiedDate(SyncManager syncManager, JSONObject record) throws JSONException, IOException {
    final String objectType = (String) SmartStore.project(record, Constants.SOBJECT_TYPE);
    final String objectId = record.getString(getIdFieldName());
    RestRequest lastModRequest = RestRequest.getRequestForRetrieve(syncManager.apiVersion, objectType, objectId, Arrays.asList(getModificationDateFieldName()));
    RestResponse lastModResponse = syncManager.sendSyncWithMobileSyncUserAgent(lastModRequest);
    return new RecordModDate(lastModResponse.isSuccess() ? lastModResponse.asJSONObject().getString(getModificationDateFieldName()) : null, lastModResponse.getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND);
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse)

Example 22 with RestResponse

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

the class IDPRequestHandler method getValidAccessToken.

/**
 * Makes a simple REST call to and returns a valid access token or null if refresh failed.
 * This method should NOT be called from the main thread since it makes a network request.
 *
 * @return Valid access token.
 * @throws IDPRequestHandlerException
 */
public String getValidAccessToken() throws IDPRequestHandlerException {
    /*
         * Ensures the access token associated with this user account is valid and refreshes the
         * token if it's not valid. Any request to 'frontdoor' must be made with a valid token.
         */
    final Context context = SalesforceSDKManager.getInstance().getAppContext();
    RestResponse restResponse = null;
    Exception exception = null;
    String accessToken = null;
    try {
        restResponse = restClient.sendSync(RestRequest.getRequestForUserInfo());
    } catch (IOException e) {
        exception = e;
    } finally {
        if (restResponse == null || !restResponse.isSuccess()) {
            handleError("Invalid REST client", exception);
        } else {
            accessToken = restClient.getAuthToken();
        }
    }
    return accessToken;
}
Also used : Context(android.content.Context) RestResponse(com.salesforce.androidsdk.rest.RestResponse) IOException(java.io.IOException) IOException(java.io.IOException)

Example 23 with RestResponse

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

the class ContentSoqlSyncDownTarget method startFetch.

@Override
public JSONArray startFetch(SyncManager syncManager, long maxTimeStamp) throws IOException, JSONException {
    String queryToRun = maxTimeStamp > 0 ? SoqlSyncDownTarget.addFilterForReSync(getQuery(maxTimeStamp), getModificationDateFieldName(), maxTimeStamp) : getQuery(maxTimeStamp);
    // cheap call to refresh session
    syncManager.getRestClient().sendSync(RestRequest.getRequestForUserInfo());
    RestRequest request = buildQueryRequest(syncManager.getRestClient().getAuthToken(), queryToRun);
    RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
    JSONArray records = parseSoapResponse(response);
    return records;
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray)

Example 24 with RestResponse

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

the class ContentSoqlSyncDownTarget method continueFetch.

@Override
public JSONArray continueFetch(SyncManager syncManager) throws IOException, JSONException {
    if (queryLocator == null) {
        return null;
    }
    RestRequest request = buildQueryMoreRequest(syncManager.getRestClient().getAuthToken(), queryLocator);
    RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(request);
    JSONArray records = parseSoapResponse(response);
    return records;
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray)

Example 25 with RestResponse

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

the class ManagerTestCase method updateRecordsOnServer.

/**
 * Update records on server
 * @param idToFieldsUpdated
 * @param sObjectType
 * @throws Exception
 */
protected void updateRecordsOnServer(Map<String, Map<String, Object>> idToFieldsUpdated, String sObjectType) throws Exception {
    List<RestRequest> requests = new ArrayList<>();
    for (String id : idToFieldsUpdated.keySet()) {
        requests.add(RestRequest.getRequestForUpdate(apiVersion, sObjectType, id, idToFieldsUpdated.get(id)));
    }
    RestResponse response = restClient.sendSync(RestRequest.getBatchRequest(apiVersion, false, requests));
    Assert.assertTrue("Updates failed", response.isSuccess() && !response.asJSONObject().getBoolean("hasErrors"));
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) ArrayList(java.util.ArrayList)

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