Last active
March 28, 2023 02:40
-
-
Save Avinava/5300211ca6e3f4c06ec1d1bac24c29aa to your computer and use it in GitHub Desktop.
Generic http class that can be used in apex REST callouts
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
/** | |
* @description The HTTPUtil class provides a set of methods to perform HTTP requests to a given endpoint | |
* with specified headers, url parameters, and payload. It also allows encoding of url parameters, | |
* getting the response object, and getting the response headers | |
**/ | |
public with sharing class HTTPUtil { | |
public transient HttpResponse response; | |
private String currentEndpoint; | |
private String baseEndpoint; | |
private Map<String, String> headers; | |
public HTTPUtil(String baseEndpoint, Map<String, String> headers) { | |
this.baseEndpoint = baseEndpoint; | |
this.headers = headers; | |
if (String.isNotBlank(baseEndpoint) && !baseEndpoint.endsWith('/')) { | |
this.baseEndpoint += '/'; | |
} | |
} | |
public String post(String pathLet, Map<String, String> urlParams, String payload) { | |
return doRequest('POST', pathLet, headers, urlParams, payload); | |
} | |
public String get(String pathLet, Map<String, String> urlParams) { | |
return doRequest('GET', pathLet, headers, urlParams, null); | |
} | |
public String doRequest(String method, String pathLet, Map<String, String> headers, Map<String, String> urlParams, String payload) { | |
HttpRequest req = new HttpRequest(); | |
setHeaders(req, headers); | |
req.setMethod(method); | |
if (payload != null) { | |
req.setBody(payload); | |
} | |
req.setTimeout(2 * 60 * 1000); | |
req.setEndpoint(getEndpoint(pathLet, urlParams)); | |
Http h = new Http(); | |
this.currentEndpoint = req.getEndpoint(); | |
this.response = h.send(req); | |
System.debug('Header : ' + this.response.getHeaderKeys()); | |
System.debug('Body : ' + this.response.getBody()); | |
String body = this.response.getBody(); | |
return body; | |
} | |
public HttpResponse getResponse() { | |
return this.response; | |
} | |
public String getEndpoint() { | |
return this.currentEndpoint; | |
} | |
public static Map<String, String> encodeParams(Map<String, String> params) { | |
Map<String, String> encoded = new Map<String, String>(); | |
for (String k : params.keySet()) { | |
encoded.put(k, urlEncode(params.get(k))); | |
} | |
return encoded; | |
} | |
public static String urlEncode(String s) { | |
return EncodingUtil.urlEncode(s, 'utf-8').replace('+', '%20').replace('*', '%2A').replace('%7E', '~'); | |
} | |
private static void setHeaders(HttpRequest req, Map<String, String> headers) { | |
if (headers != null) | |
for (String k : headers.keySet()) { | |
req.setHeader(k, headers.get(k)); | |
} | |
} | |
public String getEndpoint(String endpoint, Map<String, String> urlParams) { | |
if (String.isNotBlank(this.baseEndpoint)) { | |
endpoint = this.baseEndpoint + (endpoint.startsWith('/') ? endpoint.substring(1) : endpoint); | |
} | |
Pagereference et = new Pagereference(endpoint); | |
if (urlParams != null && !urlParams.isEmpty()) { | |
et.getParameters().putAll(urlParams); | |
} | |
return et.getUrl(); | |
} | |
public Map<String, String> getResponseHeaders() { | |
Map<String, String> headers = new Map<string, string>(); | |
for (string s : this.response.getHeaderKeys()) { | |
headers.put(s, this.response.getHeader(s)); | |
} | |
return headers; | |
} | |
} |
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
@isTest | |
private class HTTPUtilTest { | |
@isTest | |
static void doGet() { | |
HTTPUtil http = new HTTPUtil('https://test-dev-ed.lightning.force.com/', null); | |
Test.startTest(); | |
Test.setMock(HttpCalloutMock.class, new GenericHTTPMock()); | |
http.get('/', new Map<String, String>{ 'test' => 'test' }); | |
Test.stopTest(); | |
http.getEndpoint(); | |
http.getResponse(); | |
http.getResponseHeaders(); | |
System.assert(HTTPUtil.encodeParams(new Map<String, String>{ 'test' => 'test' }) != null); | |
System.assert(http.getEndpoint().contains('https://test-dev-ed.lightning.force.com/')); | |
} | |
@isTest | |
static void doPost() { | |
HTTPUtil http = new HTTPUtil('https://test-dev-ed.lightning.force.com/', null); | |
Test.startTest(); | |
Test.setMock(HttpCalloutMock.class, new GenericHTTPMock()); | |
http.post('/', new Map<String, String>{ 'test' => 'test' }, 'test'); | |
Test.stopTest(); | |
} | |
private class GenericHTTPMock implements HttpCalloutMock { | |
public HttpResponse respond(HttpRequest request) { | |
HttpResponse res = new HttpResponse(); | |
res.setBody('{"text":"value"}'); | |
res.setStatusCode(200); | |
return res; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment