Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created November 12, 2018 08:28
Show Gist options
  • Save swapnilshrikhande/c4cccbd28881726436ad7fa837cfe1ad to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/c4cccbd28881726436ad7fa837cfe1ad to your computer and use it in GitHub Desktop.
Simple Rest Utility To help consume a rest service from salesforce.
/*
How to use RestUtility
1. RestUtility.URL_ENCODE_PARAMS to true if you wish to encode the url parameters,
by default they are not encoded.
2. getAuthorizationHeader(String username,String password) to use basic authentication method.
*/
public with sharing class RestUtility {
public static final String BASIC = 'Basic ';
public static Boolean URL_ENCODE_PARAMS = false;
public Enum HttpMethod { GET, POST, PUT, PATCH }
public RestUtility()
{
}
public static String getAuthorizationHeader(String username,String password){
Blob headerValue = Blob.valueOf( username+':'+password);
String authorizationHeader = RestUtility.BASIC+EncodingUtil.base64Encode(headerValue);
return authorizationHeader;
}
// Method to create HTTP request with parameters
public static HttpRequest createHttpRequest(String endpoint,
HttpMethod method,
Map<String, String> params,
Map<String, String> headers, String body)
{
HttpRequest request = new HttpRequest();
request.setMethod(method.name());
request.setTimeout(ConstantUtility.SET_TIMEOUT);
if (method == HttpMethod.GET && params != null) {
endpoint += '?';
for (String name : params.keySet()) {
endpoint += name + '=' + encode(params.get(name)) +'&';
}
}
endpoint = endpoint.removeEnd('&');
request.setEndpoint(endpoint);
if ((method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) && body!= null) {
request.setBody(body);
}
if (headers != null) {
for (String header : headers.keySet())
{
request.setHeader(header, headers.get(header));
}
}
return request;
}
// Method to create a POST HTTP request
public static HttpRequest createPostRequest (String endpoint, Map<String, String> headers, String body) {
return createHttpRequest(endpoint, HttpMethod.POST, null, headers, body);
}
// Method to create a PUT HTTP request
/*public static HttpRequest createPutRequest (String endpoint, Map<String, String> headers, String body) {
return createHttpRequest(endpoint, HttpMethod.PUT, null, headers, body);
}*/
// Method to create a GET HTTP request
public static HttpRequest createGetRequest (String endpoint, Map<String, String> headers, Map<String, String> params) {
return createHttpRequest(endpoint, HttpMethod.GET, params, headers, null);
}
// Method to create a PATCH HTTP request
/*public static HttpRequest createPatchRequest (String endpoint, Map<String, String> headers, String body) {
return createHttpRequest(endpoint, HttpMethod.POST, null, headers, body);
}*/
// Method to get the response as String
public static HttpResponse getResponse (HttpRequest request) {
HttpResponse response;
try {
Http http = new Http();
response = http.send(request);
} catch(System.LimitException ex) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, ConstantUtility.MESSAGE_CALLOUT_LIMIT));
} return response;
}
public static String encode(String value){
return URL_ENCODE_PARAMS ? EncodingUtil.urlEncode(value, 'UTF-8') : value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment