diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java index afc7ef92ce..026f5a23c5 100644 --- a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java @@ -16,9 +16,7 @@ public final class ExchangeRate { public static List providers() { List services = new ArrayList<>(); ServiceLoader loader = ServiceLoader.load(ExchangeRateProvider.class); - loader.forEach(exchangeRateProvider -> { - services.add(exchangeRateProvider); - }); + loader.forEach(services::add); return services; } diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java index 577af3b618..b5c2bf23fb 100644 --- a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java @@ -8,7 +8,12 @@ public class Quote { private BigDecimal ask; private BigDecimal bid; private LocalDate date; - //... + + public Quote(String currency, BigDecimal ask, BigDecimal bid) { + this.currency = currency; + this.ask = ask; + this.bid = bid; + } public String getCurrency() { return currency; diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponse.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponse.java deleted file mode 100644 index 9ba4fb26b0..0000000000 --- a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponse.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.rate.impl; - -import com.baeldung.rate.api.Quote; - -import java.util.List; - -public class QuoteResponse { - private List result; - private String error; - - public List getResult() { - return result; - } - - public void setResult(List result) { - this.result = result; - } - - public String getError() { - return error; - } - - public void setError(String error) { - this.error = error; - } -} diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponseWrapper.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponseWrapper.java deleted file mode 100644 index 6d7be086f0..0000000000 --- a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponseWrapper.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.rate.impl; - -public class QuoteResponseWrapper { - private QuoteResponse quoteResponse; - - public QuoteResponse getQuoteResponse() { - return quoteResponse; - } - - public void setQuoteResponse(QuoteResponse quoteResponse) { - this.quoteResponse = quoteResponse; - } -} diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java index f5c60699c7..ff507d4c9f 100644 --- a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java +++ b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java @@ -2,64 +2,102 @@ package com.baeldung.rate.impl; import com.baeldung.rate.api.Quote; import com.baeldung.rate.api.QuoteManager; + import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import javax.json.bind.Jsonb; import javax.json.bind.JsonbBuilder; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; +import java.math.BigDecimal; import java.time.LocalDate; +import java.util.ArrayList; import java.util.Currency; import java.util.List; +import java.util.Map; public class YahooQuoteManagerImpl implements QuoteManager { - static final String URL_PROVIDER = "https://query1.finance.yahoo.com/v7/finance/quote"; + static final String URL_PROVIDER = "https://query2.finance.yahoo.com/v6/finance/quoteSummary/%s=X?modules=summaryDetail"; OkHttpClient client = new OkHttpClient(); @Override public List getQuotes(String baseCurrency, LocalDate date) { - StringBuilder sb = new StringBuilder(); + List currencyQuery = new ArrayList<>(); Currency.getAvailableCurrencies().forEach(currency -> { if (!baseCurrency.equals(currency.getCurrencyCode())) { - sb.append(baseCurrency).append(currency.getCurrencyCode()).append("=X").append(","); + currencyQuery.add(String.format(URL_PROVIDER, baseCurrency + currency.getCurrencyCode())); } }); - - String value = ""; - try { - value = URLEncoder.encode(sb.toString().substring(0, sb.toString().length() - 1), "UTF-8"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + final List quotes = new ArrayList<>(); + for (String url: currencyQuery) { + String response = doGetRequest(url); + if (response != null) { + final Quote map = map(response); + if (map != null) { + quotes.add(map); + } + } } - String queryString = String.format("%s=%s", "symbols", value); - String response = doGetRequest(queryString); - System.out.println(response); - return map(response); + return quotes; } - private List map(String response) { - QuoteResponseWrapper qrw = JsonbBuilder.create().fromJson(response, QuoteResponseWrapper.class); - return qrw.getQuoteResponse().getResult(); + private Quote map(String response) { + try (final Jsonb jsonb = JsonbBuilder.create()) { + final Map qrw = jsonb.fromJson(response, Map.class); + return parseResult(qrw); + } catch (Exception e) { + System.out.println("Error while trying to read response"); + return null; + } } - String doGetRequest(String queryString) { - String fullUrl = URL_PROVIDER + "?" + queryString; + private static Quote parseResult(Map qrw) { + Quote quote = null; + if (qrw != null) { + final Map quoteSummary = (Map) qrw.get("quoteSummary"); + if (quoteSummary != null) { + final List result = (List) quoteSummary.get("result"); + if (result != null) { + final Map resultArray = result.get(0); + if (resultArray != null) { + final Map summaryDetail = (Map) resultArray.get("summaryDetail"); + if (summaryDetail != null) { + quote = constructQuote(summaryDetail); + } + } + } + } + } + return quote; + } - System.out.println(fullUrl); + private static Quote constructQuote(Map summaryDetail) { + final String currency = (String) summaryDetail.get("currency"); + final Map ask = (Map) summaryDetail.get("ask"); + final Map bid = (Map) summaryDetail.get("bid"); + final BigDecimal askPrice = (BigDecimal) ask.get("raw"); + final BigDecimal bidPrice = (BigDecimal) bid.get("raw"); + if (askPrice != null && bidPrice != null) { + return new Quote(currency, askPrice, bidPrice); + } + return null; + } + + String doGetRequest(String url) { + + System.out.println(url); Request request = new Request.Builder() - .url(fullUrl) + .url(url) .build(); - Response response = null; + Response response; try { response = client.newCall(request).execute(); return response.body().string(); } catch (IOException e) { - e.printStackTrace(); + return null; } - return null; } }