105 lines
3.9 KiB
Java
105 lines
3.9 KiB
Java
package com.example.dcav2gui;
|
|
|
|
import static com.example.dcav2gui.MainActivity.globalSettings;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Locale;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
|
|
public class InstanceInterface {
|
|
private static final String API_BASE_URL = globalSettings.apiUrl;
|
|
private static final String API_KEY = globalSettings.apiKey;
|
|
private static final OkHttpClient httpClient = new OkHttpClient();
|
|
|
|
public static ProfitStatsData getProfitStatsData() throws IOException {
|
|
// Construct the API request URL for stock quote
|
|
Request stockRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/statistics_server/combined_totals")
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
|
|
try (Response statsResponse = httpClient.newCall(stockRequest).execute()) {
|
|
if (!statsResponse.isSuccessful()) {
|
|
throw new IOException("Unexpected code " + statsResponse);
|
|
}
|
|
String stockResponseBody = statsResponse.body().string();
|
|
|
|
double todaysProfit = getTodaysProfit(stockResponseBody);
|
|
double thisMonthProfit = getThisMonthsProfit(stockResponseBody);
|
|
|
|
// Return the stock quote data
|
|
return new ProfitStatsData(
|
|
todaysProfit,
|
|
thisMonthProfit
|
|
);
|
|
}
|
|
}
|
|
|
|
private static double getTodaysProfit(String jsonResponse) {
|
|
try {
|
|
// Parse the JSON response
|
|
JsonElement jsonElement = JsonParser.parseString(jsonResponse);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return 0.0;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("combined")) {
|
|
JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray();
|
|
return combinedArray.get(0).getAsDouble();
|
|
} else {
|
|
System.err.println("The parsed JSON response does not contain a 'combined' array.");
|
|
return 0.0;
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println("Error processing combined profit data: " + e.getMessage());
|
|
return 0.0;
|
|
}
|
|
}
|
|
|
|
private static double getThisMonthsProfit(String jsonResponse) {
|
|
try {
|
|
// Parse the JSON response
|
|
JsonElement jsonElement = JsonParser.parseString(jsonResponse);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return 0.0;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("combined")) {
|
|
JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray();
|
|
return combinedArray.get(1).getAsDouble();
|
|
} else {
|
|
System.err.println("The parsed JSON response does not contain a 'combined' array.");
|
|
return 0.0;
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println("Error processing combined profit data: " + e.getMessage());
|
|
return 0.0;
|
|
}
|
|
}
|
|
|
|
|
|
// Class to hold profits stats data
|
|
public static class ProfitStatsData {
|
|
private final double profitsToday;
|
|
private final double profitsThisMonth;
|
|
|
|
public ProfitStatsData(double profitsToday, double profitsThisMonth) {
|
|
this.profitsToday = profitsToday;
|
|
this.profitsThisMonth = profitsThisMonth;
|
|
}
|
|
|
|
// Getters
|
|
public double getProfitsToday() { return profitsToday; }
|
|
public double getProfitsThisMonth() { return profitsThisMonth; }
|
|
}
|
|
} |