2626import org .apache .http .impl .client .CloseableHttpClient ;
2727import org .apache .http .impl .client .HttpClients ;
2828
29- // Hack to get DELETE to accept a request body
29+
30+ /**
31+ * Hack to get DELETE to accept a request body.
32+ */
3033class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
3134 public static final String METHOD_NAME = "DELETE" ;
3235
@@ -41,6 +44,7 @@ public HttpDeleteWithBody(final String uri) {
4144 }
4245}
4346
47+
4448/**
4549 * Class Client allows for quick and easy access any REST or REST-like API.
4650 */
@@ -50,6 +54,7 @@ public class Client implements Closeable {
5054 private Boolean test ;
5155 private boolean createdHttpClient ;
5256
57+
5358 /**
5459 * Constructor for using the default CloseableHttpClient.
5560 */
@@ -59,6 +64,7 @@ public Client() {
5964 this .createdHttpClient = true ;
6065 }
6166
67+
6268 /**
6369 * Constructor for passing in an httpClient, typically for mocking. Passed-in httpClient will not be closed
6470 * by this Client.
@@ -70,8 +76,9 @@ public Client(CloseableHttpClient httpClient) {
7076 this (httpClient , false );
7177 }
7278
79+
7380 /**
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.
7582 *
7683 * @param test
7784 * is a Bool
@@ -80,8 +87,9 @@ public Client(Boolean test) {
8087 this (HttpClients .createDefault (), test );
8188 }
8289
90+
8391 /**
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.
8593 *
8694 * @param httpClient
8795 * an Apache CloseableHttpClient
@@ -104,6 +112,8 @@ public Client(CloseableHttpClient httpClient, Boolean test) {
104112 * (e.g. "/your/endpoint/path")
105113 * @param queryParams
106114 * map of key, values representing the query parameters
115+ * @throws URISyntaxException
116+ * in of a URI syntax error
107117 */
108118 public URI buildUri (String baseUri , String endpoint , Map <String , String > queryParams ) throws URISyntaxException {
109119 URIBuilder builder = new URIBuilder ();
@@ -133,11 +143,15 @@ public URI buildUri(String baseUri, String endpoint, Map<String, String> queryPa
133143 return uri ;
134144 }
135145
146+
136147 /**
137148 * Prepare a Response object from an API call via Apache's HTTP client.
138149 *
139150 * @param response
140151 * from a call to a CloseableHttpClient
152+ * @throws IOException
153+ * in case of a network error
154+ * @return the response object
141155 */
142156 public Response getResponse (CloseableHttpResponse response ) throws IOException {
143157 ResponseHandler <String > handler = new SendGridResponseHandler ();
@@ -154,9 +168,18 @@ public Response getResponse(CloseableHttpResponse response) throws IOException {
154168 return new Response (statusCode , responseBody , responseHeaders );
155169 }
156170
171+
157172 /**
158173 * Make a GET request and provide the status code, response body and
159174 * 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
160183 */
161184 public Response get (Request request ) throws URISyntaxException , IOException {
162185 URI uri = null ;
@@ -177,9 +200,18 @@ public Response get(Request request) throws URISyntaxException, IOException {
177200 return executeApiCall (httpGet );
178201 }
179202
203+
180204 /**
181205 * Make a POST request and provide the status code, response body and
182206 * 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
183215 */
184216 public Response post (Request request ) throws URISyntaxException , IOException {
185217 URI uri = null ;
@@ -204,9 +236,18 @@ public Response post(Request request) throws URISyntaxException, IOException {
204236 return executeApiCall (httpPost );
205237 }
206238
239+
207240 /**
208241 * Make a PATCH request and provide the status code, response body and
209242 * 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
210251 */
211252 public Response patch (Request request ) throws URISyntaxException , IOException {
212253 URI uri = null ;
@@ -231,9 +272,18 @@ public Response patch(Request request) throws URISyntaxException, IOException {
231272 return executeApiCall (httpPatch );
232273 }
233274
275+
234276 /**
235277 * Make a PUT request and provide the status code, response body and
236278 * 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
237287 */
238288 public Response put (Request request ) throws URISyntaxException , IOException {
239289 URI uri = null ;
@@ -258,8 +308,17 @@ public Response put(Request request) throws URISyntaxException, IOException {
258308 return executeApiCall (httpPut );
259309 }
260310
311+
261312 /**
262313 * 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
263322 */
264323 public Response delete (Request request ) throws URISyntaxException , IOException {
265324 URI uri = null ;
@@ -290,6 +349,16 @@ private void writeContentTypeIfNeeded(Request request, HttpMessage httpMessage)
290349 }
291350 }
292351
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+ */
293362 private Response executeApiCall (HttpRequestBase httpPost ) throws IOException {
294363 try {
295364 CloseableHttpResponse serverResponse = httpClient .execute (httpPost );
@@ -303,8 +372,15 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
303372 }
304373 }
305374
375+
306376 /**
307377 * 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
308384 */
309385 public Response api (Request request ) throws IOException {
310386 try {
@@ -334,11 +410,25 @@ public Response api(Request request) throws IOException {
334410 }
335411 }
336412
413+
414+ /**
415+ * Closes the http client.
416+ *
417+ * @throws IOException
418+ * in case of a network error
419+ */
337420 @ Override
338421 public void close () throws IOException {
339422 this .httpClient .close ();
340423 }
341424
425+
426+ /**
427+ * Closes and finalizes the http client.
428+ *
429+ * @throws Throwable
430+ * in case of an error
431+ */
342432 @ Override
343433 public void finalize () throws Throwable {
344434 try {
0 commit comments