2017-06-30 23:24:19 +02:00
|
|
|
package nl.komtek.gpi.controllers;
|
|
|
|
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import nl.komtek.gpi.services.GunbotProxyService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2017-07-04 00:23:39 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2017-06-30 23:24:19 +02:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Elroy on 17-6-2017.
|
|
|
|
*/
|
|
|
|
@Controller
|
|
|
|
public class GunbotProxyController {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GunbotProxyService gunbotProxyService;
|
2017-07-04 00:23:39 +02:00
|
|
|
@Value("${doubleBuyProtection:false}")
|
|
|
|
private boolean doubleBuyProtection;
|
2017-06-30 23:24:19 +02:00
|
|
|
|
|
|
|
@RequestMapping(value = "/public/**")
|
|
|
|
@ResponseBody
|
|
|
|
public String interceptAllCalls(HttpServletRequest request) {
|
|
|
|
System.out.println("intercepted -- " + request.getRequestURL() + "?" + request.getQueryString() + "?command=" + request.getParameter("command"));
|
|
|
|
return "intercepted";
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/public/**", params = "command=returnChartData")
|
|
|
|
@ResponseBody
|
|
|
|
public String publicRequestChartData(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam String start,
|
|
|
|
@RequestParam long period) throws InterruptedException {
|
|
|
|
|
|
|
|
return gunbotProxyService.getChartData(currencyPair, start, period);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/public/**", params = "command=returnTicker")
|
|
|
|
@ResponseBody
|
|
|
|
public String publicRequestTicker() {
|
|
|
|
return gunbotProxyService.getTicker();
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequests(HttpServletRequest request) {
|
|
|
|
System.out.println(request.getRequestURL() + "??command=" + request.getParameter("command"));
|
|
|
|
request.getParameterMap().keySet().stream().forEach((e) -> System.out.print(e + "-"));
|
|
|
|
return "trading api";
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=returnCompleteBalances")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestCompleteBalances(HttpServletRequest request) {
|
2017-07-04 00:23:39 +02:00
|
|
|
|
|
|
|
String market = "default";
|
|
|
|
if (gunbotProxyService.isUsingMultipleMarkets()) {
|
|
|
|
String key = request.getHeader("key");
|
|
|
|
market = gunbotProxyService.getMarket(key);
|
|
|
|
}
|
|
|
|
return gunbotProxyService.getCompleteBalances(market);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=returnOpenOrders")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestOpenOrders(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair) {
|
2017-07-04 00:23:39 +02:00
|
|
|
String market = "default";
|
|
|
|
if (gunbotProxyService.isUsingMultipleMarkets()) {
|
|
|
|
String key = request.getHeader("key");
|
|
|
|
market = gunbotProxyService.getMarket(key);
|
|
|
|
}
|
|
|
|
String result = gunbotProxyService.getOpenOrders(market);
|
2017-06-30 23:24:19 +02:00
|
|
|
|
|
|
|
JsonElement jelement = new JsonParser().parse(result);
|
|
|
|
JsonObject jobject = jelement.getAsJsonObject();
|
|
|
|
JsonArray jarray = jobject.getAsJsonArray(currencyPair);
|
2017-07-01 22:30:06 +02:00
|
|
|
return jarray != null ? jarray.toString() : "[]";
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=returnTradeHistory")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestTradeHistory(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair) {
|
|
|
|
|
2017-07-04 00:23:39 +02:00
|
|
|
String market = "default";
|
|
|
|
if (gunbotProxyService.isUsingMultipleMarkets()) {
|
|
|
|
String key = request.getHeader("key");
|
|
|
|
market = gunbotProxyService.getMarket(key);
|
|
|
|
}
|
|
|
|
String result = gunbotProxyService.getTradeHistory(market);
|
2017-06-30 23:24:19 +02:00
|
|
|
|
2017-07-05 10:35:56 +02:00
|
|
|
if (result.equals("[]")){
|
|
|
|
return result;
|
|
|
|
}
|
2017-06-30 23:24:19 +02:00
|
|
|
JsonElement jelement = new JsonParser().parse(result);
|
|
|
|
JsonObject jobject = jelement.getAsJsonObject();
|
|
|
|
JsonArray jarray = jobject.getAsJsonArray(currencyPair);
|
2017-07-01 22:30:06 +02:00
|
|
|
return jarray != null ? jarray.toString() : "[]";
|
2017-06-30 23:24:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=cancelOrder")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestCancelOrder(HttpServletRequest request,
|
|
|
|
@RequestParam String orderNumber) {
|
|
|
|
|
2017-07-04 00:23:39 +02:00
|
|
|
String key = request.getHeader("key");
|
|
|
|
return gunbotProxyService.cancelOrder(key, orderNumber);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=sell")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestSell(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam BigDecimal rate,
|
|
|
|
@RequestParam BigDecimal amount) {
|
2017-07-04 00:23:39 +02:00
|
|
|
String key = request.getHeader("key");
|
|
|
|
return gunbotProxyService.sellOrder(key, currencyPair, rate, amount);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=buy")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestBuy(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam BigDecimal rate,
|
|
|
|
@RequestParam BigDecimal amount) {
|
|
|
|
|
2017-07-04 00:23:39 +02:00
|
|
|
String key = request.getHeader("key");
|
|
|
|
if (doubleBuyProtection) {
|
|
|
|
return gunbotProxyService.buyOrderWithProtection(key, currencyPair, rate, amount);
|
|
|
|
} else {
|
|
|
|
return gunbotProxyService.buyOrder(key, currencyPair, rate, amount);
|
|
|
|
}
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
}
|