Skip to content

Commit 7146937

Browse files
committed
Fixed authentication loop. This would happen when authentication expires.
1 parent a56310d commit 7146937

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

fi/cosky/sdk/API.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class API {
3535
private ApiData apiData;
3636
private TokenData tokenData;
3737
private ObjectCache objectCache;
38+
3839
static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX").create();
3940

4041
public API(String baseUrl) {
@@ -67,7 +68,7 @@ public boolean authenticate(String username, String password) {
6768
* @param l navigation link
6869
* @return object of type tClass
6970
* @throws NFleetException when there is data validation problems
70-
* @throws IOException when there is problems with infrastructure (connection etc..)
71+
* IOException when there is problems with infrastructure (connection etc..)
7172
*/
7273
public <T extends BaseData> T navigate(Class<T> tClass, Link l) throws IOException {
7374
return navigate(tClass, l, null);
@@ -212,9 +213,13 @@ private <T extends BaseData> T sendRequest(Verb verb, String url, Class<T> tClas
212213

213214
if (connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
214215
System.out.println("Authentication expired");
215-
this.authenticate(ClientKey, ClientSecret);
216-
System.out.println("Authenticated again");
217-
return sendRequest(verb, url, tClass, object);
216+
217+
if( this.authenticate(ClientKey, ClientSecret) ) {
218+
System.out.println("Authenticated again");
219+
return sendRequest(verb, url, tClass, object);
220+
}
221+
222+
else throw new IOException("Could not authenticate");
218223
}
219224

220225
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
@@ -305,6 +310,17 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
305310
return (T) data;
306311
}
307312

313+
if (connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
314+
System.out.println("Authentication expired");
315+
316+
if( this.authenticate(ClientKey, ClientSecret) ) {
317+
System.out.println("Authenticated again");
318+
return sendRequestWithAddedHeaders(verb, url, tClass, object, headers);
319+
}
320+
321+
else throw new IOException("Could not authenticate");
322+
}
323+
308324
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
309325
System.out.println("code: " + connection.getResponseCode() + " " + connection.getResponseMessage() + " " + url);
310326
InputStream stream = connection.getErrorStream();

0 commit comments

Comments
 (0)