26
26
import org .apache .http .impl .client .CloseableHttpClient ;
27
27
import org .apache .http .impl .client .HttpClients ;
28
28
29
- // Hack to get DELETE to accept a request body
29
+
30
+ /**
31
+ * Hack to get DELETE to accept a request body.
32
+ */
30
33
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
31
34
public static final String METHOD_NAME = "DELETE" ;
32
35
@@ -41,6 +44,7 @@ public HttpDeleteWithBody(final String uri) {
41
44
}
42
45
}
43
46
47
+
44
48
/**
45
49
* Class Client allows for quick and easy access any REST or REST-like API.
46
50
*/
@@ -50,6 +54,7 @@ public class Client implements Closeable {
50
54
private Boolean test ;
51
55
private boolean createdHttpClient ;
52
56
57
+
53
58
/**
54
59
* Constructor for using the default CloseableHttpClient.
55
60
*/
@@ -59,6 +64,7 @@ public Client() {
59
64
this .createdHttpClient = true ;
60
65
}
61
66
67
+
62
68
/**
63
69
* Constructor for passing in an httpClient, typically for mocking. Passed-in httpClient will not be closed
64
70
* by this Client.
@@ -70,8 +76,9 @@ public Client(CloseableHttpClient httpClient) {
70
76
this (httpClient , false );
71
77
}
72
78
79
+
73
80
/**
74
- * Constructor for passing in a test parameter to allow for http calls
81
+ * Constructor for passing in a test parameter to allow for http calls.
75
82
*
76
83
* @param test
77
84
* is a Bool
@@ -80,8 +87,9 @@ public Client(Boolean test) {
80
87
this (HttpClients .createDefault (), test );
81
88
}
82
89
90
+
83
91
/**
84
- * Constructor for passing in a an httpClient and test parameter to allow for http calls
92
+ * Constructor for passing in an httpClient and test parameter to allow for http calls.
85
93
*
86
94
* @param httpClient
87
95
* an Apache CloseableHttpClient
@@ -104,6 +112,8 @@ public Client(CloseableHttpClient httpClient, Boolean test) {
104
112
* (e.g. "/your/endpoint/path")
105
113
* @param queryParams
106
114
* map of key, values representing the query parameters
115
+ * @throws URISyntaxException
116
+ * in of a URI syntax error
107
117
*/
108
118
public URI buildUri (String baseUri , String endpoint , Map <String , String > queryParams ) throws URISyntaxException {
109
119
URIBuilder builder = new URIBuilder ();
@@ -133,11 +143,15 @@ public URI buildUri(String baseUri, String endpoint, Map<String, String> queryPa
133
143
return uri ;
134
144
}
135
145
146
+
136
147
/**
137
148
* Prepare a Response object from an API call via Apache's HTTP client.
138
149
*
139
150
* @param response
140
151
* from a call to a CloseableHttpClient
152
+ * @throws IOException
153
+ * in case of a network error
154
+ * @return the response object
141
155
*/
142
156
public Response getResponse (CloseableHttpResponse response ) throws IOException {
143
157
ResponseHandler <String > handler = new SendGridResponseHandler ();
@@ -154,9 +168,18 @@ public Response getResponse(CloseableHttpResponse response) throws IOException {
154
168
return new Response (statusCode , responseBody , responseHeaders );
155
169
}
156
170
171
+
157
172
/**
158
173
* Make a GET request and provide the status code, response body and
159
174
* response headers.
175
+ *
176
+ * @param request
177
+ * the request object
178
+ * @throws URISyntaxException
179
+ * in case of a URI syntax error
180
+ * @throws IOException
181
+ * in case of a network error
182
+ * @return the response object
160
183
*/
161
184
public Response get (Request request ) throws URISyntaxException , IOException {
162
185
URI uri = null ;
@@ -177,9 +200,18 @@ public Response get(Request request) throws URISyntaxException, IOException {
177
200
return executeApiCall (httpGet );
178
201
}
179
202
203
+
180
204
/**
181
205
* Make a POST request and provide the status code, response body and
182
206
* response headers.
207
+ *
208
+ * @param request
209
+ * the request object
210
+ * @throws URISyntaxException
211
+ * in case of a URI syntax error
212
+ * @throws IOException
213
+ * in case of a network error
214
+ * @return the response object
183
215
*/
184
216
public Response post (Request request ) throws URISyntaxException , IOException {
185
217
URI uri = null ;
@@ -204,9 +236,18 @@ public Response post(Request request) throws URISyntaxException, IOException {
204
236
return executeApiCall (httpPost );
205
237
}
206
238
239
+
207
240
/**
208
241
* Make a PATCH request and provide the status code, response body and
209
242
* response headers.
243
+ *
244
+ * @param request
245
+ * the request object
246
+ * @throws URISyntaxException
247
+ * in case of a URI syntax error
248
+ * @throws IOException
249
+ * in case of a network error
250
+ * @return the response object
210
251
*/
211
252
public Response patch (Request request ) throws URISyntaxException , IOException {
212
253
URI uri = null ;
@@ -231,9 +272,18 @@ public Response patch(Request request) throws URISyntaxException, IOException {
231
272
return executeApiCall (httpPatch );
232
273
}
233
274
275
+
234
276
/**
235
277
* Make a PUT request and provide the status code, response body and
236
278
* response headers.
279
+ *
280
+ * @param request
281
+ * the request object
282
+ * @throws URISyntaxException
283
+ * in case of a URI syntax error
284
+ * @throws IOException
285
+ * in case of a network error
286
+ * @return the response object
237
287
*/
238
288
public Response put (Request request ) throws URISyntaxException , IOException {
239
289
URI uri = null ;
@@ -258,8 +308,17 @@ public Response put(Request request) throws URISyntaxException, IOException {
258
308
return executeApiCall (httpPut );
259
309
}
260
310
311
+
261
312
/**
262
313
* Make a DELETE request and provide the status code and response headers.
314
+ *
315
+ * @param request
316
+ * the request object
317
+ * @throws URISyntaxException
318
+ * in case of a URI syntax error
319
+ * @throws IOException
320
+ * in case of a network error
321
+ * @return the response object
263
322
*/
264
323
public Response delete (Request request ) throws URISyntaxException , IOException {
265
324
URI uri = null ;
@@ -290,6 +349,16 @@ private void writeContentTypeIfNeeded(Request request, HttpMessage httpMessage)
290
349
}
291
350
}
292
351
352
+
353
+ /**
354
+ * Makes a call to the client API.
355
+ *
356
+ * @param httpPost
357
+ * the request method object
358
+ * @throws IOException
359
+ * in case of a network error
360
+ * @return the response object
361
+ */
293
362
private Response executeApiCall (HttpRequestBase httpPost ) throws IOException {
294
363
try {
295
364
CloseableHttpResponse serverResponse = httpClient .execute (httpPost );
@@ -303,8 +372,15 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
303
372
}
304
373
}
305
374
375
+
306
376
/**
307
377
* A thin wrapper around the HTTP methods.
378
+ *
379
+ * @param request
380
+ * the request object
381
+ * @throws IOException
382
+ * in case of a network error
383
+ * @return the response object
308
384
*/
309
385
public Response api (Request request ) throws IOException {
310
386
try {
@@ -334,11 +410,25 @@ public Response api(Request request) throws IOException {
334
410
}
335
411
}
336
412
413
+
414
+ /**
415
+ * Closes the http client.
416
+ *
417
+ * @throws IOException
418
+ * in case of a network error
419
+ */
337
420
@ Override
338
421
public void close () throws IOException {
339
422
this .httpClient .close ();
340
423
}
341
424
425
+
426
+ /**
427
+ * Closes and finalizes the http client.
428
+ *
429
+ * @throws Throwable
430
+ * in case of an error
431
+ */
342
432
@ Override
343
433
public void finalize () throws Throwable {
344
434
try {
0 commit comments