Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Common HTTP Client Class for making Callouts
- *
- */
- public with sharing class INT_CommonHttpClient {
- //Client request method calls
- public static final String REQUEST_METHOD_DELETE = 'DELETE';
- public static final String REQUEST_METHOD_GET = 'GET';
- public static final String REQUEST_METHOD_POST = 'POST';
- public static final String REQUEST_METHOD_PUT = 'PUT';
- //Header statics
- public static final String HTTP_HEADER_AUTHORIZATION = 'Authorization';
- public static final String HTTP_HEADER_CONTENT_DISPOSITION = 'Content-Disposition';
- public static final String HTTP_HEADER_CONTENT_LENGTH = 'Content-Length';
- public static final String HTTP_HEADER_CONTENT_TYPE = 'Content-Type';
- //Separator
- public static final String HTTP_REQUEST_ENDPOINT_SEPARATOR = '&';
- public static final String HTTP_REQUEST_PARAMETER_SEPARATOR = '=';
- public static final String HTTP_REQUEST_PARAMETER_PREFIX = '?';
- //Charset
- public static final String CHARSET_UTF8 = 'UTF-8';
- /**
- * AbstractClientRequest Interface
- */
- public interface IAbstractClientRequest
- {
- HttpRequest buildHttpRequest();
- HttpResponse executeUnparsed();
- }
- public interface IHttpContent
- {
- Object getHttpContent();
- }
- public abstract class AbstractClientRequest implements IAbstractClientRequest
- {
- protected String requestMethod {get;set;}
- protected String endpoint {get;set;}
- protected IHttpContent httpContent {get;set;}
- private Map<String, String> m_params = new Map<String, String>();
- protected AbstractClientRequest()
- {
- }
- protected AbstractClientRequest(String endpoint,
- String requestMethod, IHttpContent httpContent)
- {
- this.requestMethod = requestMethod;
- this.endpoint = endpoint;
- this.httpContent = httpContent;
- }
- public HttpRequest buildHttpRequest()
- {
- HttpRequest req = new HttpRequest();
- req.setEndpoint(createEndpoint());
- req.setTimeout(30000);
- req.setMethod(this.requestMethod);
- if (httpContent != null)
- {
- Object body = httpContent.getHttpContent();
- if (body instanceOf Blob)
- {
- Blob bodyBlob = (Blob)body;
- req.setBodyAsBlob(bodyBlob);
- req.setHeader(HTTP_HEADER_CONTENT_TYPE, INT_DocumentHttpClient.HTTP_CONTENT_TYPE);
- req.setHeader(HTTP_HEADER_CONTENT_LENGTH, String.valueOf(bodyBlob.size()));
- }
- else
- {
- String bodyStr = String.valueof(body);
- req.setBody(bodyStr);
- req.setHeader(HTTP_HEADER_CONTENT_TYPE, 'application/json');
- req.setHeader(HTTP_HEADER_CONTENT_LENGTH, String.valueOf(bodyStr.length()));
- }
- }
- return req;
- }
- /**
- * Creates the endpoint taking into account the endpoint, urlParam and parameters provided.
- */
- private String createEndpoint()
- {
- String endpoint = this.endpoint;
- Set<String> keys = m_params.keySet();
- if (keys.size() > 0)
- {
- endpoint += HTTP_REQUEST_PARAMETER_PREFIX;
- for (String key : keys)
- {
- String param = (String)m_params.get(key);
- if (param != null)
- {
- endpoint += key;
- endpoint += HTTP_REQUEST_PARAMETER_SEPARATOR;
- endpoint += EncodingUtil.urlEncode(param, CHARSET_UTF8);
- endpoint += HTTP_REQUEST_ENDPOINT_SEPARATOR;
- }
- }
- }
- if (endpoint.endsWith(HTTP_REQUEST_ENDPOINT_SEPARATOR))
- {
- endpoint = endpoint.substring(0, endpoint.length() - 1);
- }
- System.debug('endpoint ' + endpoint);
- return endpoint;
- }
- public HttpResponse executeUnparsed()
- {
- System.debug('@@ start executeUnparsed @@');
- HttpRequest httpRequest = buildHttpRequest();
- System.debug('@@ httpRequest executeUnparsed @@ ' +httpRequest);
- HttpResponse httpResponse = new Http().send(httpRequest);
- System.debug('@@ httpResponse @@ ' +httpResponse);
- Integer statusCode = httpResponse.getStatusCode();
- String status = httpResponse.getStatus();
- return httpResponse;
- }
- @TestVisible
- protected AbstractClientRequest addParam(String name, String param)
- {
- m_params.put(name, param);
- return this;
- }
- @TestVisible
- protected String getParam(String name)
- {
- return m_params.get(name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement