1616
1717/**
1818 * ApiClient is used to make HTTP requests to CoScale API client
19- *
19+ *
2020 * @author cristi
2121 *
2222 */
@@ -75,7 +75,7 @@ public ApiClient(String appId, Credentials credentials) {
7575
7676 /**
7777 * ApiClient Constructor.
78- *
78+ *
7979 * @param appId
8080 * The CoScale Application id.
8181 * @param credentials
@@ -119,7 +119,7 @@ public String getBaseURL() {
119119
120120 /**
121121 * setBaseURL is used to set a new API base URL. Is for Testing purpose.
122- *
122+ *
123123 * @param url
124124 */
125125 public void setBaseURL (String url ) {
@@ -128,7 +128,7 @@ public void setBaseURL(String url) {
128128
129129 /**
130130 * return the default connection timeout in ms.
131- *
131+ *
132132 * @return
133133 */
134134 public int getConnectionTimeout () {
@@ -137,7 +137,7 @@ public int getConnectionTimeout() {
137137
138138 /**
139139 * Change the default connection timeout.
140- *
140+ *
141141 * @param timeout
142142 * in ms.
143143 */
@@ -147,7 +147,7 @@ public void setConnectionTimeout(int timeout) {
147147
148148 /**
149149 * return the default read timeout in ms.
150- *
150+ *
151151 * @return
152152 */
153153 public int getReadTimeout () {
@@ -156,7 +156,7 @@ public int getReadTimeout() {
156156
157157 /**
158158 * Change the default read timeout.
159- *
159+ *
160160 * @param timeout
161161 * in ms.
162162 */
@@ -166,7 +166,7 @@ public void setReadTimeout(int timeout) {
166166
167167 /**
168168 * Get the SOURCE String which mark the created resources.
169- *
169+ *
170170 * @return
171171 */
172172 public static String getSource () {
@@ -175,7 +175,7 @@ public static String getSource() {
175175
176176 /**
177177 * Set the Source String which mark the created resources.
178- *
178+ *
179179 * @param source
180180 */
181181 public void setSource (String source ) {
@@ -185,7 +185,7 @@ public void setSource(String source) {
185185 /**
186186 * getDeserializationExceptions is used to get the number of JSON
187187 * deserialization errors.
188- *
188+ *
189189 * @return integer representing the number of JSON deserialization errors.
190190 */
191191 public int getDeserializationExceptions () {
@@ -281,7 +281,7 @@ private String doHttpRequest(String method, String uri, String payload, boolean
281281 /**
282282 * login will get the token used to authenticate the requests on CoScale
283283 * API.
284- *
284+ *
285285 * @throws IOException
286286 */
287287 private void login () throws IOException {
@@ -297,11 +297,11 @@ private void login() throws IOException {
297297 /**
298298 * callWithAuth is used to make requests that require Authentication on
299299 * CoScale API.
300- *
300+ *
301301 * @param method
302302 * request HTTP method.
303- * @param url
304- * for the request.
303+ * @param endpoint
304+ * The url for the request.
305305 * @param obj
306306 * object with data for the request. This parameter can be null.
307307 * @param valueType
@@ -319,7 +319,7 @@ public <T> T callWithAuth(String method, String endpoint, Object obj, TypeRefere
319319 // Do the actual request.
320320 int tries = 0 ;
321321 int responseCode = 0 ;
322- while ( tries ++ <= AUTH_RETRIES ) {
322+ do {
323323 if (this .token == null ) {
324324 login ();
325325 }
@@ -332,8 +332,10 @@ public <T> T callWithAuth(String method, String endpoint, Object obj, TypeRefere
332332 throw e ;
333333 }
334334 }
335- }
336- throw new CoscaleApiException (responseCode , "Failed to get authentication right" );
335+
336+ tries ++;
337+ } while (tries <= AUTH_RETRIES );
338+ throw new CoscaleApiException (responseCode , "Authentication failed." );
337339 }
338340
339341 /**
@@ -371,7 +373,7 @@ public <T> T call(String method, String url, Object obj, TypeReference<T> valueT
371373 * getAppRequestURL will construct the URL for a request using the end point
372374 * provided. This method will be used to construct the URL for a specific
373375 * application since the resulted URL will contain the application Id.
374- *
376+ *
375377 * @param endpoint
376378 * String representing the API end point of the request.
377379 * @return String containing the URL.
@@ -384,7 +386,7 @@ public String getAppRequestURL(String endpoint) {
384386 * getGlobalRequestURL will construct the URL for a request using the end
385387 * point provided. This will generate a URL for requests that doesn't
386388 * contain the application Id. Should be used for example in login process.
387- *
389+ *
388390 * @param endpoint
389391 * String representing the API end point of the request.
390392 * @return String containing the URL.
@@ -421,7 +423,12 @@ public String getRequesUrl(String endpoint, Options options, boolean globalApi)
421423 }
422424 sb .append (endpoint );
423425
424- if (options != null ) {
426+ if (options != null && options .hasQuery ()) {
427+ // No ? yet in the string, then add one.
428+ if (sb .indexOf ("?" ) != -1 ) {
429+ sb .append ('?' );
430+ }
431+
425432 sb .append (options .query ());
426433 }
427434
@@ -440,11 +447,21 @@ private static String convertStreamToString(java.io.InputStream is) {
440447 if (is == null ) {
441448 return "" ;
442449 }
443- try {
444- return new java .util .Scanner (is ).useDelimiter ("\\ A" ).next ();
450+
451+ String result = null ;
452+ // Try-with-resources closes any object that implements the Closeable interface.
453+ try (java .util .Scanner scanner = new java .util .Scanner (is ).useDelimiter ("\\ A" )) {
454+ result = scanner .next ();
445455 } catch (java .util .NoSuchElementException e ) {
446456 return "" ;
447457 }
458+
459+ /**
460+ * The useDelimiter method returns the very same instance that was used for the call.
461+ * While the compiler will warn that there is a possible memory leak, that can only
462+ * happen if it would return an other instance, leaving the original hanging.
463+ */
464+ return result ;
448465 }
449466
450467 /**
0 commit comments