Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ test {
}

dependencies {
compile project(':model')
compile project(':security')
implementation project(':model')
implementation project(':security')
implementation 'com.fasterxml.jackson.core:jackson-core'
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.core:jackson-annotations'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework:spring-context'
implementation 'org.slf4j:slf4j-api'
Expand All @@ -22,4 +23,4 @@ dependencies {
testImplementation 'org.junit.platform:junit-platform-suite-api'
testImplementation 'org.springframework.boot:spring-boot-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
}
12 changes: 12 additions & 0 deletions api/src/main/java/com/coinbase/exchange/api/accounts/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ public String getProfile_id() {
public void setProfile_id(String profile_id) {
this.profile_id = profile_id;
}

@Override
public String toString() {
return "Account{"
+ "id='" + id + '\''
+ ", currency='" + currency + '\''
+ ", balance=" + balance
+ ", available=" + available
+ ", hold=" + hold
+ ", profile_id='" + profile_id + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ public Detail getDetail() {
public void setDetail(Detail detail) {
this.detail = detail;
}

@Override
public String toString() {
return "AccountHistory{"
+ "id=" + id
+ ", created_at='" + created_at + '\''
+ ", amount=" + amount
+ ", balance=" + balance
+ ", type='" + type + '\''
+ ", detail=" + detail
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coinbase.exchange.api.accounts;

import com.coinbase.exchange.api.exchange.CoinbaseExchangeException;
import com.coinbase.exchange.model.Hold;
import com.coinbase.exchange.api.exchange.CoinbaseExchange;
import org.springframework.core.ParameterizedTypeReference;
Expand All @@ -19,23 +20,23 @@ public AccountService(final CoinbaseExchange exchange) {

public static final String ACCOUNTS_ENDPOINT = "/accounts";

public List<Account> getAccounts(){
public List<Account> getAccounts() throws CoinbaseExchangeException {
return exchange.getAsList(ACCOUNTS_ENDPOINT, new ParameterizedTypeReference<Account[]>(){});
}

public Account getAccount(String id) {
public Account getAccount(String id) throws CoinbaseExchangeException {
return exchange.get(ACCOUNTS_ENDPOINT + "/" + id, new ParameterizedTypeReference<Account>() {});
}

public List<AccountHistory> getAccountHistory(String accountId) {
public List<AccountHistory> getAccountHistory(String accountId) throws CoinbaseExchangeException {
String accountHistoryEndpoint = ACCOUNTS_ENDPOINT + "/" + accountId + "/ledger";
return exchange.getAsList(accountHistoryEndpoint, new ParameterizedTypeReference<AccountHistory[]>(){});
}

public List<AccountHistory> getPagedAccountHistory(String accountId,
String beforeOrAfter,
Integer pageNumber,
Integer limit) {
Integer limit) throws CoinbaseExchangeException {

String accountHistoryEndpoint = ACCOUNTS_ENDPOINT + "/" + accountId + "/ledger";
return exchange.pagedGetAsList(accountHistoryEndpoint,
Expand All @@ -45,15 +46,15 @@ public List<AccountHistory> getPagedAccountHistory(String accountId,
limit);
}

public List<Hold> getHolds(String accountId) {
public List<Hold> getHolds(String accountId) throws CoinbaseExchangeException {
String holdsEndpoint = ACCOUNTS_ENDPOINT + "/" + accountId + "/holds";
return exchange.getAsList(holdsEndpoint, new ParameterizedTypeReference<Hold[]>(){});
}

public List<Hold> getPagedHolds(String accountId,
String beforeOrAfter,
Integer pageNumber,
Integer limit) {
Integer limit) throws CoinbaseExchangeException {
String holdsEndpoint = ACCOUNTS_ENDPOINT + "/" + accountId + "/holds";
return exchange.pagedGetAsList(holdsEndpoint,
new ParameterizedTypeReference<Hold[]>(){},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coinbase.exchange.api.deposits;

import com.coinbase.exchange.api.exchange.CoinbaseExchangeException;
import com.coinbase.exchange.model.CoinbasePaymentRequest;
import com.coinbase.exchange.model.PaymentResponse;
import com.coinbase.exchange.api.exchange.CoinbaseExchange;
Expand Down Expand Up @@ -28,7 +29,8 @@ public DepositService(final CoinbaseExchange exchange) {
* @param paymentMethodId
* @return PaymentResponse
*/
public PaymentResponse depositViaPaymentMethod(BigDecimal amount, String currency, final String paymentMethodId) {
public PaymentResponse depositViaPaymentMethod(BigDecimal amount, String currency, final String paymentMethodId)
throws CoinbaseExchangeException {
CoinbasePaymentRequest coinbasePaymentRequest = new CoinbasePaymentRequest(amount, currency, paymentMethodId);
return exchange.post(DEPOSIT_ENDPOINT + PAYMENTS,
new ParameterizedTypeReference<PaymentResponse>(){},
Expand All @@ -41,7 +43,8 @@ public PaymentResponse depositViaPaymentMethod(BigDecimal amount, String currenc
* @param coinbaseAccountId
* @return PaymentResponse
*/
public PaymentResponse depositViaCoinbase(BigDecimal amount, String currency, String coinbaseAccountId) {
public PaymentResponse depositViaCoinbase(BigDecimal amount, String currency, String coinbaseAccountId)
throws CoinbaseExchangeException {
CoinbasePaymentRequest coinbasePaymentRequest = new CoinbasePaymentRequest(amount, currency, coinbaseAccountId);
return exchange.post(DEPOSIT_ENDPOINT + COINBASE_PAYMENT,
new ParameterizedTypeReference<PaymentResponse>(){},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@
public interface CoinbaseExchange {

String getBaseUrl();

<R> HttpEntity<String> securityHeaders(String endpoint, String method, String body);
<T> T get(String endpoint, ParameterizedTypeReference<T> type);
<T> T pagedGet(String endpoint, ParameterizedTypeReference<T> responseType, String beforeOrAfter, Integer pageNumber, Integer limit);
<T> List<T> getAsList(String endpoint, ParameterizedTypeReference<T[]> type);
<T> List<T> pagedGetAsList(String endpoint, ParameterizedTypeReference<T[]> responseType, String beforeOrAfter, Integer pageNumber, Integer limit);
<T, R> T post(String endpoint, ParameterizedTypeReference<T> type, R jsonObject);
<T> T delete(String endpoint, ParameterizedTypeReference<T> type);

<T> T get(String endpoint, ParameterizedTypeReference<T> type) throws CoinbaseExchangeException;

<T> T pagedGet(
String endpoint, ParameterizedTypeReference<T> responseType, String beforeOrAfter, Integer pageNumber,
Integer limit
) throws CoinbaseExchangeException;

<T> List<T> getAsList(String endpoint, ParameterizedTypeReference<T[]> type) throws CoinbaseExchangeException;

<T> List<T> pagedGetAsList(
String endpoint, ParameterizedTypeReference<T[]> responseType, String beforeOrAfter, Integer pageNumber,
Integer limit
) throws CoinbaseExchangeException;

<T, R> T post(String endpoint, ParameterizedTypeReference<T> type, R jsonObject) throws CoinbaseExchangeException;

<T> T delete(String endpoint, ParameterizedTypeReference<T> type) throws CoinbaseExchangeException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.coinbase.exchange.api.exchange;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.client.HttpClientErrorException;

public class CoinbaseExchangeException extends Exception {
public CoinbaseExchangeException(final String message, final Throwable cause) {
super(message, cause);
}

static CoinbaseExchangeException create(final HttpClientErrorException cause, final ObjectMapper objectMapper) {
String message = null;
try {
final Error error = objectMapper.readValue(cause.getResponseBodyAsString(), Error.class);
message = error.getMessage();
} catch (JsonProcessingException e) {
// ignore
}

return new CoinbaseExchangeException(message, cause);
}

private static class Error {
private String message;

public String getMessage() {
return message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CoinbaseExchangeImpl(final String publicKey,
}

@Override
public <T> T get(String resourcePath, ParameterizedTypeReference<T> responseType) {
public <T> T get(String resourcePath, ParameterizedTypeReference<T> responseType) throws CoinbaseExchangeException {
try {
ResponseEntity<T> responseEntity = restTemplate.exchange(getBaseUrl() + resourcePath,
HttpMethod.GET,
Expand All @@ -59,12 +59,13 @@ public <T> T get(String resourcePath, ParameterizedTypeReference<T> responseType
return responseEntity.getBody();
} catch (HttpClientErrorException ex) {
log.error("GET request Failed for '" + resourcePath + "': " + ex.getResponseBodyAsString());
throw CoinbaseExchangeException.create(ex, objectMapper);
}
return null;
}

@Override
public <T> List<T> getAsList(String resourcePath, ParameterizedTypeReference<T[]> responseType) {
public <T> List<T> getAsList(String resourcePath, ParameterizedTypeReference<T[]> responseType)
throws CoinbaseExchangeException {
T[] result = get(resourcePath, responseType);

return result == null ? emptyList() : Arrays.asList(result);
Expand All @@ -75,7 +76,7 @@ public <T> T pagedGet(String resourcePath,
ParameterizedTypeReference<T> responseType,
String beforeOrAfter,
Integer pageNumber,
Integer limit) {
Integer limit) throws CoinbaseExchangeException {
resourcePath += "?" + beforeOrAfter + "=" + pageNumber + "&limit=" + limit;
return get(resourcePath, responseType);
}
Expand All @@ -85,13 +86,14 @@ public <T> List<T> pagedGetAsList(String resourcePath,
ParameterizedTypeReference<T[]> responseType,
String beforeOrAfter,
Integer pageNumber,
Integer limit) {
Integer limit) throws CoinbaseExchangeException {
T[] result = pagedGet(resourcePath, responseType, beforeOrAfter, pageNumber, limit );
return result == null ? emptyList() : Arrays.asList(result);
}

@Override
public <T> T delete(String resourcePath, ParameterizedTypeReference<T> responseType) {
public <T> T delete(String resourcePath, ParameterizedTypeReference<T> responseType)
throws CoinbaseExchangeException {
try {
ResponseEntity<T> response = restTemplate.exchange(getBaseUrl() + resourcePath,
HttpMethod.DELETE,
Expand All @@ -100,12 +102,13 @@ public <T> T delete(String resourcePath, ParameterizedTypeReference<T> responseT
return response.getBody();
} catch (HttpClientErrorException ex) {
log.error("DELETE request Failed for '" + resourcePath + "': " + ex.getResponseBodyAsString());
throw CoinbaseExchangeException.create(ex, objectMapper);
}
return null;
}

@Override
public <T, R> T post(String resourcePath, ParameterizedTypeReference<T> responseType, R jsonObj) {
public <T, R> T post(String resourcePath, ParameterizedTypeReference<T> responseType, R jsonObj)
throws CoinbaseExchangeException {
String jsonBody = toJson(jsonObj);

try {
Expand All @@ -116,8 +119,8 @@ public <T, R> T post(String resourcePath, ParameterizedTypeReference<T> respons
return response.getBody();
} catch (HttpClientErrorException ex) {
log.error("POST request Failed for '" + resourcePath + "': " + ex.getResponseBodyAsString());
throw CoinbaseExchangeException.create(ex, objectMapper);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ public List<OrderItem> getAsks() {
public void setAsks(List<OrderItem> asks) {
this.asks = asks;
}

@Override
public String toString() {
return "MarketData{"
+ "sequence=" + sequence
+ ", bids=" + bids
+ ", asks=" + asks
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.coinbase.exchange.api.marketdata;

import com.coinbase.exchange.api.exchange.CoinbaseExchange;
import com.coinbase.exchange.api.exchange.CoinbaseExchangeException;
import org.springframework.core.ParameterizedTypeReference;

import java.util.List;
Expand All @@ -18,14 +19,14 @@ public MarketDataService(final CoinbaseExchange exchange) {

public static final String PRODUCT_ENDPOINT = "/products";

public MarketData getMarketDataOrderBook(String productId, int level) {
public MarketData getMarketDataOrderBook(String productId, int level) throws CoinbaseExchangeException {
String marketDataEndpoint = PRODUCT_ENDPOINT + "/" + productId + "/book";
if(level != 1)
marketDataEndpoint += "?level=" + level;
return exchange.get(marketDataEndpoint, new ParameterizedTypeReference<MarketData>(){});
}

public List<Trade> getTrades(String productId) {
public List<Trade> getTrades(String productId) throws CoinbaseExchangeException {
String tradesEndpoint = PRODUCT_ENDPOINT + "/" + productId + "/trades";
return exchange.getAsList(tradesEndpoint, new ParameterizedTypeReference<Trade[]>(){});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ public void setTime(String time) {
this.time = time;
}

@Override
public String toString() {
return "Message{"
+ "type='" + type + '\''
+ ", sequence=" + sequence
+ ", order_id='" + order_id + '\''
+ ", size=" + size
+ ", price=" + price
+ ", side='" + side + '\''
+ ", remaining_size=" + remaining_size
+ ", reason='" + reason + '\''
+ ", maker_order_id='" + maker_order_id + '\''
+ ", taker_order_id='" + taker_order_id + '\''
+ ", time='" + time + '\''
+ '}';
}
}
24 changes: 18 additions & 6 deletions api/src/main/java/com/coinbase/exchange/api/orders/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,24 @@ public void setSettled(Boolean settled) {
this.settled = settled;
}

@Override
public String toString() {
String orderString = getSide();
orderString += ": " + getProduct_id();
orderString += ": " + getPrice();
orderString += ": " + getSize();
return orderString;
return "Order{"
+ "id='" + id + '\''
+ ", size='" + size + '\''
+ ", price='" + price + '\''
+ ", product_id='" + product_id + '\''
+ ", side='" + side + '\''
+ ", stp='" + stp + '\''
+ ", type='" + type + '\''
+ ", time_in_force='" + time_in_force + '\''
+ ", post_only='" + post_only + '\''
+ ", created_at='" + created_at + '\''
+ ", fill_fees='" + fill_fees + '\''
+ ", filled_size='" + filled_size + '\''
+ ", executed_value='" + executed_value + '\''
+ ", status='" + status + '\''
+ ", settled=" + settled
+ '}';
}

}
Loading