Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active December 7, 2021 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swapnilshrikhande/27266d15622f09185a90cb09c68e8557 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/27266d15622f09185a90cb09c68e8557 to your computer and use it in GitHub Desktop.
HttpConnector
public inherited sharing virtual class ApexExtension {
public static String str(String input){
return input == null ? '' : input;
}
public static String str(Object input){
return str(String.valueOf(input));
}
public static String str(String input, String defaultValue){
return String.isBlank(str(input)) ? defaultValue : input;
}
public static Object obj( Object element, Object defaultValue ){
return element == null ? defaultValue : element;
}
public static Object def( Object element, Object defaultValue ){
return element == null ? defaultValue : element;
}
public static Boolean bool(Boolean input,Boolean defaultValue ){
return (Boolean) obj(input,defaultValue);
}
public static Decimal dec(Decimal input,Decimal defaultValue ){
return (Decimal) obj(input,defaultValue);
}
public static Integer int(Object input,Integer defaultValue ){
return Integer.valueOf(obj(input,defaultValue));
}
public static List<String> lst(){
return new List<String>();
}
public static List<Object> lst( List<Object> elements ){
return (List<Object>) obj(elements, new List<Object>());
}
public static List<String> lst(Object lstObject){
return lstObject == null ? lst() : (List<String>) lstObject;
}
public static List<String> lst( List<String> elements ){
return (List<String>) obj(elements, lst());
}
public static List<String> lst(Set<String> elements){
return new List<String>(elements);
}
public static List<String> lst(String index0,String index1){
return new List<String>{index0,index1};
}
public static List<String> lst(String index0,String index1,String index2){
return new List<String>{index0,index1,index2};
}
public static Set<String> set(String index0,String index1){
return new Set<String>{index0,index1};
}
}
public inherited sharing class HttpConnector extends ApexExtension {
public static final String METHOD_POST = 'POST';
public static final String METHOD_GET = 'GET';
private Http http;
private HttpRequest request;
private HttpResponse response;
private Map<String,String> paramsMap;
private String endPointURL;
public HttpConnector() {
http = new Http();
request = new HttpRequest();
paramsMap = new Map<String,String>();
}
public HttpConnector setEndPoint(String endPoint){
this.endPointURL = endPoint;
return this;
}
public HttpConnector setMethod(String method){
this.request.setMethod(method);
return this;
}
public HttpConnector setHeader(String key,String value){
this.request.setHeader(key, value);
return this;
}
public HttpConnector setHeader(Map<String,String> header){
for(String key : header.keySet() ){
this.request.setHeader( key , header.get(key) );
}
return this;
}
public HttpConnector setParam(String key,String value) {
paramsMap.put(key, value);
return this;
}
public HttpConnector setBody(String body){
this.request.setBody(body);
return this;
}
public SyncResponse send(){
String errorMessage, statusCode;
SyncResponse syncResponse = new SyncResponse();
request.setEndpoint( getURL() );
this.response = http.send(request);
try{
String responseBody = str( this.response.getBody() ).trim();
Boolean isJSON = responseBody.startsWith('{');
Boolean isArray = responseBody.startsWith('[');
if( isArray ) {
syncResponse.resultList = (List<Object>) JSON.deserializeUntyped(responseBody);
} else if( isJSON ) {
syncResponse.result = (Map<String,Object>)JSON.deserializeUntyped(responseBody);
}
} catch(Exception exp){
syncResponse.error = exp.getMessage();
}
statusCode = ''+response.getStatusCode();
//not success ?
if( !statusCode.startsWith('2') ){
syncResponse.error = response.getStatus();
syncResponse.isSuccess = false;
} else {
syncResponse.error = '';
syncResponse.isSuccess = true;
}
syncResponse.statusCode = statusCode;
syncResponse.status = response.getStatus();
syncResponse.responseBody = response.getBody();
return syncResponse;
}
public HttpResponse getResponse(){
return this.response;
}
public HttpRequest getRequest(){
return this.request;
}
public String getURL(){
endPointURL = str( endPointURL );
Boolean isFirst=true;
String prefix,value;
for(String key : paramsMap.keySet()){
prefix = isFirst ? '?' : '&';
value = EncodingUtil.urlEncode(paramsMap.get(key), 'UTF-8');
endPointURL += ( prefix + key + '=' + value );
isFirst = false;
}
return endPointURL;
}
}
public inherited sharing class SyncResponse {
public SyncResponse() {
isSuccess = false;
}
public Map<String,Object> result {set;get;}
public List<Object> resultList {set;get;}
public Boolean isSuccess {set;get;}
public String error {set;get;}
public String statusCode {set;get;}
public String status {set;get;}
public String responseBody {set;get;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment