Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. /**
  2. *
  3. * Common HTTP Client Class for making Callouts
  4. *
  5. */
  6. public with sharing class INT_CommonHttpClient {
  7.  
  8. //Client request method calls
  9. public static final String REQUEST_METHOD_DELETE = 'DELETE';
  10. public static final String REQUEST_METHOD_GET = 'GET';
  11. public static final String REQUEST_METHOD_POST = 'POST';
  12. public static final String REQUEST_METHOD_PUT = 'PUT';
  13.  
  14. //Header statics
  15. public static final String HTTP_HEADER_AUTHORIZATION = 'Authorization';
  16. public static final String HTTP_HEADER_CONTENT_DISPOSITION = 'Content-Disposition';
  17. public static final String HTTP_HEADER_CONTENT_LENGTH = 'Content-Length';
  18. public static final String HTTP_HEADER_CONTENT_TYPE = 'Content-Type';
  19.  
  20. //Separator
  21. public static final String HTTP_REQUEST_ENDPOINT_SEPARATOR = '&';
  22. public static final String HTTP_REQUEST_PARAMETER_SEPARATOR = '=';
  23. public static final String HTTP_REQUEST_PARAMETER_PREFIX = '?';
  24.  
  25. //Charset
  26. public static final String CHARSET_UTF8 = 'UTF-8';
  27.  
  28. /**
  29. * AbstractClientRequest Interface
  30. */
  31. public interface IAbstractClientRequest
  32. {
  33. HttpRequest buildHttpRequest();
  34. HttpResponse executeUnparsed();
  35. }
  36.  
  37. public interface IHttpContent
  38. {
  39. Object getHttpContent();
  40. }
  41.  
  42. public abstract class AbstractClientRequest implements IAbstractClientRequest
  43. {
  44. protected String requestMethod {get;set;}
  45. protected String endpoint {get;set;}
  46. protected IHttpContent httpContent {get;set;}
  47.  
  48. private Map<String, String> m_params = new Map<String, String>();
  49.  
  50. protected AbstractClientRequest()
  51. {
  52. }
  53.  
  54. protected AbstractClientRequest(String endpoint,
  55. String requestMethod, IHttpContent httpContent)
  56. {
  57. this.requestMethod = requestMethod;
  58. this.endpoint = endpoint;
  59. this.httpContent = httpContent;
  60. }
  61.  
  62. public HttpRequest buildHttpRequest()
  63. {
  64. HttpRequest req = new HttpRequest();
  65. req.setEndpoint(createEndpoint());
  66. req.setTimeout(30000);
  67. req.setMethod(this.requestMethod);
  68. if (httpContent != null)
  69. {
  70. Object body = httpContent.getHttpContent();
  71. if (body instanceOf Blob)
  72. {
  73. Blob bodyBlob = (Blob)body;
  74. req.setBodyAsBlob(bodyBlob);
  75. req.setHeader(HTTP_HEADER_CONTENT_TYPE, INT_DocumentHttpClient.HTTP_CONTENT_TYPE);
  76. req.setHeader(HTTP_HEADER_CONTENT_LENGTH, String.valueOf(bodyBlob.size()));
  77. }
  78. else
  79. {
  80. String bodyStr = String.valueof(body);
  81. req.setBody(bodyStr);
  82. req.setHeader(HTTP_HEADER_CONTENT_TYPE, 'application/json');
  83. req.setHeader(HTTP_HEADER_CONTENT_LENGTH, String.valueOf(bodyStr.length()));
  84. }
  85. }
  86. return req;
  87. }
  88.  
  89. /**
  90. * Creates the endpoint taking into account the endpoint, urlParam and parameters provided.
  91. */
  92. private String createEndpoint()
  93. {
  94. String endpoint = this.endpoint;
  95.  
  96. Set<String> keys = m_params.keySet();
  97. if (keys.size() > 0)
  98. {
  99. endpoint += HTTP_REQUEST_PARAMETER_PREFIX;
  100.  
  101. for (String key : keys)
  102. {
  103. String param = (String)m_params.get(key);
  104. if (param != null)
  105. {
  106. endpoint += key;
  107. endpoint += HTTP_REQUEST_PARAMETER_SEPARATOR;
  108. endpoint += EncodingUtil.urlEncode(param, CHARSET_UTF8);
  109. endpoint += HTTP_REQUEST_ENDPOINT_SEPARATOR;
  110. }
  111. }
  112. }
  113.  
  114. if (endpoint.endsWith(HTTP_REQUEST_ENDPOINT_SEPARATOR))
  115. {
  116. endpoint = endpoint.substring(0, endpoint.length() - 1);
  117. }
  118. System.debug('endpoint ' + endpoint);
  119. return endpoint;
  120. }
  121.  
  122. public HttpResponse executeUnparsed()
  123. {
  124. System.debug('@@ start executeUnparsed @@');
  125. HttpRequest httpRequest = buildHttpRequest();
  126. System.debug('@@ httpRequest executeUnparsed @@ ' +httpRequest);
  127.  
  128.  
  129. HttpResponse httpResponse = new Http().send(httpRequest);
  130. System.debug('@@ httpResponse @@ ' +httpResponse);
  131. Integer statusCode = httpResponse.getStatusCode();
  132. String status = httpResponse.getStatus();
  133. return httpResponse;
  134. }
  135.  
  136. @TestVisible
  137. protected AbstractClientRequest addParam(String name, String param)
  138. {
  139. m_params.put(name, param);
  140. return this;
  141. }
  142.  
  143. @TestVisible
  144. protected String getParam(String name)
  145. {
  146. return m_params.get(name);
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement