39
39
public class Client {
40
40
41
41
private CloseableHttpClient httpClient ;
42
-
42
+
43
43
/**
44
44
* Constructor for using the default CloseableHttpClient.
45
45
*/
46
46
public Client () {
47
47
this .httpClient = HttpClients .createDefault ();
48
48
}
49
-
49
+
50
50
/**
51
51
* Constructor for passing in an httpClient for mocking.
52
52
*
@@ -55,7 +55,7 @@ public Client() {
55
55
public Client (CloseableHttpClient httpClient ) {
56
56
this .httpClient = httpClient ;
57
57
}
58
-
58
+
59
59
/**
60
60
* Add query parameters to a URL.
61
61
*
@@ -67,22 +67,22 @@ public URI buildUri(String baseUri, String endpoint, Map<String,String> queryPar
67
67
throws URISyntaxException {
68
68
URIBuilder builder = new URIBuilder ();
69
69
URI uri ;
70
-
70
+
71
71
builder .setScheme ("https" );
72
- builder .setHost (baseUri );
73
- builder .setPath (endpoint );
72
+ builder .setHost (baseUri );
73
+ builder .setPath (endpoint );
74
74
if (queryParams != null ) {
75
75
for (Map .Entry <String , String > entry : queryParams .entrySet ()) {
76
76
builder .setParameter (entry .getKey (), entry .getValue ());
77
- }
77
+ }
78
78
}
79
-
79
+
80
80
try {
81
81
uri = builder .build ();
82
82
} catch (URISyntaxException ex ) {
83
83
throw ex ;
84
84
}
85
-
85
+
86
86
return uri ;
87
87
}
88
88
@@ -94,24 +94,24 @@ public URI buildUri(String baseUri, String endpoint, Map<String,String> queryPar
94
94
public Response getResponse (CloseableHttpResponse response ) throws IOException {
95
95
ResponseHandler <String > handler = new BasicResponseHandler ();
96
96
String responseBody = "" ;
97
-
97
+
98
98
int statusCode = response .getStatusLine ().getStatusCode ();
99
-
99
+
100
100
try {
101
101
responseBody = handler .handleResponse (response );
102
102
} catch (IOException ex ) {
103
103
throw ex ;
104
104
}
105
-
105
+
106
106
Header [] headers = response .getAllHeaders ();
107
107
Map <String ,String > responseHeaders = new HashMap <String ,String >();
108
108
for (Header h :headers ) {
109
109
responseHeaders .put (h .getName (), h .getValue ());
110
110
}
111
-
111
+
112
112
return new Response (statusCode , responseBody , responseHeaders );
113
113
}
114
-
114
+
115
115
/**
116
116
* Make a GET request and provide the status code, response body and response headers.
117
117
*/
@@ -120,21 +120,21 @@ public Response get(Request request) throws URISyntaxException, IOException {
120
120
Response response = new Response ();
121
121
URI uri = null ;
122
122
HttpGet httpGet = null ;
123
-
123
+
124
124
try {
125
125
uri = buildUri (request .baseUri , request .endpoint , request .queryParams );
126
- httpGet = new HttpGet (uri .toString ());
126
+ httpGet = new HttpGet (uri .toString ());
127
127
} catch (URISyntaxException ex ) {
128
128
throw ex ;
129
129
}
130
-
131
- if (request .requestHeaders != null ) {
132
- for (Map .Entry <String , String > entry : request .requestHeaders .entrySet ()) {
130
+
131
+ if (request .headers != null ) {
132
+ for (Map .Entry <String , String > entry : request .headers .entrySet ()) {
133
133
httpGet .setHeader (entry .getKey (), entry .getValue ());
134
- }
134
+ }
135
135
}
136
-
137
-
136
+
137
+
138
138
try {
139
139
serverResponse = httpClient .execute (httpGet );
140
140
response = getResponse (serverResponse );
@@ -145,10 +145,10 @@ public Response get(Request request) throws URISyntaxException, IOException {
145
145
serverResponse .close ();
146
146
}
147
147
}
148
-
148
+
149
149
return response ;
150
150
}
151
-
151
+
152
152
/**
153
153
* Make a POST request and provide the status code, response body and response headers.
154
154
*/
@@ -157,26 +157,26 @@ public Response post(Request request) throws URISyntaxException, IOException {
157
157
Response response = new Response ();
158
158
URI uri = null ;
159
159
HttpPost httpPost = null ;
160
-
160
+
161
161
try {
162
162
uri = buildUri (request .baseUri , request .endpoint , request .queryParams );
163
- httpPost = new HttpPost (uri .toString ());
163
+ httpPost = new HttpPost (uri .toString ());
164
164
} catch (URISyntaxException ex ) {
165
165
throw ex ;
166
166
}
167
-
168
- if (request .requestHeaders != null ) {
169
- for (Map .Entry <String , String > entry : request .requestHeaders .entrySet ()) {
167
+
168
+ if (request .headers != null ) {
169
+ for (Map .Entry <String , String > entry : request .headers .entrySet ()) {
170
170
httpPost .setHeader (entry .getKey (), entry .getValue ());
171
- }
171
+ }
172
172
}
173
-
173
+
174
174
try {
175
- httpPost .setEntity (new StringEntity (request .requestBody ));
175
+ httpPost .setEntity (new StringEntity (request .body ));
176
176
} catch (IOException ex ) {
177
177
throw ex ;
178
178
}
179
-
179
+
180
180
try {
181
181
serverResponse = httpClient .execute (httpPost );
182
182
response = getResponse (serverResponse );
@@ -188,38 +188,38 @@ public Response post(Request request) throws URISyntaxException, IOException {
188
188
serverResponse .close ();
189
189
}
190
190
}
191
-
191
+
192
192
return response ;
193
193
}
194
194
195
195
/**
196
196
* Make a PATCH request and provide the status code, response body and response headers.
197
- */
197
+ */
198
198
public Response patch (Request request ) throws URISyntaxException , IOException {
199
199
CloseableHttpResponse serverResponse = null ;
200
200
Response response = new Response ();
201
201
URI uri = null ;
202
202
HttpPatch httpPatch = null ;
203
-
203
+
204
204
try {
205
205
uri = buildUri (request .baseUri , request .endpoint , request .queryParams );
206
- httpPatch = new HttpPatch (uri .toString ());
206
+ httpPatch = new HttpPatch (uri .toString ());
207
207
} catch (URISyntaxException ex ) {
208
208
throw ex ;
209
209
}
210
-
211
- if (request .requestHeaders != null ) {
212
- for (Map .Entry <String , String > entry : request .requestHeaders .entrySet ()) {
210
+
211
+ if (request .headers != null ) {
212
+ for (Map .Entry <String , String > entry : request .headers .entrySet ()) {
213
213
httpPatch .setHeader (entry .getKey (), entry .getValue ());
214
- }
214
+ }
215
215
}
216
-
216
+
217
217
try {
218
- httpPatch .setEntity (new StringEntity (request .requestBody ));
218
+ httpPatch .setEntity (new StringEntity (request .body ));
219
219
} catch (IOException ex ) {
220
220
throw ex ;
221
221
}
222
-
222
+
223
223
try {
224
224
serverResponse = httpClient .execute (httpPatch );
225
225
response = getResponse (serverResponse );
@@ -231,7 +231,7 @@ public Response patch(Request request) throws URISyntaxException, IOException {
231
231
serverResponse .close ();
232
232
}
233
233
}
234
-
234
+
235
235
return response ;
236
236
}
237
237
@@ -243,27 +243,27 @@ public Response put(Request request) throws URISyntaxException, IOException {
243
243
Response response = new Response ();
244
244
URI uri = null ;
245
245
HttpPut httpPut = null ;
246
-
246
+
247
247
try {
248
248
uri = buildUri (request .baseUri , request .endpoint , request .queryParams );
249
- httpPut = new HttpPut (uri .toString ());
249
+ httpPut = new HttpPut (uri .toString ());
250
250
} catch (URISyntaxException ex ) {
251
251
throw ex ;
252
252
}
253
-
254
- if (request .requestHeaders != null ) {
255
- for (Map .Entry <String , String > entry : request .requestHeaders .entrySet ()) {
253
+
254
+ if (request .headers != null ) {
255
+ for (Map .Entry <String , String > entry : request .headers .entrySet ()) {
256
256
httpPut .setHeader (entry .getKey (), entry .getValue ());
257
- }
257
+ }
258
258
}
259
-
260
-
259
+
260
+
261
261
try {
262
- httpPut .setEntity (new StringEntity (request .requestBody ));
262
+ httpPut .setEntity (new StringEntity (request .body ));
263
263
} catch (IOException ex ) {
264
264
throw ex ;
265
265
}
266
-
266
+
267
267
try {
268
268
serverResponse = httpClient .execute (httpPut );
269
269
response = getResponse (serverResponse );
@@ -275,7 +275,7 @@ public Response put(Request request) throws URISyntaxException, IOException {
275
275
serverResponse .close ();
276
276
}
277
277
}
278
-
278
+
279
279
return response ;
280
280
}
281
281
@@ -287,20 +287,20 @@ public Response delete(Request request) throws URISyntaxException, IOException {
287
287
Response response = new Response ();
288
288
URI uri = null ;
289
289
HttpDelete httpDelete = null ;
290
-
290
+
291
291
try {
292
292
uri = buildUri (request .baseUri , request .endpoint , request .queryParams );
293
- httpDelete = new HttpDelete (uri .toString ());
293
+ httpDelete = new HttpDelete (uri .toString ());
294
294
} catch (URISyntaxException ex ) {
295
295
throw ex ;
296
296
}
297
-
298
- if (request .requestHeaders != null ) {
299
- for (Map .Entry <String , String > entry : request .requestHeaders .entrySet ()) {
297
+
298
+ if (request .headers != null ) {
299
+ for (Map .Entry <String , String > entry : request .headers .entrySet ()) {
300
300
httpDelete .setHeader (entry .getKey (), entry .getValue ());
301
- }
301
+ }
302
302
}
303
-
303
+
304
304
try {
305
305
serverResponse = httpClient .execute (httpDelete );
306
306
response = getResponse (serverResponse );
@@ -312,10 +312,10 @@ public Response delete(Request request) throws URISyntaxException, IOException {
312
312
serverResponse .close ();
313
313
}
314
314
}
315
-
315
+
316
316
return response ;
317
317
}
318
-
318
+
319
319
/**
320
320
* A thin wrapper around the HTTP methods.
321
321
*/
@@ -325,15 +325,15 @@ public Response api(Request request) throws IOException {
325
325
throw new IOException ("We only support GET, PUT, PATCH, POST and DELETE." );
326
326
}
327
327
switch (request .method ) {
328
- case GET :
328
+ case GET :
329
329
return get (request );
330
- case POST :
330
+ case POST :
331
331
return post (request );
332
- case PUT :
332
+ case PUT :
333
333
return put (request );
334
334
case PATCH :
335
335
return patch (request );
336
- case DELETE :
336
+ case DELETE :
337
337
return delete (request );
338
338
default :
339
339
throw new IOException ("We only support GET, PUT, PATCH, POST and DELETE." );
0 commit comments