BAEL-1570 (#3762)
* Create pom.xml * Update pom.xml * Create pom.xml * Update pom.xml * add impl * add app
This commit is contained in:
42
java-spi/exchange-rate-impl/pom.xml
Normal file
42
java-spi/exchange-rate-impl/pom.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>exchange-rate-impl</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-spi</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>exchange-rate-api</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.10.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.json.bind</groupId>
|
||||
<artifactId>javax.json.bind-api</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse</groupId>
|
||||
<artifactId>yasson</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.rate.impl;
|
||||
|
||||
import com.baeldung.rate.api.Quote;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class QuoteResponse {
|
||||
private List<Quote> result;
|
||||
private String error;
|
||||
|
||||
public List<Quote> getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(List<Quote> result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.rate.impl;
|
||||
|
||||
public class QuoteResponseWrapper {
|
||||
private QuoteResponse quoteResponse;
|
||||
|
||||
public QuoteResponse getQuoteResponse() {
|
||||
return quoteResponse;
|
||||
}
|
||||
|
||||
public void setQuoteResponse(QuoteResponse quoteResponse) {
|
||||
this.quoteResponse = quoteResponse;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.rate.impl;
|
||||
|
||||
import com.baeldung.rate.api.QuoteManager;
|
||||
import com.baeldung.rate.spi.ExchangeRateProvider;
|
||||
|
||||
public class YahooFinanceExchangeRateProvider implements ExchangeRateProvider {
|
||||
|
||||
@Override
|
||||
public QuoteManager create() {
|
||||
return new YahooQuoteManagerImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
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.JsonbBuilder;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Currency;
|
||||
import java.util.List;
|
||||
|
||||
public class YahooQuoteManagerImpl implements QuoteManager {
|
||||
|
||||
static final String URL_PROVIDER = "https://query1.finance.yahoo.com/v7/finance/quote";
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
@Override
|
||||
public List<Quote> getQuotes(String baseCurrency, LocalDate date) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Currency.getAvailableCurrencies().forEach(currency -> {
|
||||
if (!currency.equals(currency.getCurrencyCode())) {
|
||||
sb.append(baseCurrency).append(currency.getCurrencyCode()).append("=X").append(",");
|
||||
}
|
||||
});
|
||||
|
||||
String value = "";
|
||||
try {
|
||||
value = URLEncoder.encode(sb.toString().substring(0, sb.toString().length() - 1), "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String queryString = String.format("%s=%s", "symbols", value);
|
||||
String response = doGetRequest(queryString);
|
||||
System.out.println(response);
|
||||
return map(response);
|
||||
}
|
||||
|
||||
private List<Quote> map(String response) {
|
||||
QuoteResponseWrapper qrw = JsonbBuilder.create().fromJson(response, QuoteResponseWrapper.class);
|
||||
return qrw.getQuoteResponse().getResult();
|
||||
}
|
||||
|
||||
String doGetRequest(String queryString) {
|
||||
String fullUrl = URL_PROVIDER + "?" + queryString;
|
||||
|
||||
System.out.println(fullUrl);
|
||||
Request request = new Request.Builder()
|
||||
.url(fullUrl)
|
||||
.build();
|
||||
Response response = null;
|
||||
try {
|
||||
response = client.newCall(request).execute();
|
||||
return response.body().string();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.baeldung.rate.impl.YahooFinanceExchangeRateProvider
|
||||
Reference in New Issue
Block a user