Search in sources :

Example 1 with RestRequest

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

the class ContentSoqlSyncDownTarget method buildSoapRequest.

/**
 * @param sessionId
 * @param body
 * @return request for soap call
 * @throws UnsupportedEncodingException
 */
private RestRequest buildSoapRequest(String sessionId, String body) throws UnsupportedEncodingException {
    Map<String, String> customHeaders = new HashMap<String, String>();
    customHeaders.put("SOAPAction", "\"\"");
    RequestBody requestBody = RequestBody.create(MEDIA_TYPE_XML, String.format(REQUEST_TEMPLATE, sessionId, body));
    String version = ApiVersionStrings.getVersionNumber(SalesforceSDKManager.getInstance().getAppContext()).substring(1);
    /* no v */
    ;
    return new RestRequest(RestRequest.RestMethod.POST, "/services/Soap/u/" + version, requestBody, customHeaders);
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) HashMap(java.util.HashMap) RequestBody(okhttp3.RequestBody)

Example 2 with RestRequest

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

the class ParentChildrenSyncTestCase method createAccountsAndContactsOnServer.

protected void createAccountsAndContactsOnServer(int numberAccounts, int numberContactsPerAccount) throws Exception {
    accountIdToFields = new HashMap<>();
    accountIdContactIdToFields = new HashMap<>();
    Map<String, Map<String, Object>> refIdToFields = new HashMap<>();
    List<RestRequest.SObjectTree> accountTrees = new ArrayList<>();
    List<Map<String, Object>> listAccountFields = buildFieldsMapForRecords(numberAccounts, Constants.ACCOUNT, null);
    for (int i = 0; i < listAccountFields.size(); i++) {
        List<Map<String, Object>> listContactFields = buildFieldsMapForRecords(numberContactsPerAccount, Constants.CONTACT, null);
        String refIdAccount = "refAccount_" + i;
        Map<String, Object> accountFields = listAccountFields.get(i);
        refIdToFields.put(refIdAccount, accountFields);
        List<RestRequest.SObjectTree> contactTrees = new ArrayList<>();
        for (int j = 0; j < listContactFields.size(); j++) {
            String refIdContact = refIdAccount + ":refContact_" + j;
            Map<String, Object> contactFields = listContactFields.get(j);
            refIdToFields.put(refIdContact, contactFields);
            contactTrees.add(new RestRequest.SObjectTree(Constants.CONTACT, Constants.CONTACTS, refIdContact, contactFields, null));
        }
        accountTrees.add(new RestRequest.SObjectTree(Constants.ACCOUNT, null, refIdAccount, accountFields, contactTrees));
    }
    RestRequest request = RestRequest.getRequestForSObjectTree(apiVersion, Constants.ACCOUNT, accountTrees);
    // Send request
    RestResponse response = restClient.sendSync(request);
    // Parse response
    Map<String, String> refIdToId = new HashMap<>();
    JSONArray results = response.asJSONObject().getJSONArray("results");
    for (int i = 0; i < results.length(); i++) {
        JSONObject result = results.getJSONObject(i);
        String refId = result.getString(RestRequest.REFERENCE_ID);
        String id = result.getString(Constants.LID);
        refIdToId.put(refId, id);
    }
    // Populate accountIdToFields and accountIdContactIdToFields
    for (String refId : refIdToId.keySet()) {
        Map<String, Object> fields = refIdToFields.get(refId);
        String[] parts = refId.split(":");
        String accountId = refIdToId.get(parts[0]);
        String contactId = parts.length > 1 ? refIdToId.get(refId) : null;
        if (contactId == null) {
            accountIdToFields.put(accountId, fields);
        } else {
            if (!accountIdContactIdToFields.containsKey(accountId))
                accountIdContactIdToFields.put(accountId, new HashMap<String, Map<String, Object>>());
            accountIdContactIdToFields.get(accountId).put(contactId, fields);
        }
    }
}
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 3 with RestRequest

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

the class SyncManagerTestCase method checkServerDeleted.

/**
 * Check that records were deleted from server
 * @param ids
 * @param sObjectType
 * @throws IOException
 */
protected void checkServerDeleted(String[] ids, String sObjectType) throws IOException, JSONException {
    String soql = String.format("SELECT %s FROM %s WHERE %s IN %s", Constants.ID, sObjectType, Constants.ID, makeInClause(ids));
    RestRequest request = RestRequest.getRequestForQuery(ApiVersionStrings.getVersionNumber(targetContext), soql);
    RestResponse response = restClient.sendSync(request);
    JSONArray records = response.asJSONObject().getJSONArray(RECORDS);
    Assert.assertEquals("No accounts should have been returned from server", 0, records.length());
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) JSONArray(org.json.JSONArray)

Example 4 with RestRequest

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

the class FileRequests method uploadFile.

/**
 * Build a request that can upload a new file to the server, this will
 * create a new file at version 1.
 *
 * @param theFile The path of the local file to upload to the server.
 * @param name The name of this file.
 * @param title The title of this file.
 * @param description A description of the file.
 * @param mimeType The mime-type of the file, if known.
 * @return A RestRequest that can perform this upload.
 */
public static RestRequest uploadFile(File theFile, String name, String title, String description, String mimeType) {
    MediaType mediaType = MediaType.parse(mimeType);
    MultipartBody.Builder builder = new MultipartBody.Builder();
    if (title != null)
        builder.addFormDataPart("title", title);
    if (description != null)
        builder.addFormDataPart("desc", description);
    builder.addFormDataPart("fileData", name, RequestBody.create(mediaType, theFile));
    return new RestRequest(RestMethod.POST, base("connect/files/users").appendPath("me").toString(), builder.build(), HTTP_HEADERS);
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType)

Example 5 with RestRequest

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

the class ExplorerActivity method onAddFileShareClick.

/**
 * Called when the "add file share" button is clicked.
 *
 * @param v View that was clicked.
 */
public void onAddFileShareClick(View v) {
    final String documentId = ((EditText) findViewById(R.id.add_file_share_document_id_text)).getText().toString();
    final String entityId = ((EditText) findViewById(R.id.add_file_share_entity_id_text)).getText().toString();
    final String shareType = ((EditText) findViewById(R.id.add_file_share_share_type_text)).getText().toString();
    RestRequest request = FileRequests.addFileShare(documentId, entityId, shareType);
    sendRequest(request);
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest)

Aggregations

RestRequest (com.salesforce.androidsdk.rest.RestRequest)51 RestResponse (com.salesforce.androidsdk.rest.RestResponse)26 JSONObject (org.json.JSONObject)20 JSONArray (org.json.JSONArray)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 HashMap (java.util.HashMap)7 RestClient (com.salesforce.androidsdk.rest.RestClient)5 RequestBody (okhttp3.RequestBody)5 LinkedHashMap (java.util.LinkedHashMap)4 CompositeSubResponse (com.salesforce.androidsdk.rest.CompositeResponse.CompositeSubResponse)3 AsyncRequestCallback (com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 JSONException (org.json.JSONException)3 SmallTest (androidx.test.filters.SmallTest)2 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