Marek Czernek
2020-03-11 4ee09a800249c91d03502e52fc276d4cf4ae2a23
Add proxy for the Currency service
2 files added
4 files modified
144 ■■■■■ changed files
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/Currency.java 9 ●●●●● patch | view | raw | blame | history
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/CurrencyResource.java 45 ●●●● patch | view | raw | blame | history
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/CurrencyService.java 7 ●●●●● patch | view | raw | blame | history
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/ExchangeResource.java 58 ●●●●● patch | view | raw | blame | history
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/ExchangeService.java 21 ●●●●● patch | view | raw | blame | history
mczernek-exchange-application/exchange/src/main/resources/application.properties 4 ●●● patch | view | raw | blame | history
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/Currency.java
@@ -4,6 +4,7 @@
    private String value;
    private String date;
    private String sign;
    private String name;
    public String getValue() {
        return value;
@@ -29,6 +30,14 @@
        this.sign = sign;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Currency{" +
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/CurrencyResource.java
@@ -1,60 +1,27 @@
package com.redhat.restclient;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Path("/exchangeRate")
@Path("/currencies")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class CurrencyResource {
    @Inject
    @RestClient
    CurrencyService currencyService;
    ObjectMapper mapper = new ObjectMapper();
    CurrencyService currencies;
    @POST
    @Path("/historicalData")
    // TODO: validate whether currency Service serves the source/target currency
    // something like new ObjectMapper().readTree(body).get("source")
    public List<Currency> getHistoricalData(String body) {
        System.out.println("Serving:" + body);
        List<Currency> res = currencyService.getCurrencies(body);
        return currencyService.getCurrencies(body);
    }
    @POST
    @Path("/singleCurrency")
    // TODO: validate whether currency Service serves the source/target currency
    public Currency getExchangeRate(String body) {
        List<Currency> currencies = currencyService.getCurrencies(body);
        Currency latestCurrency = currencies.get(0);
        try {
            String target = mapper.readTree(body).get("target").asText();
            if(target.equals("USD")) {
                latestCurrency.setSign("$");
            } else {
                latestCurrency.setSign("€");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return latestCurrency;
    }
    // A simple health check of the service, as well as
    // connectivity check between the service and other services
    @GET
    public String ping() {
        return "pong";
    public List<String> getCurrencyNames() {
        return currencies.getCurrencyNames();
    }
}
}
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/CurrencyService.java
@@ -3,19 +3,18 @@
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
// Path on the service we're calling
@Path("/")
@RegisterRestClient
public interface CurrencyService {
    @POST
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    List<Currency> getCurrencies(String body);
    List<String> getCurrencyNames();
}
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/ExchangeResource.java
New file
@@ -0,0 +1,58 @@
package com.redhat.restclient;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Path("/exchangeRate")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ExchangeResource {
    @Inject
    @RestClient
    ExchangeService historyService;
    ObjectMapper mapper = new ObjectMapper();
    @POST
    @Path("/historicalData")
    // TODO: validate whether currency Service serves the source/target currency
    // something like new ObjectMapper().readTree(body).get("source")
    public List<Currency> getHistoricalData(String body) {
        return historyService.getCurrencyExchangeRates(body);
    }
    @POST
    @Path("/singleCurrency")
    // TODO: validate whether currency Service serves the source/target currency
    public Currency getExchangeRate(String body) {
        List<Currency> currencies = historyService.getCurrencyExchangeRates(body);
        Currency latestCurrency = currencies.get(0);
        try {
            String target = mapper.readTree(body).get("target").asText();
            if(target.equals("USD")) {
                latestCurrency.setSign("$");
            } else {
                latestCurrency.setSign("€");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return latestCurrency;
    }
    // A simple health check of the service, as well as
    // connectivity check between the service and other services
    @GET
    public String ping() {
        return "pong";
    }
}
mczernek-exchange-application/exchange/src/main/java/com/redhat/restclient/ExchangeService.java
New file
@@ -0,0 +1,21 @@
package com.redhat.restclient;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
// Path on the service we're calling
@Path("/")
@RegisterRestClient
public interface ExchangeService {
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    List<Currency> getCurrencyExchangeRates(String body);
}
mczernek-exchange-application/exchange/src/main/resources/application.properties
@@ -1,5 +1,7 @@
# Configuration file
com.redhat.restclient.CurrencyService/mp-rest/url=http://history:8080
com.redhat.restclient.ExchangeService/mp-rest/url=http://history:8080
com.redhat.restclient.ExchangeService/mp-rest/scope=javax.inject.Singleton
com.redhat.restclient.CurrencyService/mp-rest/url=http://currency:5000
com.redhat.restclient.CurrencyService/mp-rest/scope=javax.inject.Singleton
quarkus.http.port=8080