Skip to content

Commit

Permalink
Release 3.0.0
Browse files Browse the repository at this point in the history
* Release 3.0.0 - Implement HTTP Proxy

* Removed unnecessary `secretKey` parameter from `UserData` classes

---------

Co-authored-by: Radu Linu <radu.linu@mateco.eu>
Co-authored-by: Jonte <114036008+jonte-z@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 6, 2023
1 parent 5bebbb9 commit e94098d
Show file tree
Hide file tree
Showing 27 changed files with 346 additions and 57 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 3.0.0 - 2023-03-06

### Added
- HTTP Proxy Support

### Updated
- Removed unnecessary `secretKey` parameter from `UserData` classes

## 2.0.0 - 2023-01-12

### Fixed
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,54 @@ output:
```
INFO: {"data":"{"serverTime":1633434339494}","x-mbx-used-weight":"1","x-mbx-used-weight-1m":"1"}
```

### Proxy
HTTP Proxy is supported.

To set it up, call `setProxy()` with `ProxyAuth` and before submitting requests to binance:

```java
CMFuturesClientImpl client = new CMFuturesClientImpl();
Proxy proxyConn = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080));
ProxyAuth proxy = new ProxyAuth(proxyConn, null);

client.setProxy(proxy);
logger.info(client.market().time());
```

For authenticated `Proxy`, define `ProxyAuth` with [`Authenticator` from `okhttp3`](https://square.github.io/okhttp/3.x/okhttp/index.html?okhttp3/Authenticator.html):

```java
CMFuturesClientImpl client = new CMFuturesClientImpl();
Proxy proxyConn = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080));
Authenticator auth = new Authenticator() {
public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Proxy-Authorization") != null) {
return null; // Give up, we've already failed to authenticate.
}

String credential = Credentials.basic("username", "password");
return response.request().newBuilder().header("Proxy-Authorization", credential).build();

}
};
ProxyAuth proxy = new ProxyAuth(proxyConn, auth);

client.setProxy(proxy);
logger.info(client.market().time());
```

To undo `Proxy`, use `unsetProxy()` before submitting requests to binance:

```java
client.unsetProxy();
logger.info(client.market().time());
```

Complete examples are available to the following folders:
- `test/java/examples/cm_futures/proxy`
- `test/java/examples/um_futures/proxy`

### Logging

`ch.qos.logback` is used for logging in this connector. The configuration xml file can be found under
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.binance</groupId>
<artifactId>binance-futures-connector-java</artifactId>
<version>2.0.0</version>
<version>3.0.0</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>lightweight connector to Futures API</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ public CMFuturesClientImpl(String apiKey, String secretKey, String baseUrl) {

@Override
public CMMarket market() {
return new CMMarket(getProductUrl(), getBaseUrl(), getApiKey(), getShowLimitUsage());
return new CMMarket(getProductUrl(), getBaseUrl(), getApiKey(), getShowLimitUsage(), getProxy());
}

@Override
public CMAccount account() {
return new CMAccount(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage());
return new CMAccount(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
}

@Override
public CMUserData userData() {
return new CMUserData(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage());
return new CMUserData(getProductUrl(), getApiKey(), getShowLimitUsage(), getProxy());
}

@Override
public CMPortfolioMargin portfolioMargin() {
return new CMPortfolioMargin(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage());
return new CMPortfolioMargin(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.binance.connector.futures.client.impl;

import com.binance.connector.futures.client.FuturesClient;
import com.binance.connector.futures.client.utils.ProxyAuth;

public abstract class FuturesClientImpl implements FuturesClient {
private final String apiKey;
private final String secretKey;
private final String baseUrl;
private final String productUrl;
private boolean showLimitUsage;
private ProxyAuth proxy = null;

public FuturesClientImpl(String baseUrl, String product) {
this(null, null, baseUrl, product);
Expand Down Expand Up @@ -53,4 +55,16 @@ public void setShowLimitUsage(boolean showLimitUsage) {
this.showLimitUsage = showLimitUsage;
}

public void setProxy(ProxyAuth proxy) {
this.proxy = proxy;
}

public ProxyAuth getProxy() {
return proxy;
}

public void unsetProxy() {
this.proxy = null;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.binance.connector.futures.client.impl;

import com.binance.connector.futures.client.enums.DefaultUrls;
import com.binance.connector.futures.client.impl.um_futures.UMMarket;
import com.binance.connector.futures.client.impl.um_futures.UMAccount;
import com.binance.connector.futures.client.impl.um_futures.UMUserData;
import com.binance.connector.futures.client.impl.um_futures.UMMarket;
import com.binance.connector.futures.client.impl.um_futures.UMPortfolioMargin;
import com.binance.connector.futures.client.impl.um_futures.UMUserData;

public class UMFuturesClientImpl extends FuturesClientImpl {
private static String defaultBaseUrl = DefaultUrls.USDM_PROD_URL;
Expand Down Expand Up @@ -36,21 +36,21 @@ public UMFuturesClientImpl(String apiKey, String secretKey, String baseUrl) {

@Override
public UMMarket market() {
return new UMMarket(getProductUrl(), getBaseUrl(), getApiKey(), getShowLimitUsage());
return new UMMarket(getProductUrl(), getBaseUrl(), getApiKey(), getShowLimitUsage(), getProxy());
}

@Override
public UMAccount account() {
return new UMAccount(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage());
return new UMAccount(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
}

@Override
public UMUserData userData() {
return new UMUserData(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage());
return new UMUserData(getProductUrl(), getApiKey(), getShowLimitUsage(), getProxy());
}

@Override
public UMPortfolioMargin portfolioMargin() {
return new UMPortfolioMargin(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage());
return new UMPortfolioMargin(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.utils.ParameterChecker;
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.Account;
import com.binance.connector.futures.client.utils.ProxyAuth;

/**
* <h2>Coin-Margined Trade Endpoints</h2>
Expand All @@ -14,8 +15,8 @@
* Response will be returned in <i>String format</i>.
*/
public class CMAccount extends Account {
public CMAccount(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
super(productUrl, apiKey, secretKey, showLimitUsage);
public CMAccount(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, secretKey, showLimitUsage, proxy);
}

private final String ORDER = "/v1/order";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.utils.ParameterChecker;
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.Market;
import com.binance.connector.futures.client.utils.ProxyAuth;

/**
* <h2>Coin-Margined Market Endpoints</h2>
Expand All @@ -14,8 +15,8 @@
* Response will be returned in <i>String format</i>.
*/
public class CMMarket extends Market {
public CMMarket(String productUrl, String baseUrl, String apiKey, boolean showLimitUsage) {
super(productUrl, baseUrl, apiKey, showLimitUsage);
public CMMarket(String productUrl, String baseUrl, String apiKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, baseUrl, apiKey, showLimitUsage, proxy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.PortfolioMargin;
import com.binance.connector.futures.client.utils.ProxyAuth;

/**
* <h2>Coin-Margined Portfolio Margin Endpoints</h2>
Expand All @@ -12,8 +13,8 @@
* Response will be returned in <i>String format</i>.
*/
public class CMPortfolioMargin extends PortfolioMargin {
public CMPortfolioMargin(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
super(productUrl, apiKey, secretKey, showLimitUsage);
public CMPortfolioMargin(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, secretKey, showLimitUsage, proxy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.binance.connector.futures.client.impl.cm_futures;

import com.binance.connector.futures.client.impl.futures.UserData;
import com.binance.connector.futures.client.utils.ProxyAuth;

/**
* <h2>Coin-Margined User Data Streams Endpoints</h2>
Expand All @@ -11,7 +12,7 @@
* Response will be returned in <i>String format</i>.
*/
public class CMUserData extends UserData {
public CMUserData(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
super(productUrl, apiKey, secretKey, showLimitUsage);
public CMUserData(String productUrl, String apiKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, showLimitUsage, proxy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.binance.connector.futures.client.enums.HttpMethod;
import com.binance.connector.futures.client.utils.ParameterChecker;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;
import java.util.LinkedHashMap;

Expand All @@ -14,9 +15,9 @@ public abstract class Account {
private RequestHandler requestHandler;
private boolean showLimitUsage;

public Account(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
public Account(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
this.productUrl = productUrl;
this.requestHandler = new RequestHandler(apiKey, secretKey);
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
this.showLimitUsage = showLimitUsage;
}

Expand All @@ -36,8 +37,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey, String secretKey) {
this.requestHandler = new RequestHandler(apiKey, secretKey);
public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.binance.connector.futures.client.enums.HttpMethod;
import com.binance.connector.futures.client.utils.ParameterChecker;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;
import java.util.LinkedHashMap;

Expand All @@ -15,10 +16,10 @@ public abstract class Market {
private RequestHandler requestHandler;
private boolean showLimitUsage;

public Market(String productUrl, String baseUrl, String apiKey, boolean showLimitUsage) {
public Market(String productUrl, String baseUrl, String apiKey, boolean showLimitUsage, ProxyAuth proxy) {
this.baseUrl = baseUrl;
this.productUrl = productUrl;
this.requestHandler = new RequestHandler(apiKey);
this.requestHandler = new RequestHandler(apiKey, proxy);
this.showLimitUsage = showLimitUsage;
}

Expand Down Expand Up @@ -46,8 +47,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey, String secretKey) {
new RequestHandler(apiKey, secretKey);
public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
new RequestHandler(apiKey, secretKey, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.binance.connector.futures.client.impl.futures;

import com.binance.connector.futures.client.enums.HttpMethod;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;
import com.binance.connector.futures.client.utils.ParameterChecker;
import java.util.LinkedHashMap;
Expand All @@ -14,9 +15,9 @@ public abstract class PortfolioMargin {
private RequestHandler requestHandler;
private boolean showLimitUsage;

public PortfolioMargin(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
public PortfolioMargin(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
this.productUrl = productUrl;
this.requestHandler = new RequestHandler(apiKey, secretKey);
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
this.showLimitUsage = showLimitUsage;
}

Expand All @@ -36,8 +37,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey, String secretKey) {
this.requestHandler = new RequestHandler(apiKey, secretKey);
public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.binance.connector.futures.client.impl.futures;

import com.binance.connector.futures.client.enums.HttpMethod;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;

/**
Expand All @@ -12,9 +13,9 @@ public abstract class UserData {
private RequestHandler requestHandler;
private boolean showLimitUsage;

public UserData(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
public UserData(String productUrl, String apiKey, boolean showLimitUsage, ProxyAuth proxy) {
this.productUrl = productUrl;
this.requestHandler = new RequestHandler(apiKey);
this.requestHandler = new RequestHandler(apiKey, proxy);
this.showLimitUsage = showLimitUsage;
}

Expand All @@ -34,8 +35,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey) {
this.requestHandler = new RequestHandler(apiKey);
public void setRequestHandler(String apiKey, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.utils.ParameterChecker;
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.Account;
import com.binance.connector.futures.client.utils.ProxyAuth;

/**
* <h2>USDⓈ-Margined Trade Endpoints</h2>
Expand All @@ -14,8 +15,8 @@
* Response will be returned in <i>String format</i>.
*/
public class UMAccount extends Account {
public UMAccount(String productUrl, String apiKey, String secretKey, boolean showLimitUsage) {
super(productUrl, apiKey, secretKey, showLimitUsage);
public UMAccount(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, secretKey, showLimitUsage, proxy);
}

private final String MULTI_ASSETS_MARGIN = "/v1/multiAssetsMargin";
Expand Down
Loading

0 comments on commit e94098d

Please sign in to comment.