Skip to content

Commit

Permalink
Merge pull request #37 from cody824/master
Browse files Browse the repository at this point in the history
support proxy
  • Loading branch information
TheCookieLab authored Nov 10, 2018
2 parents ac027e5 + 7951b70 commit 4a2f92e
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 53 deletions.
15 changes: 6 additions & 9 deletions src/main/java/com/cf/ExchangeService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package com.cf;

import com.cf.data.model.poloniex.PoloniexActiveLoanTypes;
import com.cf.data.model.poloniex.PoloniexChartData;
import com.cf.data.model.poloniex.PoloniexCompleteBalance;
import com.cf.data.model.poloniex.PoloniexFeeInfo;
import com.cf.data.model.poloniex.PoloniexOpenOrder;
import com.cf.data.model.poloniex.PoloniexOrderResult;
import com.cf.data.model.poloniex.PoloniexOrderTrade;
import com.cf.data.model.poloniex.PoloniexTicker;
import com.cf.data.model.poloniex.PoloniexTradeHistory;
import com.cf.data.model.poloniex.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -60,4 +53,8 @@ public interface ExchangeService {

public PoloniexActiveLoanTypes returnActiveLoans();

public PoloniexOrderStatus returnOrderStatus(String orderNumber);

public PoloniexWithdrawResult withdraw(String currency, BigDecimal amount, String address, String paymentId);

}
4 changes: 4 additions & 0 deletions src/main/java/com/cf/TradingAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface TradingAPIClient

public String returnOrderTrades(String orderNumber);

public String returnOrderStatus(String orderNumber);

public String cancelOrder(String orderNumber);

public String moveOrder(String orderNumber, BigDecimal rate);
Expand All @@ -28,6 +30,8 @@ public interface TradingAPIClient

public String buy(String currencyPair, BigDecimal buyPrice, BigDecimal amount, boolean fillOrKill, boolean immediateOrCancel, boolean postOnly);

public String withdraw(String currency, BigDecimal amount, String address, String paymentId);

// Lending APIs
public String returnActiveLoans();

Expand Down
28 changes: 20 additions & 8 deletions src/main/java/com/cf/client/HTTPClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.cf.client;

import java.io.IOException;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -14,12 +9,29 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;

/**
*
* @author David
*/
public class HTTPClient
{

private final HttpHost proxy;

public HTTPClient()
{
this.proxy = null;
}

public HTTPClient(HttpHost proxy)
{
this.proxy = proxy;
}


public String postHttp(String url, List<NameValuePair> params, List<NameValuePair> headers) throws IOException
{
HttpPost post = new HttpPost(url);
Expand All @@ -34,7 +46,7 @@ public String postHttp(String url, List<NameValuePair> params, List<NameValuePai
}
}

HttpClient httpClient = HttpClientBuilder.create().build();
HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build();
HttpResponse response = httpClient.execute(post);

HttpEntity entity = response.getEntity();
Expand All @@ -58,7 +70,7 @@ public String getHttp(String url, List<NameValuePair> headers) throws IOExceptio
}
}

HttpClient httpClient = HttpClientBuilder.create().build();
HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build();
HttpResponse response = httpClient.execute(request);

HttpEntity entity = response.getEntity();
Expand Down
76 changes: 64 additions & 12 deletions src/main/java/com/cf/client/poloniex/PoloniexExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@
import com.cf.PriceDataAPIClient;
import com.cf.TradingAPIClient;
import com.cf.data.map.poloniex.PoloniexDataMapper;
import com.cf.data.model.poloniex.PoloniexActiveLoanTypes;
import com.cf.data.model.poloniex.PoloniexChartData;
import com.cf.data.model.poloniex.PoloniexCompleteBalance;
import com.cf.data.model.poloniex.PoloniexFeeInfo;
import com.cf.data.model.poloniex.PoloniexOpenOrder;
import com.cf.data.model.poloniex.PoloniexOrderResult;
import com.cf.data.model.poloniex.PoloniexOrderTrade;
import com.cf.data.model.poloniex.PoloniexTicker;
import com.cf.data.model.poloniex.PoloniexTradeHistory;
import com.cf.data.model.poloniex.*;
import org.apache.http.HttpHost;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
*
* @author David
Expand All @@ -39,6 +32,12 @@ public PoloniexExchangeService(String apiKey, String apiSecret) {
this.mapper = new PoloniexDataMapper();
}

public PoloniexExchangeService(String apiKey, String apiSecret, HttpHost httpHost) {
this.publicClient = new PoloniexPublicAPIClient(httpHost);
this.tradingClient = new PoloniexTradingAPIClient(apiKey, apiSecret, httpHost);
this.mapper = new PoloniexDataMapper();
}

public PoloniexExchangeService(PriceDataAPIClient publicClient, TradingAPIClient tradingClient, PoloniexDataMapper mapper) {
this.publicClient = publicClient;
this.tradingClient = tradingClient;
Expand Down Expand Up @@ -225,6 +224,59 @@ public PoloniexActiveLoanTypes returnActiveLoans() {
return activeLoanTypes;
}

/**
* Returns order status for a given orderNumber
*
* @param orderNumber
* @return PoloniexOrderStatus
*/
@Override
public PoloniexOrderStatus returnOrderStatus(String orderNumber) {
long start = System.currentTimeMillis();
PoloniexOrderStatus orderStatus = null;
PoloniexOrderStatusCheck orderStatusCheck;
try {
String orderStatusStr = tradingClient.returnOrderStatus(orderNumber);
orderStatusCheck = mapper.mapOrderStatusCheck(orderStatusStr);
if (orderStatusCheck.success == 1) {
orderStatus = mapper.mapOrderStatus(orderStatusStr);
} else {
PoloniexOrderStatusError error = mapper.mapOrderStatusError(orderStatusStr);
orderStatus = new PoloniexOrderStatus(0, error.result.get("error"), null);
}
LOG.trace("Retrieved and mapped {} {} order status in {} ms", orderStatusStr, orderNumber, System.currentTimeMillis() - start);
return orderStatus;
} catch (Exception ex) {
LOG.error("Error retrieving order status for {} - {}", orderNumber, ex.getMessage());
}

return orderStatus;
}

/**
* Places a withdraw order
*
* @param currency Examples: USDT ETH
* @param amount the amount to withdraw
* @param address the address of currency
* @param paymentId For XMR withdrawals, you may optionally specify "paymentId".
* @return PoloniexWithdrawResult
*/
@Override
public PoloniexWithdrawResult withdraw(String currency, BigDecimal amount, String address, String paymentId) {
long start = System.currentTimeMillis();
PoloniexWithdrawResult withdrawResult = null;
try {
String withdrawResultStr = tradingClient.withdraw(currency, amount, address, paymentId);
withdrawResult = mapper.mapWithdrawResult(withdrawResultStr);
LOG.trace("Retrieved and mapped {} {} withdraw in {} ms", withdrawResultStr, currency, System.currentTimeMillis() - start);
return withdrawResult;
} catch (Exception ex) {
LOG.error("Error retrieving withdraw for {} - {}", currency, ex.getMessage());
}
return withdrawResult;
}

/**
* *
* Returns your open orders for a given currency pair
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.cf.data.model.poloniex.PoloniexLendingHistory;
import com.cf.data.model.poloniex.PoloniexLendingResult;
import com.cf.data.model.poloniex.PoloniexLoanOffer;
import org.apache.http.HttpHost;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -31,6 +32,12 @@ public PoloniexLendingService(String apiKey, String apiSecret)
this.mapper = new PoloniexDataMapper();
}

public PoloniexLendingService(String apiKey, String apiSecret, HttpHost proxy)
{
this.tradingClient = new PoloniexTradingAPIClient(apiKey, apiSecret, proxy);
this.mapper = new PoloniexDataMapper();
}

public PoloniexLendingService(TradingAPIClient tradingClient, PoloniexDataMapper mapper)
{
this.tradingClient = tradingClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.cf.PriceDataAPIClient;
import com.cf.client.HTTPClient;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.logging.log4j.LogManager;

import java.io.IOException;

/**
*
* @author David
Expand All @@ -20,6 +22,11 @@ public PoloniexPublicAPIClient()
this.client = new HTTPClient();
}

public PoloniexPublicAPIClient(HttpHost proxy)
{
this.client = new HTTPClient(proxy);
}

public PoloniexPublicAPIClient(HTTPClient client)
{
this.client = client;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/cf/client/poloniex/PoloniexTradingAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cf.TradingAPIClient;
import com.cf.client.HTTPClient;
import org.apache.commons.codec.binary.Hex;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -34,6 +35,12 @@ public PoloniexTradingAPIClient(String apiKey, String apiSecret) {
this.client = new HTTPClient();
}

public PoloniexTradingAPIClient(String apiKey, String apiSecret, HttpHost httpHost) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.client = new HTTPClient(httpHost);
}

@Override
public String returnBalances() {
return this.returnTradingAPICommandResults("returnBalances");
Expand Down Expand Up @@ -82,6 +89,13 @@ public String returnOrderTrades(String orderNumber) {
return returnTradingAPICommandResults("returnOrderTrades", additionalPostParams);
}

@Override
public String returnOrderStatus(String orderNumber) {
List<NameValuePair> additionalPostParams = new ArrayList<>();
additionalPostParams.add(new BasicNameValuePair("orderNumber", orderNumber));
return returnTradingAPICommandResults("returnOrderStatus", additionalPostParams);
}

@Override
public String cancelOrder(String orderNumber) {
List<NameValuePair> additionalPostParams = new ArrayList<>();
Expand Down Expand Up @@ -109,6 +123,18 @@ public String buy(String currencyPair, BigDecimal buyPrice, BigDecimal amount, b
return trade("buy", currencyPair, buyPrice, amount, fillOrKill, immediateOrCancel, postOnly);
}

@Override
public String withdraw(String currency, BigDecimal amount, String address, String paymentId) {
List<NameValuePair> additionalPostParams = new ArrayList<>();
additionalPostParams.add(new BasicNameValuePair("currency", currency));
additionalPostParams.add(new BasicNameValuePair("amount", amount.toPlainString()));
additionalPostParams.add(new BasicNameValuePair("address", address));
if (paymentId != null) {
additionalPostParams.add(new BasicNameValuePair("paymentId", paymentId));
}
return returnTradingAPICommandResults("withdraw", additionalPostParams);
}

// Lending APIs
@Override
public String returnActiveLoans() {
Expand Down
48 changes: 25 additions & 23 deletions src/main/java/com/cf/data/map/poloniex/PoloniexDataMapper.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
package com.cf.data.map.poloniex;

import com.cf.data.model.poloniex.PoloniexActiveLoanTypes;
import com.cf.data.model.poloniex.PoloniexChartData;
import com.cf.data.model.poloniex.PoloniexCompleteBalance;
import com.cf.data.model.poloniex.PoloniexFeeInfo;
import com.cf.data.model.poloniex.PoloniexLendingHistory;
import com.cf.data.model.poloniex.PoloniexLendingResult;
import com.cf.data.model.poloniex.PoloniexLoanOffer;
import com.cf.data.model.poloniex.PoloniexOpenOrder;
import com.cf.data.model.poloniex.PoloniexOrderResult;
import com.cf.data.model.poloniex.PoloniexOrderTrade;
import com.cf.data.model.poloniex.PoloniexTicker;
import com.cf.data.model.poloniex.PoloniexTradeHistory;
import com.cf.data.model.poloniex.*;
import com.cf.data.model.poloniex.deserialize.PoloniexChartDataDeserializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.time.ZoneOffset;
Expand All @@ -31,9 +16,6 @@
import java.util.*;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
*
* @author David
Expand Down Expand Up @@ -175,4 +157,24 @@ public PoloniexLendingResult mapLendingResult(String result) {
return plr;
}

public PoloniexOrderStatus mapOrderStatus(String result) {
return gson.fromJson(result, new TypeToken<PoloniexOrderStatus>() {
}.getType());
}

public PoloniexOrderStatusCheck mapOrderStatusCheck(String result) {
return gson.fromJson(result, new TypeToken<PoloniexOrderStatusCheck>() {
}.getType());
}

public PoloniexOrderStatusError mapOrderStatusError(String result) {
return gson.fromJson(result, new TypeToken<PoloniexOrderStatusError>() {
}.getType());
}

public PoloniexWithdrawResult mapWithdrawResult(String result) {
return gson.fromJson(result, new TypeToken<PoloniexWithdrawResult>() {
}.getType());
}

}
Loading

0 comments on commit 4a2f92e

Please sign in to comment.