Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement functionality to get product stats. #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.coinbase.exchange.api.products;

import com.coinbase.exchange.api.exchange.CoinbaseExchange;
import com.coinbase.exchange.model.Candles;
import com.coinbase.exchange.model.Granularity;
import com.coinbase.exchange.model.Product;
import com.coinbase.exchange.model.*;
import org.springframework.core.ParameterizedTypeReference;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -47,7 +43,8 @@ public Candles getCandles(String productId, Map<String, String> queryParams) {
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(joining("&")));
}
return new Candles(exchange.get(url.toString(), new ParameterizedTypeReference<List<String[]>>() {}));
return new Candles(exchange.get(url.toString(), new ParameterizedTypeReference<List<String[]>>() {
}));
}

/**
Expand Down Expand Up @@ -79,9 +76,26 @@ public Candles getCandles(String productId, Granularity granularity) {
}

/**
* If either one of the start or end fields are not provided then both fields will be ignored.
* If either one of the start or end fields are not provided then both fields will be ignored.
*/
public Candles getCandles(String productId, Instant start, Instant end) {
return getCandles(productId, start, end, null);
}
}

public ProductStats getProductStats(String productId) {
return exchange.get(PRODUCTS_ENDPOINT + "/" + productId + "/stats", new ParameterizedTypeReference<ProductStats>() {
});
}

public List<Stats> getStats() {
Map<String, Stats> statsMap = exchange.get(PRODUCTS_ENDPOINT + "/stats", new ParameterizedTypeReference<Map<String, Stats>>() {
});

List<String> products = new ArrayList<>(statsMap.keySet());
for (String product : products) {
statsMap.get(product).setProductId(product);
}

return new LinkedList<>(statsMap.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.coinbase.exchange.api.BaseIntegrationTest;
import com.coinbase.exchange.api.config.IntegrationTestConfiguration;
import com.coinbase.exchange.model.Candles;
import com.coinbase.exchange.model.Granularity;
import com.coinbase.exchange.model.Product;
import com.coinbase.exchange.model.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -117,6 +116,18 @@ void shouldGetCandlesForWithAStartAndEndTime() {
assertTrue(candles.getCandleList().size() > 100);
}

@Test
void canGetStats() {
ProductStats productStats = productService.getProductStats(TEST_PRODUCT_ID);
Assertions.assertNotNull(productStats);
}

@Test
void canGetAllStats() {
List<Stats> stats = productService.getStats();
Assertions.assertNotNull(stats);
}

private Duration getDuration(Candles candles) {
Instant candleTime1 = candles.getCandleList().get(0).getTime();
Instant candleTime2 = candles.getCandleList().get(1).getTime();
Expand Down
60 changes: 60 additions & 0 deletions model/src/main/java/com/coinbase/exchange/model/ProductStats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.coinbase.exchange.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductStats {
@JsonProperty("open")
private BigDecimal open;
@JsonProperty("high")
private BigDecimal high;
@JsonProperty("low")
private BigDecimal low;
@JsonProperty("volume")
private BigDecimal volume;
@JsonProperty("last")
private BigDecimal last;

public BigDecimal getOpen() {
return open;
}

public void setOpen(BigDecimal open) {
this.open = open;
}

public BigDecimal getHigh() {
return high;
}

public void setHigh(BigDecimal high) {
this.high = high;
}

public BigDecimal getLow() {
return low;
}

public void setLow(BigDecimal low) {
this.low = low;
}

public BigDecimal getVolume() {
return volume;
}

public void setVolume(BigDecimal volume) {
this.volume = volume;
}

public BigDecimal getLast() {
return last;
}

public void setLast(BigDecimal last) {
this.last = last;
}
}
88 changes: 88 additions & 0 deletions model/src/main/java/com/coinbase/exchange/model/Stats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.coinbase.exchange.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Stats {
private String productId;
private BigDecimal volume30Day;
private BigDecimal open24Hour;
private BigDecimal high24Hour;
private BigDecimal low24Hour;
private BigDecimal volume24Hour;
private BigDecimal last24Hour;

@JsonProperty("stats_30day")
private void stats30Day(Map<String, BigDecimal> volume30Day) {
this.volume30Day = volume30Day.get("volume");
}

@JsonProperty("stats_24hour")
private void stats24Hour(Map<String, BigDecimal> stats24Hour) {
this.open24Hour = stats24Hour.get("open");
this.high24Hour = stats24Hour.get("high");
this.low24Hour = stats24Hour.get("low");
this.volume24Hour = stats24Hour.get("volume");
this.last24Hour = stats24Hour.get("last");
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public BigDecimal getVolume30Day() {
return volume30Day;
}

public void setVolume30Day(BigDecimal volume30Day) {
this.volume30Day = volume30Day;
}

public BigDecimal getOpen24Hour() {
return open24Hour;
}

public void setOpen24Hour(BigDecimal open24Hour) {
this.open24Hour = open24Hour;
}

public BigDecimal getHigh24Hour() {
return high24Hour;
}

public void setHigh24Hour(BigDecimal high24Hour) {
this.high24Hour = high24Hour;
}

public BigDecimal getLow24Hour() {
return low24Hour;
}

public void setLow24Hour(BigDecimal low24Hour) {
this.low24Hour = low24Hour;
}

public BigDecimal getVolume24Hour() {
return volume24Hour;
}

public void setVolume24Hour(BigDecimal volume24Hour) {
this.volume24Hour = volume24Hour;
}

public BigDecimal getLast24Hour() {
return last24Hour;
}

public void setLast24Hour(BigDecimal last24Hour) {
this.last24Hour = last24Hour;
}
}