Skip to content

Commit 0af1237

Browse files
committed
Refactoring API Call and error Handling
1 parent d6da8ee commit 0af1237

File tree

2 files changed

+19
-70
lines changed

2 files changed

+19
-70
lines changed

src/main/java/com/sendgrid/Client.java

+19-69
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
import java.util.Map;
1111

1212
import org.apache.http.Header;
13+
import org.apache.http.StatusLine;
1314
import org.apache.http.annotation.NotThreadSafe;
14-
import org.apache.http.client.ClientProtocolException;
1515
import org.apache.http.client.ResponseHandler;
1616
import org.apache.http.client.methods.CloseableHttpResponse;
1717
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
1818
import org.apache.http.client.methods.HttpGet;
1919
import org.apache.http.client.methods.HttpPatch;
2020
import org.apache.http.client.methods.HttpPost;
2121
import org.apache.http.client.methods.HttpPut;
22+
import org.apache.http.client.methods.HttpRequestBase;
2223
import org.apache.http.client.utils.URIBuilder;
2324
import org.apache.http.entity.StringEntity;
2425
import org.apache.http.impl.client.CloseableHttpClient;
@@ -143,8 +144,6 @@ public Response getResponse(CloseableHttpResponse response) throws IOException {
143144
* response headers.
144145
*/
145146
public Response get(Request request) throws URISyntaxException, IOException {
146-
CloseableHttpResponse serverResponse = null;
147-
Response response = new Response();
148147
URI uri = null;
149148
HttpGet httpGet = null;
150149

@@ -160,28 +159,14 @@ public Response get(Request request) throws URISyntaxException, IOException {
160159
httpGet.setHeader(entry.getKey(), entry.getValue());
161160
}
162161
}
163-
164-
try {
165-
serverResponse = httpClient.execute(httpGet);
166-
response = getResponse(serverResponse);
167-
} catch (IOException ex) {
168-
throw ex;
169-
} finally {
170-
if (serverResponse != null) {
171-
serverResponse.close();
172-
}
173-
}
174-
175-
return response;
162+
return executeApiCall(httpGet);
176163
}
177164

178165
/**
179166
* Make a POST request and provide the status code, response body and
180167
* response headers.
181168
*/
182169
public Response post(Request request) throws URISyntaxException, IOException {
183-
CloseableHttpResponse serverResponse = null;
184-
Response response = new Response();
185170
URI uri = null;
186171
HttpPost httpPost = null;
187172

@@ -203,28 +188,14 @@ public Response post(Request request) throws URISyntaxException, IOException {
203188
httpPost.setHeader("Content-Type", "application/json");
204189
}
205190

206-
try {
207-
serverResponse = httpClient.execute(httpPost);
208-
response = getResponse(serverResponse);
209-
serverResponse.close();
210-
} catch (IOException ex) {
211-
throw ex;
212-
} finally {
213-
if (serverResponse != null) {
214-
serverResponse.close();
215-
}
216-
}
217-
218-
return response;
191+
return executeApiCall(httpPost);
219192
}
220193

221194
/**
222195
* Make a PATCH request and provide the status code, response body and
223196
* response headers.
224197
*/
225198
public Response patch(Request request) throws URISyntaxException, IOException {
226-
CloseableHttpResponse serverResponse = null;
227-
Response response = new Response();
228199
URI uri = null;
229200
HttpPatch httpPatch = null;
230201

@@ -245,29 +216,14 @@ public Response patch(Request request) throws URISyntaxException, IOException {
245216
if (request.body != "") {
246217
httpPatch.setHeader("Content-Type", "application/json");
247218
}
248-
249-
try {
250-
serverResponse = httpClient.execute(httpPatch);
251-
response = getResponse(serverResponse);
252-
serverResponse.close();
253-
} catch (IOException ex) {
254-
throw ex;
255-
} finally {
256-
if (serverResponse != null) {
257-
serverResponse.close();
258-
}
259-
}
260-
261-
return response;
219+
return executeApiCall(httpPatch);
262220
}
263221

264222
/**
265223
* Make a PUT request and provide the status code, response body and
266224
* response headers.
267225
*/
268226
public Response put(Request request) throws URISyntaxException, IOException {
269-
CloseableHttpResponse serverResponse = null;
270-
Response response = new Response();
271227
URI uri = null;
272228
HttpPut httpPut = null;
273229

@@ -289,27 +245,13 @@ public Response put(Request request) throws URISyntaxException, IOException {
289245
httpPut.setHeader("Content-Type", "application/json");
290246
}
291247

292-
try {
293-
serverResponse = httpClient.execute(httpPut);
294-
response = getResponse(serverResponse);
295-
serverResponse.close();
296-
} catch (IOException ex) {
297-
throw ex;
298-
} finally {
299-
if (serverResponse != null) {
300-
serverResponse.close();
301-
}
302-
}
303-
304-
return response;
248+
return executeApiCall(httpPut);
305249
}
306250

307251
/**
308252
* Make a DELETE request and provide the status code and response headers.
309253
*/
310254
public Response delete(Request request) throws URISyntaxException, IOException {
311-
CloseableHttpResponse serverResponse = null;
312-
Response response = new Response();
313255
URI uri = null;
314256
HttpDeleteWithBody httpDelete = null;
315257

@@ -331,18 +273,26 @@ public Response delete(Request request) throws URISyntaxException, IOException {
331273
httpDelete.setHeader("Content-Type", "application/json");
332274
}
333275

276+
return executeApiCall(httpDelete);
277+
}
278+
279+
private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
280+
CloseableHttpResponse serverResponse = null;
281+
Response response = new Response();
334282
try {
335-
serverResponse = httpClient.execute(httpDelete);
283+
serverResponse = httpClient.execute(httpPost);
336284
response = getResponse(serverResponse);
337-
serverResponse.close();
338-
} catch (IOException ex) {
339-
throw ex;
285+
final StatusLine statusLine = serverResponse.getStatusLine();
286+
if(statusLine.getStatusCode()>=300){
287+
//throwing IOException here to not break API behavior.
288+
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.body:null));
289+
}
290+
340291
} finally {
341292
if (serverResponse != null) {
342293
serverResponse.close();
343294
}
344295
}
345-
346296
return response;
347297
}
348298

src/main/java/com/sendgrid/Response.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.sendgrid;
22

3-
import java.util.List;
43
import java.util.Map;
54

65
/**

0 commit comments

Comments
 (0)