A lightweight design pattern for consuming a REST API(Zendesk) with APEX.
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
// API we will consume | |
// https://developer.zendesk.com/rest_api/docs/support/users | |
// Create a named credential "Zendesk" with the following value "https://company.zendesk.com/api/v2" | |
// Substitute in your company and the correct version number | |
public class ZendeskService { | |
// create static variable for the endpoint we are going to leverage | |
private static String CUSTOMER_RESOURCE = '/users/create_or_update.json'; | |
// create a lightweight wrapper class to hold the details we care about from the API response | |
// class to hold callout response for parsing | |
private class Response { | |
public Integer code {get;set;} | |
public String body {get;set;} | |
public Boolean success {get;set;} | |
public String errorText {get;set;} // supply to caller if error | |
public String createdZendeskCustomerId {get;set;} | |
public Response(Integer code, String body) { | |
this.code = code; | |
this.body = body; | |
this.success = code >= 200 && code < 300; //all 200 codes are considered success | |
// customize this section futher based on each API resource | |
} | |
} | |
// Methods to perform callouts | |
private static Response ZendeskCallout() { | |
// setup response to api requester | |
Response resp; | |
String method = 'POST'; | |
// check to ensure a callout can be performed | |
if(Limits.getCallouts() >= Limits.getLimitCallouts()) { | |
resp.errorText = 'Maximum number of callouts has been reached.'; | |
} // configure and perform the callout | |
else { | |
// define basic callout variables | |
HttpRequest req = new HttpRequest(); | |
HttpResponse res = new HttpResponse(); | |
Http h = new Http(); | |
// Configure the request | |
// setup request string | |
req.setBody(request); | |
// reference named credential plus static string endpoint value | |
req.setEndpoint('callout:Zendesk' + CUSTOMER_RESOURCE); | |
req.setMethod(method); | |
req.setTimeout(120000); // max callout for APEX is 2 minutes | |
// Configure standard headers (check each API's documentation for header specifics) | |
req.setHeader('Accept', '*/*'); | |
req.setHeader('Content-Type', 'application/json'); // varies on each API (i.e. 'text/xml' :]) | |
req.setHeader('Connection', 'keep-alive'); | |
// Attempt the callout - create log on exception (common: send can fail, deserialization can fail) | |
try { | |
// Perform callout and set response | |
res = h.send(req); | |
system.debug(res); | |
system.debug(res.getBody()); | |
resp = new Response(res.getStatusCode(), res.getBody()); | |
// check response | |
if(resp.success) { | |
// deserialize the response into the inner class | |
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody()); | |
resp.createdZendeskCustomerId = handleResponseCustomerId(results); | |
} else { | |
// callout failed | |
resp.errorText = 'Callout failed. Failed to create Zendesk customers.' + res.getError(); | |
} | |
} catch(Exception ex) { | |
resp.errorText = 'An exception has een encountered while callout out to Integration: ' + ex.getMessage(); | |
} | |
} | |
// return the response | |
return resp; | |
} | |
// parse method to extract user data of the JSON returned | |
private static String handleResponseCustomerId(Map<String, Object> results) { | |
String returnCustomerId; | |
Map<String, Object> user = (Map<String, Object>) results.get('user'); | |
returnCustomerId = String.valueOf((Long)user.get('id')); | |
return returnCustomerId; | |
} | |
} |
No comments:
Post a Comment