From b75b1315574d702647c758c067e937ffc65211cf Mon Sep 17 00:00:00 2001 From: guodong Date: Mon, 29 Oct 2018 22:41:34 +0800 Subject: [PATCH] support proxy --- src/main/java/com/cf/client/HTTPClient.java | 26 +++++++++++++------ .../poloniex/PoloniexExchangeService.java | 23 ++++++++-------- .../poloniex/PoloniexLendingService.java | 6 +++++ .../poloniex/PoloniexPublicAPIClient.java | 11 +++++--- .../poloniex/PoloniexTradingAPIClient.java | 7 +++++ 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/cf/client/HTTPClient.java b/src/main/java/com/cf/client/HTTPClient.java index 8b08de6..e270365 100644 --- a/src/main/java/com/cf/client/HTTPClient.java +++ b/src/main/java/com/cf/client/HTTPClient.java @@ -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; @@ -14,12 +9,27 @@ 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 params, List headers) throws IOException { HttpPost post = new HttpPost(url); @@ -34,7 +44,7 @@ public String postHttp(String url, List params, List headers) throws IOExceptio } } - HttpClient httpClient = HttpClientBuilder.create().build(); + HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build(); HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); diff --git a/src/main/java/com/cf/client/poloniex/PoloniexExchangeService.java b/src/main/java/com/cf/client/poloniex/PoloniexExchangeService.java index c2926f6..a11fbab 100644 --- a/src/main/java/com/cf/client/poloniex/PoloniexExchangeService.java +++ b/src/main/java/com/cf/client/poloniex/PoloniexExchangeService.java @@ -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 @@ -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; diff --git a/src/main/java/com/cf/client/poloniex/PoloniexLendingService.java b/src/main/java/com/cf/client/poloniex/PoloniexLendingService.java index e381c78..4fecc50 100644 --- a/src/main/java/com/cf/client/poloniex/PoloniexLendingService.java +++ b/src/main/java/com/cf/client/poloniex/PoloniexLendingService.java @@ -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; @@ -31,6 +32,11 @@ 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; diff --git a/src/main/java/com/cf/client/poloniex/PoloniexPublicAPIClient.java b/src/main/java/com/cf/client/poloniex/PoloniexPublicAPIClient.java index 9c199d9..6c7bd3f 100644 --- a/src/main/java/com/cf/client/poloniex/PoloniexPublicAPIClient.java +++ b/src/main/java/com/cf/client/poloniex/PoloniexPublicAPIClient.java @@ -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 @@ -15,11 +17,14 @@ public class PoloniexPublicAPIClient implements PriceDataAPIClient private static final String PUBLIC_URL = "https://poloniex.com/public?"; private final HTTPClient client; - public PoloniexPublicAPIClient() - { + public PoloniexPublicAPIClient() { this.client = new HTTPClient(); } + public PoloniexPublicAPIClient(HttpHost proxy) { + this.client = new HTTPClient(proxy); + } + public PoloniexPublicAPIClient(HTTPClient client) { this.client = client; diff --git a/src/main/java/com/cf/client/poloniex/PoloniexTradingAPIClient.java b/src/main/java/com/cf/client/poloniex/PoloniexTradingAPIClient.java index 99eaf0a..da8c931 100644 --- a/src/main/java/com/cf/client/poloniex/PoloniexTradingAPIClient.java +++ b/src/main/java/com/cf/client/poloniex/PoloniexTradingAPIClient.java @@ -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; @@ -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");