org.erc.coinbase.pro.rest.RESTClient Maven / Gradle / Ivy
The newest version!
/**
This file is part of coinbase-pro-client.
coinbase-pro-client is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with coinbase-pro-client. If not, see .
*/
package org.erc.coinbase.pro.rest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.erc.coinbase.pro.Client;
import org.erc.coinbase.pro.exceptions.CoinbaseException;
import org.erc.coinbase.pro.exceptions.RequiredParameterException;
import org.erc.coinbase.pro.exceptions.SignatureException;
import org.erc.coinbase.pro.model.*;
import com.fasterxml.jackson.core.type.TypeReference;
/**
* The Class Client.
*/
public class RESTClient implements Client {
/** The http. */
private HTTPRest http;
/**
* Instantiates a new client.
*
* @param config the client configuration
* @throws SignatureException Signature Exception
*/
public RESTClient(ClientConfig config) throws SignatureException {
http = new HTTPRest(config.getBaseUrl(),config.getPublicKey(),config.getSecretKey(),config.getPassphrase());
if(config.getProxy()!=null) {
http.setProxyConfig(config.getProxy());
}
}
/**
* Inits the parameters.
*
* @param filter the pagination filter
* @return the map
*/
private Map initParameters(PaginationFilter filter){
Map params = new HashMap<>();
if(filter!=null) {
putIfAbsent(params, "before", filter.getBefore());
putIfAbsent(params, "after", filter.getAfter());
if(filter.getLimit()>0) {
params.put("limit", filter.getLimit());
}
}
return params;
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getAccounts(org.erc.coinbase.pro.model.AccountFilter)
*/
@Override
public List getAccounts(AccountFilter filter) throws CoinbaseException {
Map params = initParameters(filter);
return http.get("/accounts", new TypeReference>() {},params, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getAccount(java.lang.String)
*/
@Override
public Account getAccount(String id) throws CoinbaseException {
assertRequired("accountId",id);
return http.get(String.format("/account/%s",id), new TypeReference() {},null, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getAccountHistory(org.erc.coinbase.pro.model.AccountHistoryFilter)
*/
@Override
public List getAccountHistory(AccountHistoryFilter filter) throws CoinbaseException {
assertRequired("filter",filter);
assertRequired("accountId",filter.getAccountId());
Map params = initParameters(filter);
return http.get(String.format("/account/%s/ledger",filter.getAccountId()), new TypeReference>() {},params, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getAccountHolds(org.erc.coinbase.pro.model.AccountHoldFilter)
*/
@Override
public List getAccountHolds(AccountHoldFilter filter) throws CoinbaseException {
assertRequired("filter",filter);
assertRequired("accountId",filter.getAccountId());
Map params = initParameters(filter);
return http.get(String.format("/account/%s/holds",filter.getAccountId()), new TypeReference>() {},params, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#placeOrder(org.erc.coinbase.pro.model.OrderRequest)
*/
@Override
public Order placeOrder(OrderRequest request) throws CoinbaseException {
return http.post("/orders", new TypeReference() {}, request);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#cancelOrder(java.lang.String)
*/
@Override
public void cancelOrder(String id) throws CoinbaseException {
assertRequired("orderId",id);
http.delete(String.format("/orders/%s", id),null,null);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#cancelAllOrders(java.lang.String)
*/
@Override
public List cancelAllOrders(String productId) throws CoinbaseException {
Map filter = new HashMap<>();
putIfAbsent(filter, "product_id", productId);
return http.delete("/orders",new TypeReference>() {},filter);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getOrders(org.erc.coinbase.pro.model.OrderFilter)
*/
@Override
public List getOrders(OrderFilter orderFilter) throws CoinbaseException {
Map filter = new HashMap<>();
if(orderFilter!=null) {
putIfAbsent(filter, "product_id", orderFilter.getProductId());
putIfAbsent(filter, "status", orderFilter.getStatus());
}
return http.get("/orders", new TypeReference>() {},filter, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getOrder(java.lang.String)
*/
@Override
public Order getOrder(String id) throws CoinbaseException {
assertRequired("order-id",id);
return http.get(String.format("/orders/%s",id), new TypeReference() {},null, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getFills(org.erc.coinbase.pro.model.FillFilter)
*/
@Override
public List getFills(FillFilter fillFilter) throws CoinbaseException {
assertRequired("filter",fillFilter);
if(fillFilter.getOrderId() == null && fillFilter.getProductId() == null) {
throw new RequiredParameterException("order_id or product_id");
}
Map filter = initParameters(fillFilter);
putIfAbsent(filter, "order_id", fillFilter.getOrderId());
putIfAbsent(filter, "product_id", fillFilter.getProductId());
return http.get("/fills", new TypeReference>() {},filter, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#deposit(org.erc.coinbase.pro.model.DepositRequest)
*/
@Override
public Deposit deposit(DepositRequest deposit) throws CoinbaseException {
assertRequired("deposit",deposit);
if( deposit instanceof DepositCoinbaseRequest) {
return http.post("/deposits/coinbase-account", new TypeReference() {}, deposit);
} else if (deposit instanceof DepositPaymentMethodRequest) {
return http.post("/deposits/payment-method", new TypeReference() {}, deposit);
} else {
throw new RequiredParameterException("DepositCoinbaseRequest or DepositPaymentMethodRequest");
}
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#withdrawal(org.erc.coinbase.pro.model.WithdrawalRequest)
*/
@Override
public Withdrawal withdrawal(WithdrawalRequest withdrawal) throws CoinbaseException {
assertRequired("request",withdrawal);
if (withdrawal instanceof WithdrawalRequestCoinbase) {
return http.post("/withdrawals/coinbase-account", new TypeReference() {}, withdrawal);
} else if (withdrawal instanceof WithdrawalRequestPaymentMethod) {
return http.post("/withdrawals/payment-method", new TypeReference() {}, withdrawal);
} else if (withdrawal instanceof WithdrawalRequestCrypto) {
return http.post("/withdrawals/crypto", new TypeReference() {}, withdrawal);
} else {
throw new RequiredParameterException("WithdrawalRequestCoinbase or WithdrawalRequestPaymentMethod or WithdrawalRequestCrypto");
}
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getPaymentMethods()
*/
@Override
public List getPaymentMethods() throws CoinbaseException {
return http.get("/payment-methods", new TypeReference>() {},null, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getCoinbaseAccounts()
*/
@Override
public List getCoinbaseAccounts() throws CoinbaseException {
return http.get("/coinbase-accounts", new TypeReference>() {},null, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#createReport(org.erc.coinbase.pro.model.ReportRequest)
*/
@Override
public Report createReport(ReportRequest request) throws CoinbaseException {
return http.post("/reports", new TypeReference() {}, request);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getReportStatus(java.lang.String)
*/
@Override
public Report getReportStatus(String id) throws CoinbaseException {
assertRequired("report_id",id);
return http.get(String.format("/reports/%s",id), new TypeReference() {},null, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getTrailingVolume()
*/
@Override
public List getTrailingVolume() throws CoinbaseException {
return http.get("/users/self/trailing-volume", new TypeReference>() {},null, true);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getProducts(org.erc.coinbase.pro.model.ProductsFilter)
*/
@Override
public List getProducts(ProductsFilter productFilter) throws CoinbaseException {
Map filter = new HashMap<>();
if(productFilter!=null) {
putIfAbsent(filter, "base_min_size", productFilter.getMinSize());
putIfAbsent(filter, "base_max_size", productFilter.getMaxSize());
putIfAbsent(filter, "quote_increment", productFilter.getIncrement());
}
return http.get("/products", new TypeReference>() {},filter, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getProductOrderBook(java.lang.String, int)
*/
@Override
public Book getProductOrderBook(String productId,int level) throws CoinbaseException {
if(level<1 || level > 3) {
level = 1;
}
assertRequired("productId",productId);
return http.get(String.format("/products/%s/book?level=%s",productId,level), new TypeReference() {},null, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getProductTicker(java.lang.String)
*/
@Override
public Ticker getProductTicker(String productId) throws CoinbaseException {
assertRequired("productId",productId);
return http.get(String.format("/products/%s/ticker",productId), new TypeReference() {},null, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getProductTrades(org.erc.coinbase.pro.model.ProductTradesFilter)
*/
@Override
public List getProductTrades(ProductTradesFilter productFilter) throws CoinbaseException{
assertRequired("filter",productFilter);
assertRequired("productId",productFilter.getProductId());
Map params = initParameters(productFilter);
return http.get(String.format("/products/%s/trades",productFilter.getProductId()), new TypeReference>() {},params, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getProductHistoricRate(org.erc.coinbase.pro.model.ProductCandleFilter)
*/
@Override
public List getProductHistoricRate(ProductCandleFilter request) throws CoinbaseException{
assertRequired("filter",request);
assertRequired("productId",request.getProductId());
Map filter = new HashMap<>();
putIfAbsent(filter, "start", request.getStart());
putIfAbsent(filter, "end", request.getEnd());
if(request.getGranularity()!=null) {
putIfAbsent(filter, "granularity", request.getGranularity().getSeconds());
}
return http.get(String.format("/products/%s/trades",request.getProductId()), new TypeReference>() {},filter, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getProductStats(java.lang.String)
*/
@Override
public Stats getProductStats(String productId) throws CoinbaseException{
assertRequired("productId",productId);
return http.get(String.format("/products/%s/stats",productId), new TypeReference() {},null, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getCurrencies()
*/
@Override
public List getCurrencies() throws CoinbaseException{
return http.get("/currencies", new TypeReference>() {},null, false);
}
/* (non-Javadoc)
* @see org.erc.coinbase.pro.rest.Client#getTime()
*/
@Override
public Date getTime() throws CoinbaseException{
Time time = http.get("/time", new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy