Created
April 5, 2016 01:34
-
-
Save izeta/a441e1c4cd4306d0a38a3adb7e7c01f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Loop through | |
for(Integer i = 0; i < contacts.size(); i++){ | |
c = contacts.get(i); | |
if(oldAccountIds != null) | |
oldAccount = oldAccountIds.get(c.Id); | |
else | |
oldAccount = null; | |
items.add(new Map<string, object>{ | |
'method' => 'post', | |
'data_type' => 'user', | |
'data' => getIntercomUserFromContact(c, mappedFields, oldAccount) | |
}); | |
listIdx++; | |
if(listIdx == 100){ | |
listIdx = 0; | |
resp = sendRequest(USERS_ENDPOINT_BULK, 'POST', request); | |
request.put('job', new Map<String, String>{ 'id' => (String)resp.get('id') }); | |
items.clear(); | |
} | |
} | |
if(items.size() > 0) | |
{ | |
sendRequest(USERS_ENDPOINT_BULK, 'POST', request); | |
} | |
private static Map<String, Object> sendRequest(String endpoint, String method, object request){ | |
string jsonContents = Json.serialize(request); | |
// Build the request | |
HttpRequest req = new HttpRequest(); | |
req.setMethod(method); | |
req.setEndpoint(endpoint); | |
req.setTimeout(60000); | |
Blob headerValue = Blob.valueOf(APP_ID + ':' + API_KEY); | |
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); | |
req.setHeader('Authorization', authorizationHeader); | |
req.setHeader('Accept', 'application/json'); | |
req.setHeader('Content-Type', 'application/json'); | |
req.setBody(jsonContents); | |
// create the client | |
Http h = new Http(); | |
System.debug(Logginglevel.INFO, 'Sending request: ' + req.toString() + ' with data: ' + jsonContents); | |
try{ | |
HttpResponse resp = h.send(req); | |
String respBody = resp.getBody(); | |
System.debug(Logginglevel.INFO, 'Received response: ' + respBody); | |
return (Map<String,Object>)Json.deserializeUntyped(respBody); | |
}catch(Exception ex){ | |
System.debug(Logginglevel.ERROR, ex.getMessage()); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment