Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
YourHttpService
YourHttpService is an example of common HTTP utilities that might be required while
building a partner package. All user gateway API classes will be different based on your
needs. However, we've provided a template that you can use to help create your
own.
| Available in: All Salesforce Billing Editions |
1public with sharing class YourHttpService {
2 // ============================================================================
3 // CONSTANT
4 // ============================================================================
5
6 // ============================================================================
7 // STATIC VARIABLES
8 // ============================================================================
9
10 private static YourHttpService httpServiceInstance;
11
12 // ============================================================================
13 // VARIABLES
14 // ============================================================================
15
16 private HttpResponse httpResponse;
17 private Map<String,String> mapOfHeaderParameter = new Map<String,String>();
18 private enum Method {GET, POST}
19
20 /**
21 * @name getInstance
22 * @description get an Instance of Service class
23 * @params NA
24 * @return Http Service Class Instance
25 * @remark
26 * @change
27 */
28 public static YourHttpService getInstance()
29 {
30 if (NULL == httpServiceInstance)
31 {
32 httpServiceInstance = new YourHttpService();
33 }
34 return httpServiceInstance;
35 }
36
37 /**
38 * @name get
39 * @description Get Method to get a HTTP request
40 * @param endPoint
41 * @return
42 * @remark
43 * @change
44 */
45 public void get(String endPoint)
46 {
47 send(newRequest(Method.GET, endPoint));
48 }
49
50 /**
51 * @name post
52 * @description Post Method to Post a HTTP request
53 * @param endPoint and requestBody
54 * @return
55 * @exception
56 * @remark
57 * @change
58 */
59 public void post(String endPoint, String requestBody)
60 {
61 send(newRequest(Method.POST, endPoint, requestBody));
62 }
63
64 /**
65 * @name addHeader
66 * @description addHeader Methods to add all the defualt Header's required fo rthe request
67 * @param name and value
68 * @return
69 * @exception
70 * @remark
71 * @change
72 */
73 public void addHeader(String name, String value)
74 {
75 mapOfHeaderParameter.put(name, value);
76 }
77
78 /**
79 * @name setHeader
80 * @description setHeader Methods to set setHeader for the request
81 * @param request
82 * @return
83 */
84 private void setHeader(HttpRequest request)
85 {
86 for(String headerValue : mapOfHeaderParameter.keySet())
87 {
88 request.setHeader(headerValue, mapOfHeaderParameter.get(headerValue));
89 }
90 }
91
92 /**
93 * @name setAuthorizationHeader
94 * @description setAuthorizationHeader Methods to set Authorization Header for the request
95 * @param userName and password
96 * @return
97 */
98 public void setAuthorizationHeader(String userName,String password)
99 {
100 if(String.isNotBlank(userName) && String.isNotBlank(password))
101 {
102 Blob headerValue = blob.valueOf(userName + ':' + password);
103 String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
104 addHeader('Authorization', authorizationHeader);
105 }
106 }
107
108 /**
109 * @name setTokenisationHeader
110 * @description setTokenisationHeader Methods to set Tokenisation Header for the request
111 * @param userName and password
112 * @return
113 */
114 public void setTokenisationHeader(String userName,String password)
115 {
116 if(String.isNotBlank(userName) && String.isNotBlank(password))
117 {
118 Blob headerValue = blob.valueOf(userName + ':' + password);
119 String tokenisationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
120 addHeader('Tokenisation', tokenisationHeader);
121 }
122 }
123
124 /**
125 * @name newRequest
126 * @description newRequest Methods to make a new request
127 * @param method and endPoint
128 * @return newRequest
129 * @exception
130 */
131 private HttpRequest newRequest(Method method, String endPoint)
132 {
133 return newRequest(method, endPoint, NULL);
134 }
135
136 /**
137 * @name newRequest
138 * @description newRequest Methods to make a new request
139 * @param method, endPoint and requestBody
140 * @return request
141 * @exception
142 */
143 private HttpRequest newRequest(Method method, String endPoint, String requestBody)
144 {
145 HttpRequest request = new HttpRequest();
146 request.setMethod(Method.name());
147 setHeader(request);
148 request.setEndpoint(endPoint);
149 if (String.isNotBlank(requestBody))
150 {
151 request.setBody(requestBody);
152 }
153 request.setTimeout(120000);
154 return request;
155 }
156
157 /**
158 * @name send
159 * @description send Methods to send a request
160 * @param request
161 * @return
162 * @exception Throws Exception
163 */
164 private void send(HttpRequest request)
165 {
166 try
167 {
168 httpResponse = new Http().send(request);
169 }
170 catch(Exception e)
171 {
172 throw e;
173 }
174 }
175
176 /**
177 * @name getResponse
178 * @description getResponse Method to get the Response
179 * @param NA
180 * @return httpResponse
181 * @exception
182 */
183 public HttpResponse getResponse()
184 {
185 return httpResponse;
186 }
187
188 /**
189 * @name getResponseToString
190 * @description getResponse Method to get the Response
191 * @param NA
192 * @return getResponse
193 * @exception
194 */
195 public String getResponseToString()
196 {
197 return getResponse().toString();
198 }
199
200}