All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.binance.api.client.impl.BinanceApiRestClientImpl Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package com.binance.api.client.impl;

import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.config.BinanceApiConfig;
import com.binance.api.client.constant.BinanceApiConstants;
import com.binance.api.client.domain.account.*;
import com.binance.api.client.domain.account.request.*;
import com.binance.api.client.domain.general.Asset;
import com.binance.api.client.domain.general.ExchangeInfo;
import com.binance.api.client.domain.market.*;
import retrofit2.Call;

import java.util.List;

import static com.binance.api.client.impl.BinanceApiServiceGenerator.createService;
import static com.binance.api.client.impl.BinanceApiServiceGenerator.executeSync;

/**
 * Implementation of Binance's REST API using Retrofit with synchronous/blocking
 * method calls.
 */
public class BinanceApiRestClientImpl implements BinanceApiRestClient {

	private final BinanceApiService binanceApiService;

	public BinanceApiRestClientImpl(String apiKey, String secret) {
		binanceApiService = createService(BinanceApiService.class, apiKey, secret);
	}

	// General endpoints

	@Override
	public void ping() {
		executeSync(binanceApiService.ping());
	}

	@Override
	public Long getServerTime() {
		return executeSync(binanceApiService.getServerTime()).getServerTime();
	}

	@Override
	public ExchangeInfo getExchangeInfo() {
		return executeSync(binanceApiService.getExchangeInfo());
	}

	@Override
	public List getAllAssets() {
		return executeSync(binanceApiService
				.getAllAssets(BinanceApiConfig.getAssetInfoApiBaseUrl() + "assetWithdraw/getAllAsset.html"));
	}

	// Market Data endpoints

	@Override
	public OrderBook getOrderBook(String symbol, Integer limit) {
		return executeSync(binanceApiService.getOrderBook(symbol, limit));
	}

	@Override
	public List getTrades(String symbol, Integer limit) {
		return executeSync(binanceApiService.getTrades(symbol, limit));
	}

	@Override
	public List getHistoricalTrades(String symbol, Integer limit, Long fromId) {
		return executeSync(binanceApiService.getHistoricalTrades(symbol, limit, fromId));
	}

	@Override
	public List getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime) {
		return executeSync(binanceApiService.getAggTrades(symbol, fromId, limit, startTime, endTime));
	}

	@Override
	public List getAggTrades(String symbol) {
		return getAggTrades(symbol, null, null, null, null);
	}

	@Override
	public List getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit,
			Long startTime, Long endTime) {
		return executeSync(
				binanceApiService.getCandlestickBars(symbol, interval.getIntervalId(), limit, startTime, endTime));
	}

  @Override
  public List getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit) {
      return getCandlestickBars(symbol, interval, limit, null, null);
  }

	@Override
	public List getCandlestickBars(String symbol, CandlestickInterval interval) {
		return getCandlestickBars(symbol, interval, null, null, null);
	}

	@Override
	public TickerStatistics get24HrPriceStatistics(String symbol) {
		return executeSync(binanceApiService.get24HrPriceStatistics(symbol));
	}

	@Override
	public List getAll24HrPriceStatistics() {
		return executeSync(binanceApiService.getAll24HrPriceStatistics());
	}

	@Override
	public TickerPrice getPrice(String symbol) {
		return executeSync(binanceApiService.getLatestPrice(symbol));
	}

	@Override
	public List getAllPrices() {
		return executeSync(binanceApiService.getLatestPrices());
	}

	@Override
	public List getBookTickers() {
		return executeSync(binanceApiService.getBookTickers());
	}

	@Override
	public NewOrderResponse newOrder(NewOrder order) {
		final Call call;
		if (order.getQuoteOrderQty() == null) {
			call = binanceApiService.newOrder(order.getSymbol(), order.getSide(), order.getType(),
					order.getTimeInForce(), order.getQuantity(), order.getPrice(), order.getNewClientOrderId(),
					order.getStopPrice(), order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(),
					order.getTimestamp());
		} else {
			call = binanceApiService.newOrderQuoteQty(order.getSymbol(), order.getSide(), order.getType(),
					order.getTimeInForce(), order.getQuoteOrderQty(), order.getPrice(), order.getNewClientOrderId(),
					order.getStopPrice(), order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(),
					order.getTimestamp());
		}
		return executeSync(call);
	}

	@Override
	public void newOrderTest(NewOrder order) {
		executeSync(binanceApiService.newOrderTest(order.getSymbol(), order.getSide(), order.getType(),
				order.getTimeInForce(), order.getQuantity(), order.getPrice(), order.getNewClientOrderId(),
				order.getStopPrice(), order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(),
				order.getTimestamp()));
	}

	// Account endpoints

	@Override
	public Order getOrderStatus(OrderStatusRequest orderStatusRequest) {
		return executeSync(binanceApiService.getOrderStatus(orderStatusRequest.getSymbol(),
				orderStatusRequest.getOrderId(), orderStatusRequest.getOrigClientOrderId(),
				orderStatusRequest.getRecvWindow(), orderStatusRequest.getTimestamp()));
	}

	@Override
	public CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest) {
		return executeSync(
				binanceApiService.cancelOrder(cancelOrderRequest.getSymbol(), cancelOrderRequest.getOrderId(),
						cancelOrderRequest.getOrigClientOrderId(), cancelOrderRequest.getNewClientOrderId(),
						cancelOrderRequest.getRecvWindow(), cancelOrderRequest.getTimestamp()));
	}

	@Override
	public List getOpenOrders(OrderRequest orderRequest) {
		return executeSync(binanceApiService.getOpenOrders(orderRequest.getSymbol(), orderRequest.getRecvWindow(),
				orderRequest.getTimestamp()));
	}

	@Override
	public List getAllOrders(AllOrdersRequest orderRequest) {
		return executeSync(binanceApiService.getAllOrders(orderRequest.getSymbol(), orderRequest.getOrderId(),
				orderRequest.getLimit(), orderRequest.getRecvWindow(), orderRequest.getTimestamp()));
	}

	@Override
	public NewOCOResponse newOCO(NewOCO oco) {
		return executeSync(binanceApiService.newOCO(oco.getSymbol(), oco.getListClientOrderId(), oco.getSide(),
				oco.getQuantity(), oco.getLimitClientOrderId(), oco.getPrice(), oco.getLimitIcebergQty(),
				oco.getStopClientOrderId(), oco.getStopPrice(), oco.getStopLimitPrice(), oco.getStopIcebergQty(),
				oco.getStopLimitTimeInForce(), oco.getNewOrderRespType(), oco.getRecvWindow(), oco.getTimestamp()));
	}

	@Override
	public CancelOrderListResponse cancelOrderList(CancelOrderListRequest cancelOrderListRequest) {
		return executeSync(binanceApiService.cancelOrderList(cancelOrderListRequest.getSymbol(), cancelOrderListRequest.getOrderListId(),
				cancelOrderListRequest.getListClientOrderId(), cancelOrderListRequest.getNewClientOrderId(),
				cancelOrderListRequest.getRecvWindow(), cancelOrderListRequest.getTimestamp()));
	}

	@Override
	public OrderList getOrderListStatus(OrderListStatusRequest orderListStatusRequest) {
		return executeSync(binanceApiService.getOrderListStatus(orderListStatusRequest.getOrderListId(), orderListStatusRequest.getOrigClientOrderId(),
				orderListStatusRequest.getRecvWindow(), orderListStatusRequest.getTimestamp()));
	}

	@Override
	public List getAllOrderList(AllOrderListRequest allOrderListRequest) {
		return executeSync(binanceApiService.getAllOrderList(allOrderListRequest.getFromId(), allOrderListRequest.getStartTime(),
				allOrderListRequest.getEndTime(), allOrderListRequest.getLimit(), allOrderListRequest.getRecvWindow(), allOrderListRequest.getTimestamp()));
	}

	@Override
	public Account getAccount(Long recvWindow, Long timestamp) {
		return executeSync(binanceApiService.getAccount(recvWindow, timestamp));
	}

	@Override
	public Account getAccount() {
		return getAccount(BinanceApiConstants.DEFAULT_RECEIVING_WINDOW, System.currentTimeMillis());
	}

	@Override
	public List getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp) {
		return executeSync(binanceApiService.getMyTrades(symbol, limit, fromId, recvWindow, timestamp));
	}

	@Override
	public List getMyTrades(String symbol, Integer limit) {
		return getMyTrades(symbol, limit, null, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW,
				System.currentTimeMillis());
	}

	@Override
	public List getMyTrades(String symbol) {
		return getMyTrades(symbol, null, null, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW,
				System.currentTimeMillis());
	}

	@Override
	public List getMyTrades(String symbol, Long fromId) {
		return getMyTrades(symbol, null, fromId, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW,
				System.currentTimeMillis());
	}

	@Override
	public WithdrawResult withdraw(String asset, String address, String amount, String name, String addressTag) {
		return executeSync(binanceApiService.withdraw(asset, address, amount, name, addressTag,
				BinanceApiConstants.DEFAULT_RECEIVING_WINDOW, System.currentTimeMillis()));
	}

	@Override
	public DustTransferResponse dustTranfer(List asset) {
		return executeSync(binanceApiService.dustTransfer(asset, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW, System.currentTimeMillis()));
	}

	@Override
	public DepositHistory getDepositHistory(String asset) {
		return executeSync(binanceApiService.getDepositHistory(asset, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW,
				System.currentTimeMillis()));
	}

	@Override
	public WithdrawHistory getWithdrawHistory(String asset) {
		return executeSync(binanceApiService.getWithdrawHistory(asset, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW,
				System.currentTimeMillis()));
	}

	@Override
	public List getSubAccountTransfers() {
		return executeSync(binanceApiService.getSubAccountTransfers(System.currentTimeMillis()));
	}

	@Override
	public DepositAddress getDepositAddress(String asset) {
		return executeSync(binanceApiService.getDepositAddress(asset, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW,
				System.currentTimeMillis()));
	}

	// User stream endpoints

	@Override
	public String startUserDataStream() {
		return executeSync(binanceApiService.startUserDataStream()).toString();
	}

	@Override
	public void keepAliveUserDataStream(String listenKey) {
		executeSync(binanceApiService.keepAliveUserDataStream(listenKey));
	}

	@Override
	public void closeUserDataStream(String listenKey) {
		executeSync(binanceApiService.closeAliveUserDataStream(listenKey));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy