JAVA-29231 Move modules inside existing container modules (#15492)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<name>exchange-rate-impl</name>
|
||||
<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>${exchange-rate-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.json.bind</groupId>
|
||||
<artifactId>javax.json.bind-api</artifactId>
|
||||
<version>${javax.json.bind-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse</groupId>
|
||||
<artifactId>yasson</artifactId>
|
||||
<version>${yasson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>${javax.json.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${maven-dependency-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/depends</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<exchange-rate-api.version>1.0.0-SNAPSHOT</exchange-rate-api.version>
|
||||
<okhttp.version>4.12.0</okhttp.version>
|
||||
<javax.json.bind-api.version>1.0</javax.json.bind-api.version>
|
||||
<yasson.version>1.0.1</yasson.version>
|
||||
<javax.json.version>1.1.2</javax.json.version>
|
||||
<maven-dependency-plugin.version>3.1.0</maven-dependency-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+13
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
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.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://query2.finance.yahoo.com/v6/finance/quoteSummary/%s=X?modules=summaryDetail";
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
@Override
|
||||
public List<Quote> getQuotes(String baseCurrency, LocalDate date) {
|
||||
|
||||
List<String> currencyQuery = new ArrayList<>();
|
||||
Currency.getAvailableCurrencies().forEach(currency -> {
|
||||
if (!baseCurrency.equals(currency.getCurrencyCode())) {
|
||||
currencyQuery.add(String.format(URL_PROVIDER, baseCurrency + currency.getCurrencyCode()));
|
||||
}
|
||||
});
|
||||
final List<Quote> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return quotes;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private static Quote parseResult(Map qrw) {
|
||||
Quote quote = null;
|
||||
if (qrw != null) {
|
||||
final Map quoteSummary = (Map) qrw.get("quoteSummary");
|
||||
if (quoteSummary != null) {
|
||||
final List<Map> result = (List<Map>) 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;
|
||||
}
|
||||
|
||||
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(url)
|
||||
.build();
|
||||
Response response;
|
||||
try {
|
||||
response = client.newCall(request).execute();
|
||||
return response.body().string();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
com.baeldung.rate.impl.YahooFinanceExchangeRateProvider
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user