Integrate Bitpay exchange rate API

Add a Bitpay exchange rate provider and corresponding unit test.
This commit is contained in:
cd2357 2020-07-27 13:22:53 +02:00
parent 637378b58a
commit 9be2a5bbb4
No known key found for this signature in database
GPG Key ID: F26C56748514D0D3
4 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,84 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.price.spot.providers;
import bisq.price.spot.ExchangeRate;
import bisq.price.spot.ExchangeRateProvider;
import bisq.price.util.bitpay.BitpayMarketData;
import bisq.price.util.bitpay.BitpayTicker;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.time.Duration;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
@Component
class Bitpay extends ExchangeRateProvider {
private final RestTemplate restTemplate = new RestTemplate();
public Bitpay() {
super("BITPAY", "bitpay", Duration.ofMinutes(1));
}
@Override
public Set<ExchangeRate> doGet() {
Set<ExchangeRate> result = new HashSet<ExchangeRate>();
Predicate<BitpayTicker> isDesiredFiatPair = t -> SUPPORTED_FIAT_CURRENCIES.contains(t.getCode());
Predicate<BitpayTicker> isDesiredCryptoPair = t -> SUPPORTED_CRYPTO_CURRENCIES.contains(t.getCode());
getTickers()
.filter(isDesiredFiatPair.or(isDesiredCryptoPair))
.forEach(ticker -> {
result.add(new ExchangeRate(
ticker.getCode(),
ticker.getRate(),
new Date(),
this.getName()
));
});
return result;
}
private Stream<BitpayTicker> getTickers() {
BitpayMarketData marketData = restTemplate.exchange(
RequestEntity
.get(UriComponentsBuilder
.fromUriString("https://bitpay.com/rates").build()
.toUri())
.build(),
new ParameterizedTypeReference<BitpayMarketData>() {
}
).getBody();
return Arrays.stream(marketData.getData());
}
}

View File

@ -0,0 +1,13 @@
package bisq.price.util.bitpay;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class BitpayMarketData {
private BitpayTicker[] data;
}

View File

@ -0,0 +1,18 @@
package bisq.price.util.bitpay;
import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class BitpayTicker {
private String code;
private String name;
private BigDecimal rate;
}

View File

@ -0,0 +1,34 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.price.spot.providers;
import bisq.price.AbstractExchangeRateProviderTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
public class BitpayTest extends AbstractExchangeRateProviderTest {
@Test
public void doGet_successfulCall() {
doGet_successfulCall(new Bitpay());
}
}