987 lines
47 KiB
Java
987 lines
47 KiB
Java
package com.example.dcav2gui;
|
|
|
|
import static com.example.dcav2gui.MainActivity.globalSettings;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.content.Context;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.text.InputType;
|
|
import android.view.View;
|
|
import android.widget.EditText;
|
|
import android.widget.Toast;
|
|
|
|
import com.example.dcav2gui.ui.earners.EarnerData;
|
|
import com.example.dcav2gui.ui.home.HomeFragment;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import okhttp3.MediaType;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.Response;
|
|
|
|
public class EarnerInterface {
|
|
|
|
private static final String API_BASE_URL = globalSettings.apiUrl;
|
|
private static final String API_KEY = globalSettings.earnApiKey;
|
|
private static final OkHttpClient httpClient = new OkHttpClient();
|
|
|
|
|
|
public static void sendRequestGlobalData(Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
EarnerGlobalData result = getEarnerGlobalData(true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showSimpleDialog("Global data", result.toString(), context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get global status", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestTotalBalance(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getTotalBalance(exchange, true);
|
|
String dialogTitle = "Balances";
|
|
String dialogMessage = "Trading balance: " + result.get("trading_balance").getAsString() +
|
|
" | Earning balance " + result.get("earning_balance").getAsString();
|
|
new Handler(Looper.getMainLooper()).post(() -> showSimpleDialog(dialogTitle, dialogMessage, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get total balance", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestGetStepSize(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getStepSize(exchange, true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showJsonDialog(result, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get step size", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestGetPercentage(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getPercentage(exchange, true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showJsonDialog(result, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get percentage", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestLastSubscription(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getLastSubscription(exchange, true);
|
|
String dialogTitle = "Last subscription";
|
|
String dialogMessage = HomeFragment.timeStampConverter(result.get("last_subscription").getAsJsonObject().get(exchange).getAsDouble(),false);
|
|
new Handler(Looper.getMainLooper()).post(() -> showSimpleDialog(dialogTitle, dialogMessage, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get last subscription", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestLastRedemption(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getLastRedemption(exchange, true);
|
|
String dialogTitle = "Last redemption";
|
|
String dialogMessage = HomeFragment.timeStampConverter(result.get("last_redemption").getAsJsonObject().get(exchange).getAsDouble(),false);
|
|
new Handler(Looper.getMainLooper()).post(() -> showSimpleDialog(dialogTitle, dialogMessage, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get last redemption", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestGetTimeBetweenSubscriptions(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getTimeBetweenSubscriptions(exchange, true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showJsonDialog(result, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get time between subscriptions", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestGetTimeBetweenRedemptions(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getTimeBetweenRedemptions(exchange, true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showJsonDialog(result, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get time between redemptions", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestGetMinimumAmountInTradingAccount(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = getMinimumAmountInTradingAccount(exchange, true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showJsonDialog(result, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to get minimum amount in trading account", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestTogglePause(String exchange, Context context) {
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject result = togglePause(exchange, true);
|
|
new Handler(Looper.getMainLooper()).post(() -> showJsonDialog(result, context));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, "Failed to toggle pause", Toast.LENGTH_SHORT).show());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public static void sendRequestSetStepSize(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Change step size on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double amountToAdd = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = setStepSize(exchange, amountToAdd,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to change step size: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
public static void sendRequestSetPercentage(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Change percentage on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double newPercentage = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = setPercentage(exchange, newPercentage,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to change percentage: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
public static void sendRequestSetTimeBetweenSubscriptions(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Change time between subscriptions on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double newTime = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = setTimeBetweenSubscriptions(exchange, newTime,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to change time between subscriptions: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
public static void sendRequestSetTimeBetweenRedemptions(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Change time between redemptions on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double newTime = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = setTimeBetweenRedemptions(exchange, newTime,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to change time between redemptions: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
public static void sendRequestSetMinimumAmountInTradingAccount(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Change minimum amount in trading account on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double minimumAmount = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = setMinimumAmountInTradingAccount(exchange, minimumAmount,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to change minimum amount in trading account: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
public static void sendRequestSubscribeFunds(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Subscribe funds on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double amountToSubscribe = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = subscribeFunds(exchange, amountToSubscribe,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to subscribe funds: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
public static void sendRequestRedeemFunds(String exchange, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setTitle("Redeem funds on "+ exchange);
|
|
|
|
final EditText input = new EditText(context);
|
|
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
|
builder.setView(input);
|
|
|
|
builder.setPositiveButton("Set", (dialog, which) -> {
|
|
final double amountToRedeem = Double.parseDouble(input.getText().toString());
|
|
new Thread(() -> {
|
|
try {
|
|
JsonObject response = redeemFunds(exchange, amountToRedeem,true);
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
showJsonDialog(response, context);
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Show an error dialog on the main thread
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
|
|
errorBuilder.setTitle("Error");
|
|
errorBuilder.setMessage("Failed to redeem funds: " + e.getMessage());
|
|
errorBuilder.setPositiveButton("OK", null);
|
|
errorBuilder.show();
|
|
});
|
|
}
|
|
}).start();
|
|
});
|
|
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
|
|
builder.show();
|
|
}
|
|
|
|
|
|
|
|
public static EarnerGlobalData getEarnerGlobalData(boolean retry) throws IOException {
|
|
double uptime = 0.0;
|
|
List<EarnerData> earnerList = new ArrayList<>();
|
|
|
|
Request getEarnerGlobalDataRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_global_status")
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getEarnerGlobalDataResponse = httpClient.newCall(getEarnerGlobalDataRequest).execute()) {
|
|
if (!getEarnerGlobalDataResponse.isSuccessful()) {
|
|
if (getEarnerGlobalDataResponse.code() == 503 && retry) {
|
|
return getEarnerGlobalData(false);
|
|
}
|
|
throw new IOException("Unexpected code " + getEarnerGlobalDataResponse);
|
|
}
|
|
String getEarnerGlobalDataResponseBody = getEarnerGlobalDataResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getEarnerGlobalDataResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return null;
|
|
}
|
|
|
|
for (String key : jsonObject.keySet()) {
|
|
if (key.equals("uptime")) {
|
|
uptime = jsonObject.get(key).getAsDouble();
|
|
} else {
|
|
JsonObject jsonEarner = jsonObject.get(key).getAsJsonObject();
|
|
earnerList.add(new EarnerData(
|
|
key,
|
|
jsonEarner.get("trading_balance").getAsDouble(),
|
|
jsonEarner.get("earning_balance").getAsDouble(),
|
|
jsonEarner.get("is_paused").getAsBoolean(),
|
|
jsonEarner.get("step_size").getAsDouble(),
|
|
jsonEarner.get("percentage").getAsDouble(),
|
|
jsonEarner.get("minimum_amount_in_trading_account").getAsDouble(),
|
|
jsonEarner.get("time_between_subscriptions").getAsDouble(),
|
|
jsonEarner.get("time_between_redemptions").getAsDouble(),
|
|
jsonEarner.get("last_subscription").getAsJsonObject(),
|
|
jsonEarner.get("last_redemption").getAsJsonObject()
|
|
));
|
|
}
|
|
}
|
|
}
|
|
return new EarnerGlobalData(uptime, earnerList);
|
|
}
|
|
|
|
public static JsonObject togglePause(String exchange, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request togglePauseRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/toggle_pause")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response togglePauseResponse = httpClient.newCall(togglePauseRequest).execute()) {
|
|
if (!togglePauseResponse.isSuccessful()) {
|
|
if (togglePauseResponse.code() == 503 && retry) {
|
|
return togglePause(exchange, false);
|
|
}
|
|
throw new IOException("Unexpected code " + togglePauseResponse);
|
|
}
|
|
String togglePauseResponseBody = togglePauseResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(togglePauseResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no error, the response is {"Status": true|false }
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getStepSize(String exchange, boolean retry) throws IOException {
|
|
Request getStepSizeRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_step_size?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getStepSizeResponse = httpClient.newCall(getStepSizeRequest).execute()) {
|
|
if (!getStepSizeResponse.isSuccessful()) {
|
|
if (getStepSizeResponse.code() == 503 && retry) {
|
|
return getStepSize(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getStepSizeResponse);
|
|
}
|
|
String getStepSizeResponseBody = getStepSizeResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getStepSizeResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
return jsonElement.getAsJsonObject();
|
|
}
|
|
}
|
|
|
|
public static JsonObject setStepSize(String exchange, double stepSize, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("new_step_size", stepSize);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request setStepSizeRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/set_step_size")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response setStepSizeResponse = httpClient.newCall(setStepSizeRequest).execute()) {
|
|
if (!setStepSizeResponse.isSuccessful()) {
|
|
if (setStepSizeResponse.code() == 503 && retry) {
|
|
return setStepSize(exchange, stepSize, false);
|
|
}
|
|
throw new IOException("Unexpected code " + setStepSizeResponse);
|
|
}
|
|
String setStepSizeResponseBody = setStepSizeResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(setStepSizeResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no error, the response is {"step_size": stepSize }
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getPercentage(String exchange, boolean retry) throws IOException {
|
|
Request getPercentageRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_percentage?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getPercentageResponse = httpClient.newCall(getPercentageRequest).execute()) {
|
|
if (!getPercentageResponse.isSuccessful()) {
|
|
if (getPercentageResponse.code() == 503 && retry) {
|
|
return getPercentage(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getPercentageResponse);
|
|
}
|
|
String getPercentageResponseBody = getPercentageResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getPercentageResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
return jsonElement.getAsJsonObject();
|
|
}
|
|
}
|
|
|
|
public static JsonObject setPercentage(String exchange, double newPercentage, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("new_percentage", newPercentage);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request setPercentageRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/set_percentage")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response setPercentageResponse = httpClient.newCall(setPercentageRequest).execute()) {
|
|
if (!setPercentageResponse.isSuccessful()) {
|
|
if (setPercentageResponse.code() == 503 && retry) {
|
|
return setPercentage(exchange, newPercentage, false);
|
|
}
|
|
throw new IOException("Unexpected code " + setPercentageResponse);
|
|
}
|
|
String setPercentageResponseBody = setPercentageResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(setPercentageResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no error, the response is {"percentage": newPercentage }
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getTimeBetweenSubscriptions(String exchange, boolean retry) throws IOException {
|
|
Request getTimeBetweenSubscriptionsRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_time_between_subscriptions?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getTimeBetweenSubscriptionsResponse = httpClient.newCall(getTimeBetweenSubscriptionsRequest).execute()) {
|
|
if (!getTimeBetweenSubscriptionsResponse.isSuccessful()) {
|
|
if (getTimeBetweenSubscriptionsResponse.code() == 503 && retry) {
|
|
return getTimeBetweenSubscriptions(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getTimeBetweenSubscriptionsResponse);
|
|
}
|
|
String getTimeBetweenSubscriptionsResponseBody = getTimeBetweenSubscriptionsResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getTimeBetweenSubscriptionsResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
return jsonElement.getAsJsonObject();
|
|
}
|
|
}
|
|
|
|
public static JsonObject setTimeBetweenSubscriptions(String exchange, double newTime, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("new_time_between_subscriptions", newTime);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request setTimeBetweenSubscriptionsRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/set_time_between_subscriptions")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response setTimeBetweenSubscriptionsResponse = httpClient.newCall(setTimeBetweenSubscriptionsRequest).execute()) {
|
|
if (!setTimeBetweenSubscriptionsResponse.isSuccessful()) {
|
|
if (setTimeBetweenSubscriptionsResponse.code() == 503 && retry) {
|
|
return setTimeBetweenSubscriptions(exchange, newTime, false);
|
|
}
|
|
throw new IOException("Unexpected code " + setTimeBetweenSubscriptionsResponse);
|
|
}
|
|
String setTimeBetweenSubscriptionsResponseBody = setTimeBetweenSubscriptionsResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(setTimeBetweenSubscriptionsResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no error, the response is {"time_between_subscriptions": newTime }
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getTimeBetweenRedemptions(String exchange, boolean retry) throws IOException {
|
|
Request getTimeBetweenRedemptionsRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_time_between_redemptions?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getTimeBetweenRedemptionsResponse = httpClient.newCall(getTimeBetweenRedemptionsRequest).execute()) {
|
|
if (!getTimeBetweenRedemptionsResponse.isSuccessful()) {
|
|
if (getTimeBetweenRedemptionsResponse.code() == 503 && retry) {
|
|
return getTimeBetweenRedemptions(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getTimeBetweenRedemptionsResponse);
|
|
}
|
|
String getTimeBetweenRedemptionsResponseBody = getTimeBetweenRedemptionsResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getTimeBetweenRedemptionsResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
return jsonElement.getAsJsonObject();
|
|
}
|
|
}
|
|
|
|
public static JsonObject setTimeBetweenRedemptions(String exchange, double newTime, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("new_time_between_redemptions", newTime);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request setTimeBetweenRedemptionsRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/set_time_between_redemptions")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response setTimeBetweenRedemptionsResponse = httpClient.newCall(setTimeBetweenRedemptionsRequest).execute()) {
|
|
if (!setTimeBetweenRedemptionsResponse.isSuccessful()) {
|
|
if (setTimeBetweenRedemptionsResponse.code() == 503 && retry) {
|
|
return setTimeBetweenRedemptions(exchange, newTime, false);
|
|
}
|
|
throw new IOException("Unexpected code " + setTimeBetweenRedemptionsResponse);
|
|
}
|
|
String setTimeBetweenRedemptionsResponseBody = setTimeBetweenRedemptionsResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(setTimeBetweenRedemptionsResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no error, the response is {"time_between_redemptions": newTime }
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getMinimumAmountInTradingAccount(String exchange, boolean retry) throws IOException {
|
|
Request getMinimumAmountInTradingAccountRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_minimum_amount_in_trading_account?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getMinimumAmountInTradingAccountResponse = httpClient.newCall(getMinimumAmountInTradingAccountRequest).execute()) {
|
|
if (!getMinimumAmountInTradingAccountResponse.isSuccessful()) {
|
|
if (getMinimumAmountInTradingAccountResponse.code() == 503 && retry) {
|
|
return getMinimumAmountInTradingAccount(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getMinimumAmountInTradingAccountResponse);
|
|
}
|
|
String getMinimumAmountInTradingAccountResponseBody = getMinimumAmountInTradingAccountResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getMinimumAmountInTradingAccountResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
return jsonElement.getAsJsonObject();
|
|
}
|
|
}
|
|
|
|
public static JsonObject setMinimumAmountInTradingAccount(String exchange, double minAmount, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("new_minimum_amount_in_trading_account", minAmount);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request setMinimumAmountInTradingAccountRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/set_minimum_amount_in_trading_account")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response setMinimumAmountInTradingAccountResponse = httpClient.newCall(setMinimumAmountInTradingAccountRequest).execute()) {
|
|
if (!setMinimumAmountInTradingAccountResponse.isSuccessful()) {
|
|
if (setMinimumAmountInTradingAccountResponse.code() == 503 && retry) {
|
|
return setMinimumAmountInTradingAccount(exchange, minAmount, false);
|
|
}
|
|
throw new IOException("Unexpected code " + setMinimumAmountInTradingAccountResponse);
|
|
}
|
|
String setMinimumAmountInTradingAccountResponseBody = setMinimumAmountInTradingAccountResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(setMinimumAmountInTradingAccountResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no error, the response is {"minimum_amount_in_trading_account": minAmount}
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getLastSubscription(String exchange, boolean retry) throws IOException {
|
|
Request getLastSubscriptionRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_last_subscription?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getLastSubscriptionResponse = httpClient.newCall(getLastSubscriptionRequest).execute()) {
|
|
if (!getLastSubscriptionResponse.isSuccessful()) {
|
|
if (getLastSubscriptionResponse.code() == 503 && retry) {
|
|
return getLastSubscription(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getLastSubscriptionResponse);
|
|
}
|
|
String getLastSubscriptionResponseBody = getLastSubscriptionResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getLastSubscriptionResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getLastRedemption(String exchange, boolean retry) throws IOException {
|
|
Request getLastRedemptionRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_last_redemption?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getLastRedemptionResponse = httpClient.newCall(getLastRedemptionRequest).execute()) {
|
|
if (!getLastRedemptionResponse.isSuccessful()) {
|
|
if (getLastRedemptionResponse.code() == 503 && retry) {
|
|
return getLastRedemption(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getLastRedemptionResponse);
|
|
}
|
|
String getLastRedemptionResponseBody = getLastRedemptionResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getLastRedemptionResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject getTotalBalance(String exchange, boolean retry) throws IOException {
|
|
Request getTotalBalanceRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/get_total_balance?broker=" + exchange)
|
|
.header("X-API-KEY", API_KEY)
|
|
.build();
|
|
try (Response getTotalBalanceResponse = httpClient.newCall(getTotalBalanceRequest).execute()) {
|
|
if (!getTotalBalanceResponse.isSuccessful()) {
|
|
if (getTotalBalanceResponse.code() == 503 && retry) {
|
|
return getTotalBalance(exchange,false);
|
|
}
|
|
throw new IOException("Unexpected code " + getTotalBalanceResponse);
|
|
}
|
|
String getTotalBalanceResponseBody = getTotalBalanceResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(getTotalBalanceResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
return jsonElement.getAsJsonObject();
|
|
}
|
|
}
|
|
|
|
public static JsonObject subscribeFunds(String exchange, double amount, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("amount", amount);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request subscribeFundsRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/subscribe")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response subscribeFundsResponse = httpClient.newCall(subscribeFundsRequest).execute()) {
|
|
if (!subscribeFundsResponse.isSuccessful()) {
|
|
if (subscribeFundsResponse.code() == 503 && retry) {
|
|
return subscribeFunds(exchange, amount, false);
|
|
}
|
|
throw new IOException("Unexpected code " + subscribeFundsResponse);
|
|
}
|
|
String subscribeFundsResponseBody = subscribeFundsResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(subscribeFundsResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no errors in the request, the response is {"Success": 0 (success) | 1 (earner error)}
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
public static JsonObject redeemFunds(String exchange, double amount, boolean retry) throws IOException {
|
|
Gson gson = new Gson();
|
|
JsonObject jsonPayload = new JsonObject();
|
|
jsonPayload.addProperty("broker", exchange);
|
|
jsonPayload.addProperty("amount", amount);
|
|
String jsonPayloadString = gson.toJson(jsonPayload);
|
|
|
|
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
|
|
Request redeemFundsRequest = new Request.Builder()
|
|
.url(API_BASE_URL + "/earn/redeem")
|
|
.header("X-API-KEY", API_KEY)
|
|
.post(requestBody)
|
|
.build();
|
|
|
|
try (Response redeemFundsResponse = httpClient.newCall(redeemFundsRequest).execute()) {
|
|
if (!redeemFundsResponse.isSuccessful()) {
|
|
if (redeemFundsResponse.code() == 503 && retry) {
|
|
return redeemFunds(exchange, amount, false);
|
|
}
|
|
throw new IOException("Unexpected code " + redeemFundsResponse);
|
|
}
|
|
String redeemFundsResponseBody = redeemFundsResponse.body().string();
|
|
JsonElement jsonElement = JsonParser.parseString(redeemFundsResponseBody);
|
|
if (!jsonElement.isJsonObject()) {
|
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
|
return null;
|
|
}
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
if (jsonObject.has("Error")) {
|
|
System.err.println("The parsed JSON response contains Error");
|
|
return jsonObject;
|
|
}
|
|
|
|
//If no errors in the request, the response is {"Success": 0 (success) | 1 (earner error)}
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
|
|
public static void showSimpleDialog(String title, String message, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
builder.setTitle(title);
|
|
builder.setMessage(message);
|
|
builder.setPositiveButton("OK", null);
|
|
builder.show();
|
|
}
|
|
|
|
public static void showJsonDialog(JsonObject result, Context context) {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
for (String key: result.keySet()) {
|
|
builder.setTitle(key);
|
|
builder.setMessage(result.get(key).getAsString());
|
|
}
|
|
builder.setPositiveButton("OK", null);
|
|
builder.show();
|
|
}
|
|
|
|
public static class EarnerGlobalData {
|
|
private final double uptime;
|
|
private final List<EarnerData> earnerList;
|
|
|
|
public EarnerGlobalData(double uptime, List<EarnerData> earnerList) {
|
|
this.uptime = uptime;
|
|
this.earnerList = earnerList;
|
|
}
|
|
|
|
public double getUptime() {
|
|
return uptime;
|
|
}
|
|
|
|
public List<EarnerData> getEarnerList() {
|
|
return earnerList;
|
|
}
|
|
}
|
|
}
|
|
|