Skip to content

Commit 52507e8

Browse files
Merge pull request #1 from CoScale/cleanup
Cleanup
2 parents bfe10a8 + d2c8edb commit 52507e8

File tree

9 files changed

+107
-87
lines changed

9 files changed

+107
-87
lines changed

src/main/java/com/coscale/sdk/client/ApiClient.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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
/**

src/main/java/com/coscale/sdk/client/ApiFactory.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.coscale.sdk.client;
22

3-
import com.coscale.sdk.client.ApiClient;
4-
import com.coscale.sdk.client.Credentials;
53
import com.coscale.sdk.client.data.DataApi;
64
import com.coscale.sdk.client.events.EventsApi;
75
import com.coscale.sdk.client.metrics.MetricsApi;
86
import com.coscale.sdk.client.servers.ServersApi;
97

108
/**
119
* ApiFactory is used to instantiate CoScale API Clients.
12-
*
10+
*
1311
* @author cristi
1412
*
1513
*/
@@ -20,7 +18,7 @@ public class ApiFactory {
2018

2119
/**
2220
* ApiFactory Constructor.
23-
*
21+
*
2422
* @param appId
2523
* the CoScale application id.
2624
* @param credentials
@@ -32,7 +30,7 @@ public ApiFactory(String appId, Credentials credentials) {
3230

3331
/**
3432
* Get the Api Client.
35-
*
33+
*
3634
* @return ApiClient.
3735
*/
3836
public ApiClient getApiClient() {
@@ -41,7 +39,7 @@ public ApiClient getApiClient() {
4139

4240
/**
4341
* Get a instance of MetricsApi.
44-
*
42+
*
4543
* @return MetricsApi.
4644
*/
4745
public MetricsApi getMetricsApi() {
@@ -50,7 +48,7 @@ public MetricsApi getMetricsApi() {
5048

5149
/**
5250
* Get a instance of EventsApi.
53-
*
51+
*
5452
* @return EventsApi.
5553
*/
5654
public EventsApi getEventsApi() {
@@ -59,7 +57,7 @@ public EventsApi getEventsApi() {
5957

6058
/**
6159
* Get a instance of ServersApi.
62-
*
60+
*
6361
* @return ServersApi.
6462
*/
6563
public ServersApi getServersApi() {
@@ -68,7 +66,7 @@ public ServersApi getServersApi() {
6866

6967
/**
7068
* Get a instance of DataApi.
71-
*
69+
*
7270
* @return DataApi.
7371
*/
7472
public DataApi getDataApi() {

src/main/java/com/coscale/sdk/client/Credentials.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
/**
44
* Credentials object will be used to login on CoScale API. There are 2 ways to
55
* authenticate on CoScale API:
6-
*
6+
*
77
* 1. Authenticating with an access token;
8-
*
8+
*
99
* 2. Authenticating with your email and password.
10-
*
10+
*
1111
* @author cristi
1212
*
1313
*/
1414
public class Credentials {
1515

1616
/**
1717
* Token is used for Authenticating with an access token.
18-
*
18+
*
1919
* @param accessToken
2020
* obtained from CoScale user account.
2121
* @return Credentials for authentication with an access token.
@@ -26,7 +26,7 @@ public static Credentials Token(String accessToken) {
2626

2727
/**
2828
* Authenticating with email and password.
29-
*
29+
*
3030
* @param email
3131
* CoScale user email address.
3232
* @param password
@@ -39,7 +39,7 @@ public static Credentials Password(String email, String password) {
3939

4040
/**
4141
* usesToken is used to determine which authentication method is used.
42-
*
42+
*
4343
* @return boolean true if Token authentication is used.
4444
*/
4545
public boolean usesToken() {

src/main/java/com/coscale/sdk/client/commons/Options.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,17 @@ public String query() {
7272
parts.addAll(expandList());
7373
return Joiner.on('&').join(parts);
7474
}
75+
76+
/**
77+
* If neither the selectBys or expands has entries, there are no options for the query.
78+
* @return False if neither selectBys or expands has entries/is initialized.
79+
* True if either one has entries.
80+
*/
81+
public boolean hasQuery() {
82+
if ((selectBys == null || selectBys.isEmpty()) && (expands == null || expands.isEmpty())) {
83+
return false;
84+
} else {
85+
return true;
86+
}
87+
}
7588
}

src/main/java/com/coscale/sdk/client/data/DataApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public DataApi(ApiClient api) {
3535
* @throws IOException
3636
*/
3737
public Msg insert(String subjectId, DataInsert data) throws IOException {
38-
return api.callWithAuth("POST", "/data/" + subjectId + "/", data, new TypeReference<Msg>() {
38+
return api.callWithAuth("POST", "/data/" + subjectId + '/', data, new TypeReference<Msg>() {
3939
});
4040
}
4141
}

src/main/java/com/coscale/sdk/client/data/MetricData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.coscale.sdk.client.data;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
4+
import java.util.Collections;
55
import java.util.List;
66

77
import com.fasterxml.jackson.annotation.JsonFormat;
@@ -20,7 +20,7 @@ static class DataType {
2020

2121
/**
2222
* DataType constructor.
23-
*
23+
*
2424
* @param timestamp
2525
*/
2626
public DataType(Long timestamp) {
@@ -43,7 +43,7 @@ public DataType(Long timestamp) {
4343

4444
public MetricData(Long metricId, T data) {
4545
this.metricId = metricId;
46-
this.data = new ArrayList<T>(Arrays.asList(data));
46+
this.data = new ArrayList<T>(Collections.singletonList(data));
4747

4848
this.type = data.getClass();
4949
}

0 commit comments

Comments
 (0)