22
33/**
44 * The MIT License (MIT)
5- *
5+ *
66 * Copyright (C) 2017 hellosign.com
7- *
7+ *
88 * Permission is hereby granted, free of charge, to any person obtaining a copy
99 * of this software and associated documentation files (the "Software"), to deal
1010 * in the Software without restriction, including without limitation the rights
3333
3434/**
3535 * The MIT License (MIT)
36- *
36+ *
3737 * Copyright (C) 2016 hellosign.com
38- *
38+ *
3939 * Permission is hereby granted, free of charge, to any person obtaining a copy
4040 * of this software and associated documentation files (the "Software"), to deal
4141 * in the Software without restriction, including without limitation the rights
7373
7474/**
7575 * Abstracts HTTP requests.
76- *
76+ *
7777 * @author cpaul
7878 */
7979public class HttpClient {
@@ -125,7 +125,7 @@ public HttpClient withPostFields(Map<String, Serializable> fields) {
125125
126126 /**
127127 * Returns the response to the last HTTP request as a string.
128- *
128+ *
129129 * @return String or null if the request has not been created
130130 */
131131 public String getLastResponse () {
@@ -137,7 +137,7 @@ public String getLastResponse() {
137137
138138 /**
139139 * Returns the HTTP status code for the last request.
140- *
140+ *
141141 * @return Integer or null if the request has not been created
142142 */
143143 public Integer getLastResponseCode () {
@@ -149,7 +149,7 @@ public Integer getLastResponseCode() {
149149
150150 /**
151151 * Returns the last HTTP request body as a file.
152- *
152+ *
153153 * @param f File that should contain the response
154154 * @return long bytes written
155155 * @throws HelloSignException thrown if there is a problem writing to the
@@ -162,7 +162,7 @@ public long getLastResponseAsFile(File f) throws HelloSignException {
162162 /**
163163 * Inspects the JSONObject response for errors and throws an exception if
164164 * found.
165- *
165+ *
166166 * @param json JSONObject response
167167 * @param code HTTP response code
168168 * @throws HelloSignException thrown if an error is reported from the API
@@ -193,7 +193,7 @@ private void reset() {
193193
194194 /**
195195 * Executes the request and returns the response as a JSONObject.
196- *
196+ *
197197 * @return JSONObject response
198198 * @throws HelloSignException thrown if there is a problem executing the
199199 * request
@@ -204,18 +204,20 @@ public JSONObject asJson() throws HelloSignException {
204204 logger .debug ("Response body: " + response );
205205 try {
206206 json = new JSONObject (response );
207+ validate (json );
208+ } catch (HelloSignException e ) {
209+ throw e ;
207210 } catch (JSONException e ) {
208211 throw new HelloSignException (e );
209212 } finally {
210213 reset ();
211214 }
212- validate (json );
213215 return json ;
214216 }
215217
216218 /**
217219 * Executes the request and returns the response as a File.
218- *
220+ *
219221 * @param fileName String name of destination file
220222 * @return File response
221223 * @throws HelloSignException thrown if there is a problem executing the
@@ -242,7 +244,7 @@ public File asFile(String fileName) throws HelloSignException {
242244
243245 /**
244246 * Helper method to create a temporary file.
245- *
247+ *
246248 * @param filename String
247249 * @return File temporary file handle
248250 * @throws HelloSignException thrown if the file cannot be created
@@ -264,7 +266,7 @@ private File createTemporaryFile(String filename) throws HelloSignException {
264266
265267 /**
266268 * Executes the request and returns the HTTP response code.
267- *
269+ *
268270 * @return int HTTP response code
269271 * @throws HelloSignException thrown if no request has been performed
270272 */
@@ -282,7 +284,7 @@ public int asHttpCode() throws HelloSignException {
282284
283285 /**
284286 * Initializes a GET request to the given URL.
285- *
287+ *
286288 * @param url String url
287289 * @return HttpClient
288290 * @throws HelloSignException thrown if the url is invalid
@@ -298,7 +300,7 @@ public HttpClient get(String url) throws HelloSignException {
298300
299301 /**
300302 * Initializes a POST request to the given URL.
301- *
303+ *
302304 * @param url String url
303305 * @return HttpClient
304306 * @throws HelloSignException thrown if the url is invalid
@@ -314,7 +316,7 @@ public HttpClient post(String url) throws HelloSignException {
314316
315317 /**
316318 * Initializes a DELETE request to the given URL.
317- *
319+ *
318320 * @param url String url
319321 * @return HttpClient
320322 * @throws HelloSignException thrown if the url is invalid
@@ -327,7 +329,7 @@ public HttpClient delete(String url) throws HelloSignException {
327329
328330 /**
329331 * Makes a PUT request to the given URL
330- *
332+ *
331333 * @param url String url
332334 * @return HttpClient
333335 * @throws HelloSignException thrown if the url is invalid
@@ -340,7 +342,7 @@ public HttpClient put(String url) throws HelloSignException {
340342
341343 /**
342344 * Makes an OPTIONS request to the given URL
343- *
345+ *
344346 * @param url String URL
345347 * @return HttpClient
346348 * @throws HelloSignException thrown if the URL is invalid
0 commit comments