Created
May 23, 2020 18:33
-
-
Save sudikrt/afb691041350244505c98e70530b1c2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public with sharing virtual class CalloutUtils { | |
private HttpRequest request; | |
private Http httpInstance; | |
private HttpResponse response; | |
private String method; | |
private String endPoint; | |
private String body; | |
private Integer timeout; | |
private Map<String, String> headers; | |
private Map<String, List<String>> parameters; | |
public CalloutUtils () { | |
httpInstance = new Http (); | |
response = new HttpResponse (); | |
headers = new Map <String, String> (); | |
parameters = new Map <String, List<String>> (); | |
} | |
public virtual CalloutUtils endPoint (String endPoint) { | |
this.endPoint = endPoint; | |
return this; | |
} | |
public virtual CalloutUtils method (String method) { | |
this.method = method; | |
return this; | |
} | |
public virtual CalloutUtils body (String body) { | |
this.body = body; | |
return this; | |
} | |
public virtual CalloutUtils bodyTOJSON (Object obj) { | |
this.body = JSON.serialize(obj); | |
return this; | |
} | |
public virtual CalloutUtils timeout (Integer timeout) { | |
this.timeout = timeout; | |
return this; | |
} | |
public virtual CalloutUtils addHeader (String key, String value) { | |
this.headers.put(key, value); | |
return this; | |
} | |
public virtual CalloutUtils addHeader (Map<String, String> headers) { | |
this.headers.putAll(headers); | |
return this; | |
} | |
public virtual CalloutUtils addParameter (String key, String value) { | |
if (this.parameters.get(key) == null) { | |
this.parameters.put(key, new List<String> ()); | |
} | |
this.parameters.get (key).add(value); | |
return this; | |
} | |
public virtual CalloutUtils addParameter(Map<String,List<String>> params) { | |
for(String parameter : params.keySet() ) { | |
this.parameters.put(parameter,params.get(parameter)); | |
} | |
return this; | |
} | |
public virtual String buildParameter (String key, List<String> values) { | |
String parameter; | |
Integer count = 0; | |
for (String value : values) { | |
count ++; | |
parameter += value; | |
parameter += (count == values.size()) ? '' : ','; | |
} | |
return parameter; | |
} | |
public virtual String buildQueryStringParameter () { | |
String queryString = this.endPoint; | |
if (!this.parameters.isEmpty()) { | |
queryString += '?'; | |
Integer count = 0; | |
for (String key : this.parameters.keySet()) { | |
if (count == 0) { | |
queryString += key + '=' + buildParameter(key,parameters.get(key)); | |
} else { | |
queryString += '&' + key + '=' + buildParameter(key,parameters.get(key)); | |
} | |
count ++; | |
} | |
} | |
return queryString; | |
} | |
public virtual CalloutUtils builder(){ | |
if (String.isBlank(this.method) || String.isEmpty(this.method)) { | |
throw new CalloutUtilsException ('Method not found :)'); | |
} | |
if (String.isBlank(this.endPoint) || String.isEmpty(this.endPoint)) { | |
throw new CalloutUtilsException ('Endpoint not found :)'); | |
} | |
if (this.timeout != null && this.timeout > 120000) { | |
throw new CalloutUtilsException ('Maximum timeout secod is exceeded'); | |
} | |
this.request = new HttpRequest (); | |
this.request.setMethod(this.method); | |
this.request.setEndpoint(this.endpoint); | |
if (this.body != null) { | |
this.request.setBody(this.body); | |
} | |
if (this.timeout != null) { | |
this.request.setTimeout(this.timeout); | |
} | |
if (!headers.isEmpty()) { | |
for(String header : headers.keySet() ) { | |
request.setHeader( header , headers.get( header ) ); | |
} | |
} | |
if(!parameters.isEmpty()){ | |
this.request.setEndpoint(buildQueryStringParameter()); | |
} | |
return this; | |
} | |
public virtual HttpResponse send () { | |
try { | |
this.builder(); | |
this.response = httpInstance.send(this.request); | |
} catch (CalloutUtils.CalloutUtilsException ex) { | |
throw new CalloutUtilsException(ex.getMessage()); | |
} catch (Exception ex) { | |
throw new CalloutUtilsException(ex.getMessage()); | |
} | |
return response; | |
} | |
public class CalloutUtilsException extends Exception {} | |
} |
This file contains hidden or 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
@isTest | |
global class CalloutUtilsMock implements HttpCalloutMock { | |
global HTTPResponse respond(HTTPRequest request) { | |
HttpResponse response = new HttpResponse(); | |
response.setHeader('Content-Type', 'application/json'); | |
response.setBody(''); | |
response.setStatusCode(200); | |
return response; | |
} | |
} |
This file contains hidden or 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
public with sharing class RESTUtils { | |
public static Integer STATUS_OK = 200; | |
public static Integer STATUS_BAD = 400; | |
public static Integer STATUS_FORBIDDEN = 403; | |
public static Integer STATUS_NOTFOUND = 404; | |
public static Integer STATUS_CONFLICT = 409; | |
public static Integer STATUS_ISE = 500; | |
public final static String RESPONSE_MESSAGE = 'message'; | |
public final static String RESPONSE_SUCCESS = 'status'; | |
public final static String HEADER_MESSAGE = 'Message'; | |
public final static String BLOCKING_ID = 'blockingId'; | |
public final static String INSTANCE_URL = 'https://cs5.salesforce.com'; | |
public final static String REST_SERVICE = '/services/apexrest'; | |
public static RestResponse getRestRepsonse (RestResponse response, Integer statusCode, Exception ex) { | |
response.statusCode = statusCode; | |
response.addHeader(HEADER_MESSAGE, ex.getMessage()); | |
return response; | |
} | |
public static RestResponse getSuccessResponse (RestResponse response) { | |
response.statusCode = STATUS_OK; | |
return response; | |
} | |
public static RestResponse getRestRepsonse(RestResponse response, Exception ex) { | |
Integer statusCode = STATUS_ISE; | |
if (ex instanceof VUtility.InvalidPermissionsException) { | |
statusCode = STATUS_FORBIDDEN; | |
} else if (ex instanceof VUtility.BadException){ | |
statusCode = STATUS_BAD; | |
} else if (ex instanceof VUtility.UnknownException) { | |
statusCode = STATUS_NOTFOUND; | |
} else if (ex instanceof VUtility.ConflictException) { | |
statusCode = STATUS_CONFLICT; | |
} | |
return getRestRepsonse(response, statusCode, ex); | |
} | |
public static Map<String, Object> getDefaultResponse () { | |
Map<String, Object> res = new Map<String, Object> (); | |
res.put (RESPONSE_SUCCESS, false); | |
res.put (RESPONSE_MESSAGE, ''); | |
return res; | |
} | |
} |
This file contains hidden or 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
public with sharing class VUtility { | |
public static String getAllFieldNames (String objName) { | |
if (String.isBlank (objName)) { | |
return null; | |
} | |
SObjectType objectType = Schema.getGlobalDescribe().get(objName); | |
Map<String,Schema.SObjectField> mfields = objectType.getDescribe().fields.getMap(); | |
String query = ''; | |
Integer index = 0; | |
for (String s: mfields.keySet ()) { | |
index++; | |
query += s; | |
if (index != mfields.keySet ().size ()) { | |
query += ','; | |
} | |
} | |
return query; | |
} | |
public virtual class InvalidPermissionsException extends Exception {} | |
public virtual class BadException extends Exception {} | |
public virtual class UnknownException extends Exception {} | |
public virtual class ConflictException extends Exception {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment