86 lines
2.8 KiB
Java
86 lines
2.8 KiB
Java
import Credentials.UserCredentials;
|
|
import Files.ReadFile;
|
|
import Files.WriteFile;
|
|
import HttpCall.HttpCall;
|
|
import HttpCall.HttpPost;
|
|
import org.apache.jena.base.Sys;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
import org.json.simple.parser.JSONParser;
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
public class ApiCall {
|
|
private static String token = "0";
|
|
private static String oldId = "1";
|
|
|
|
public static String FetchDataFromApi(String extension) throws InterruptedException, ExecutionException, TimeoutException {
|
|
UserCredentials userCredentials = new UserCredentials();
|
|
final String username = userCredentials.getUsername();
|
|
final String password = userCredentials.getPassword();
|
|
String baseURL = userCredentials.getBaseURL();
|
|
String outputToken;
|
|
WriteFile writingIntoFile = new WriteFile();
|
|
HttpCall httpCall = new HttpCall();
|
|
String devicesResult = "";
|
|
|
|
if (token.equals(oldId)) {
|
|
//do nothing
|
|
} else {
|
|
try {
|
|
outputToken = SecurityToken.OnCallMethod(username, password);
|
|
JSONObject obj = new JSONObject(outputToken);
|
|
token = obj.getString("id");
|
|
|
|
writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/securityToken.txt", token);
|
|
oldId = token;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
try {
|
|
devicesResult = httpCall.HttpGet(baseURL + extension + "?access_token=" + token,token);
|
|
JSONArray jsonarray = new JSONArray(devicesResult);
|
|
for (int i = 0; i < jsonarray.length(); i++) {
|
|
JSONObject jsonobject = jsonarray.getJSONObject(i);
|
|
String id = jsonobject.getString("id");
|
|
System.out.println(id);
|
|
writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/devicesID.txt", id);
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
|
|
try {
|
|
writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/" + extension + "Output.json", devicesResult);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return devicesResult;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|