WorkerInterface methods done
This commit is contained in:
parent
d812811ed5
commit
25cbf1615f
|
|
@ -187,7 +187,7 @@ public class WorkerInterface {
|
||||||
formBuilder.add("quote", quote);
|
formBuilder.add("quote", quote);
|
||||||
RequestBody formBody = formBuilder.build();
|
RequestBody formBody = formBuilder.build();
|
||||||
Request removeTraderRequest = new Request.Builder()
|
Request removeTraderRequest = new Request.Builder()
|
||||||
.url(API_BASE_URL + "/" + exchange + "/add_pair")
|
.url(API_BASE_URL + "/" + exchange + "/remove_pair")
|
||||||
.header("X-API-KEY", API_KEY)
|
.header("X-API-KEY", API_KEY)
|
||||||
.post(formBody)
|
.post(formBody)
|
||||||
.build();
|
.build();
|
||||||
|
|
@ -226,7 +226,7 @@ public class WorkerInterface {
|
||||||
formBuilder.add("quote", quote);
|
formBuilder.add("quote", quote);
|
||||||
RequestBody formBody = formBuilder.build();
|
RequestBody formBody = formBuilder.build();
|
||||||
Request restartTraderRequest = new Request.Builder()
|
Request restartTraderRequest = new Request.Builder()
|
||||||
.url(API_BASE_URL + "/" + exchange + "/add_pair")
|
.url(API_BASE_URL + "/" + exchange + "/restart_pair")
|
||||||
.header("X-API-KEY", API_KEY)
|
.header("X-API-KEY", API_KEY)
|
||||||
.post(formBody)
|
.post(formBody)
|
||||||
.build();
|
.build();
|
||||||
|
|
@ -265,7 +265,7 @@ public class WorkerInterface {
|
||||||
formBuilder.add("quote", quote);
|
formBuilder.add("quote", quote);
|
||||||
RequestBody formBody = formBuilder.build();
|
RequestBody formBody = formBuilder.build();
|
||||||
Request importTraderRequest = new Request.Builder()
|
Request importTraderRequest = new Request.Builder()
|
||||||
.url(API_BASE_URL + "/" + exchange + "/add_pair")
|
.url(API_BASE_URL + "/" + exchange + "/import_pair")
|
||||||
.header("X-API-KEY", API_KEY)
|
.header("X-API-KEY", API_KEY)
|
||||||
.post(formBody)
|
.post(formBody)
|
||||||
.build();
|
.build();
|
||||||
|
|
@ -451,25 +451,208 @@ public class WorkerInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject switchToLong(String exchange, String pair) throws IOException {
|
public static JsonObject switchToLong(String exchange, String pair, boolean calculateProfits, boolean retry) throws IOException {
|
||||||
|
String[] pairBaseAndQuote = pair.split("/");
|
||||||
|
String base = pairBaseAndQuote[0];
|
||||||
|
String quote = pairBaseAndQuote[1];
|
||||||
|
String profits = "0";
|
||||||
|
if (calculateProfits) {
|
||||||
|
profits = "1";
|
||||||
|
}
|
||||||
|
|
||||||
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
||||||
|
formBuilder.add("base", base);
|
||||||
|
formBuilder.add("quote", quote);
|
||||||
|
formBuilder.add("calculate_profits", profits);
|
||||||
|
RequestBody formBody = formBuilder.build();
|
||||||
|
Request switchToLongRequest = new Request.Builder()
|
||||||
|
.url(API_BASE_URL + "/" + exchange + "/switch_to_long")
|
||||||
|
.header("X-API-KEY", API_KEY)
|
||||||
|
.post(formBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response switchToLongResponse = httpClient.newCall(switchToLongRequest).execute()) {
|
||||||
|
if (!switchToLongResponse.isSuccessful()) {
|
||||||
|
if (switchToLongResponse.code() == 503 && retry) {
|
||||||
|
return switchToLong(exchange, pair, calculateProfits,false);
|
||||||
|
}
|
||||||
|
throw new IOException("Unexpected code " + switchToLongResponse);
|
||||||
|
}
|
||||||
|
String switchToLongResponseBody = switchToLongResponse.body().string();
|
||||||
|
JsonElement jsonElement = JsonParser.parseString(switchToLongResponseBody);
|
||||||
|
if (!jsonElement.isJsonObject()) {
|
||||||
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
if (jsonObject.has("Error")) {
|
||||||
|
System.err.println("The parsed JSON response contains Error");
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonObject switchToShort(String exchange, String pair) throws IOException {
|
//If no error, the response is {"Success":"Pair switched to long mode"}
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject switchToShort(String exchange, String pair, boolean retry) throws IOException {
|
||||||
|
String[] pairBaseAndQuote = pair.split("/");
|
||||||
|
String base = pairBaseAndQuote[0];
|
||||||
|
String quote = pairBaseAndQuote[1];
|
||||||
|
|
||||||
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
||||||
|
formBuilder.add("base", base);
|
||||||
|
formBuilder.add("quote", quote);
|
||||||
|
RequestBody formBody = formBuilder.build();
|
||||||
|
Request switchToShortRequest = new Request.Builder()
|
||||||
|
.url(API_BASE_URL + "/" + exchange + "/switch_to_short")
|
||||||
|
.header("X-API-KEY", API_KEY)
|
||||||
|
.post(formBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response switchToShortResponse = httpClient.newCall(switchToShortRequest).execute()) {
|
||||||
|
if (!switchToShortResponse.isSuccessful()) {
|
||||||
|
if (switchToShortResponse.code() == 503 && retry) {
|
||||||
|
return switchToShort(exchange, pair, false);
|
||||||
|
}
|
||||||
|
throw new IOException("Unexpected code " + switchToShortResponse);
|
||||||
|
}
|
||||||
|
String switchToShortResponseBody = switchToShortResponse.body().string();
|
||||||
|
JsonElement jsonElement = JsonParser.parseString(switchToShortResponseBody);
|
||||||
|
if (!jsonElement.isJsonObject()) {
|
||||||
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
if (jsonObject.has("Error")) {
|
||||||
|
System.err.println("The parsed JSON response contains Error");
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonObject switchQuoteCurrency(String exchange, String pair) throws IOException {
|
//If no error, the response is {"Success":"Pair switched to short mode"}
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject switchQuoteCurrency(String exchange, String pair, String targetQuoteCurrency, boolean retry) throws IOException {
|
||||||
|
String[] pairBaseAndQuote = pair.split("/");
|
||||||
|
String base = pairBaseAndQuote[0];
|
||||||
|
String quote = pairBaseAndQuote[1];
|
||||||
|
|
||||||
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
||||||
|
formBuilder.add("base", base);
|
||||||
|
formBuilder.add("quote", quote);
|
||||||
|
formBuilder.add("new_quote", targetQuoteCurrency);
|
||||||
|
RequestBody formBody = formBuilder.build();
|
||||||
|
Request switchQuoteCurrencyRequest = new Request.Builder()
|
||||||
|
.url(API_BASE_URL + "/" + exchange + "/switch_quote_currency")
|
||||||
|
.header("X-API-KEY", API_KEY)
|
||||||
|
.post(formBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response switchQuoteCurrencyResponse = httpClient.newCall(switchQuoteCurrencyRequest).execute()) {
|
||||||
|
if (!switchQuoteCurrencyResponse.isSuccessful()) {
|
||||||
|
if (switchQuoteCurrencyResponse.code() == 503 && retry) {
|
||||||
|
return switchQuoteCurrency(exchange, pair, targetQuoteCurrency,false);
|
||||||
|
}
|
||||||
|
throw new IOException("Unexpected code " + switchQuoteCurrencyResponse);
|
||||||
|
}
|
||||||
|
String switchQuoteCurrencyResponseBody = switchQuoteCurrencyResponse.body().string();
|
||||||
|
JsonElement jsonElement = JsonParser.parseString(switchQuoteCurrencyResponseBody);
|
||||||
|
if (!jsonElement.isJsonObject()) {
|
||||||
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
if (jsonObject.has("Error")) {
|
||||||
|
System.err.println("The parsed JSON response contains Error");
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonObject addSafetyOrders(String exchange, String pair) throws IOException {
|
//If no error, the response is {"Success":"Mission successful"}
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject addSafetyOrders(String exchange, String pair, int amount, boolean retry) throws IOException {
|
||||||
|
String[] pairBaseAndQuote = pair.split("/");
|
||||||
|
String base = pairBaseAndQuote[0];
|
||||||
|
String quote = pairBaseAndQuote[1];
|
||||||
|
|
||||||
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
||||||
|
formBuilder.add("base", base);
|
||||||
|
formBuilder.add("quote", quote);
|
||||||
|
formBuilder.add("amount", String.valueOf(amount));
|
||||||
|
RequestBody formBody = formBuilder.build();
|
||||||
|
Request addSafetyOrdersRequest = new Request.Builder()
|
||||||
|
.url(API_BASE_URL + "/" + exchange + "/add_so")
|
||||||
|
.header("X-API-KEY", API_KEY)
|
||||||
|
.post(formBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response addSafetyOrdersResponse = httpClient.newCall(addSafetyOrdersRequest).execute()) {
|
||||||
|
if (!addSafetyOrdersResponse.isSuccessful()) {
|
||||||
|
if (addSafetyOrdersResponse.code() == 503 && retry) {
|
||||||
|
return addSafetyOrders(exchange, pair, amount,false);
|
||||||
|
}
|
||||||
|
throw new IOException("Unexpected code " + addSafetyOrdersResponse);
|
||||||
|
}
|
||||||
|
String addSafetyOrdersResponseBody = addSafetyOrdersResponse.body().string();
|
||||||
|
JsonElement jsonElement = JsonParser.parseString(addSafetyOrdersResponseBody);
|
||||||
|
if (!jsonElement.isJsonObject()) {
|
||||||
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
if (jsonObject.has("Error")) {
|
||||||
|
System.err.println("The parsed JSON response contains Error");
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonObject addQuote(String exchange, String pair) throws IOException {
|
//If no error, the response is {"Success":"Added $amount safety orders"}
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject addQuote(String exchange, String pair, double amount, boolean retry) throws IOException {
|
||||||
|
String[] pairBaseAndQuote = pair.split("/");
|
||||||
|
String base = pairBaseAndQuote[0];
|
||||||
|
String quote = pairBaseAndQuote[1];
|
||||||
|
|
||||||
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
||||||
|
formBuilder.add("base", base);
|
||||||
|
formBuilder.add("quote", quote);
|
||||||
|
formBuilder.add("amount", String.valueOf(amount));
|
||||||
|
RequestBody formBody = formBuilder.build();
|
||||||
|
Request addQuoteRequest = new Request.Builder()
|
||||||
|
.url(API_BASE_URL + "/" + exchange + "/add_quote")
|
||||||
|
.header("X-API-KEY", API_KEY)
|
||||||
|
.post(formBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response addQuoteResponse = httpClient.newCall(addQuoteRequest).execute()) {
|
||||||
|
if (!addQuoteResponse.isSuccessful()) {
|
||||||
|
if (addQuoteResponse.code() == 503 && retry) {
|
||||||
|
return addQuote(exchange, pair, amount,false);
|
||||||
|
}
|
||||||
|
throw new IOException("Unexpected code " + addQuoteResponse);
|
||||||
|
}
|
||||||
|
String addQuoteResponseBody = addQuoteResponse.body().string();
|
||||||
|
JsonElement jsonElement = JsonParser.parseString(addQuoteResponseBody);
|
||||||
|
if (!jsonElement.isJsonObject()) {
|
||||||
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
||||||
return null;
|
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 {"Success":"Quote added successfully"}
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue