From 4ea1c0690f26275f5e0e679d921fceb3c05fc174 Mon Sep 17 00:00:00 2001 From: FarukBraimo Date: Tue, 18 Jun 2024 16:39:05 +0200 Subject: [PATCH] - Fix ( Hiding weather forecast for country) --- .../model/response/MetadataResponse.java | 1 + .../service/CountryMetadataService.java | 47 +++++++++++++++++++ .../falcon/service/ExchangeRateService.java | 24 +++------- .../falcon/service/InsightService.java | 22 ++++++--- 4 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/vodacom/falcon/service/CountryMetadataService.java diff --git a/src/main/java/com/vodacom/falcon/model/response/MetadataResponse.java b/src/main/java/com/vodacom/falcon/model/response/MetadataResponse.java index 350ad9f..06daa3a 100644 --- a/src/main/java/com/vodacom/falcon/model/response/MetadataResponse.java +++ b/src/main/java/com/vodacom/falcon/model/response/MetadataResponse.java @@ -13,5 +13,6 @@ @AllArgsConstructor public class MetadataResponse { private boolean isAuthenticatedUser; + private boolean isCountry; private String message; } diff --git a/src/main/java/com/vodacom/falcon/service/CountryMetadataService.java b/src/main/java/com/vodacom/falcon/service/CountryMetadataService.java new file mode 100644 index 0000000..6813ce1 --- /dev/null +++ b/src/main/java/com/vodacom/falcon/service/CountryMetadataService.java @@ -0,0 +1,47 @@ +package com.vodacom.falcon.service; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.vodacom.falcon.client.APICaller; +import com.vodacom.falcon.util.JsonUtil; +import org.springframework.stereotype.Service; + +import java.net.http.HttpResponse; +import java.util.Map; + +import static com.vodacom.falcon.util.FalconDefaults.COUNTRY_API_BASE_URL; +import static com.vodacom.falcon.util.JsonUtil.deserializeByTypeReference; + +@Service +public class CountryMetadataService { + public String getCountryCode(String term) { + String url = String.format("%s/api/v0.1/countries/positions/q?country=%s", COUNTRY_API_BASE_URL, term.toLowerCase()); + + HttpResponse response = APICaller.getData(url); + if (response != null) { + Map data = deserializeByTypeReference(response.body(), new TypeReference<>() { + }); + if (data != null) { + Map currency = deserializeByTypeReference(JsonUtil.serialize(data.get("data")), new TypeReference<>() { + }); + if (currency != null) return currency.get("iso2").toString(); + } + } + return null; + } + + public String getCurrency(String countyCode) { + String url = String.format("%s/api/v0.1/countries/currency/q?iso2=%s", COUNTRY_API_BASE_URL, countyCode); + + HttpResponse response = APICaller.getData(url); + if (response != null) { + Map data = deserializeByTypeReference(response.body(), new TypeReference<>() { + }); + if (data != null) { + Map currency = deserializeByTypeReference(JsonUtil.serialize(data.get("data")), new TypeReference<>() { + }); + if (currency != null) return currency.get("currency").toString(); + } + } + return null; + } +} diff --git a/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java b/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java index 7559dbc..c2dea34 100644 --- a/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java +++ b/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java @@ -4,6 +4,7 @@ import com.vodacom.falcon.client.APICaller; import com.vodacom.falcon.model.response.ExchangeRateResponse; import com.vodacom.falcon.util.JsonUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -25,13 +26,18 @@ public class ExchangeRateService { @Value("${optional-exchange-rates-api.apiKey}") private String optionalExchangeRatesApiKey; + @Autowired + private CountryMetadataService countryMetadataService; + public ExchangeRateResponse getExchangeRates(String countryCode) { - String currency = this.getCurrency(countryCode); + String currency = this.countryMetadataService.getCurrency(countryCode); String mainExchangeRateUrl = String.format("%s/v1/latest?symbols=%s&access_key=%s", MAIN_EXCHANGE_RATE_API_BASE_URL, currency, mainExchangeRatesApiKey); // Limited to 250 request per month on free acc. String optionalExchangeRateUrl = String.format("%s/v2.0/rates/latest?symbols=%s&apikey=%s", OPTIONAL_EXCHANGE_RATE_API_BASE_URL, currency, optionalExchangeRatesApiKey); // Up to 1k requests. ; ExchangeRateResponse ratesFromMainSource = buildExchangeRates(mainExchangeRateUrl); + +// FixME: Enable this // if (ratesFromMainSource != null) { // return ratesFromMainSource; // } @@ -45,20 +51,4 @@ private ExchangeRateResponse buildExchangeRates(String url) { } return null; } - - private String getCurrency(String countyCode) { - String url = String.format("%s/api/v0.1/countries/currency/q?iso2=%s", COUNTRY_API_BASE_URL, countyCode); - - HttpResponse response = APICaller.getData(url); - if (response != null) { - Map data = deserializeByTypeReference(response.body(), new TypeReference<>() { - }); - if (data != null) { - Map currency = deserializeByTypeReference(JsonUtil.serialize(data.get("data")), new TypeReference<>() { - }); - if (currency != null) return currency.get("currency").toString(); - } - } - return null; - } } diff --git a/src/main/java/com/vodacom/falcon/service/InsightService.java b/src/main/java/com/vodacom/falcon/service/InsightService.java index bea7766..e6b4a46 100644 --- a/src/main/java/com/vodacom/falcon/service/InsightService.java +++ b/src/main/java/com/vodacom/falcon/service/InsightService.java @@ -15,6 +15,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.Objects; import static com.vodacom.falcon.util.FalconDefaults.WB_FILTER_DATE; @@ -26,21 +27,30 @@ public class InsightService { private final EconomyInsightService economyInsightService; private final WeatherForecastService weatherForecastService; private final ExchangeRateService exchangeRateService; + private final CountryMetadataService countryMetadataService; public InsightResponse getInsight(String city) { log.info("Getting insights for {}", city); + MetadataResponse metadata = new MetadataResponse(); String encodedCity = URLEncoder.encode(city, StandardCharsets.UTF_8); - WeatherForecastResponse weatherForecast = weatherForecastService.getWeatherForecast(encodedCity); + String countryCode = countryMetadataService.getCountryCode(encodedCity); + WeatherForecastResponse weatherForecast = new WeatherForecastResponse(); + metadata.setCountry(true); + + if (Objects.isNull(countryCode)) { + weatherForecast = weatherForecastService.getWeatherForecast(encodedCity); + countryCode = weatherForecast + .getForecast() + .getLocation() + .getCountryCode(); - String countryCode = weatherForecast - .getForecast() - .getLocation() - .getCountryCode(); + metadata.setCountry(false); + + } Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - MetadataResponse metadata = new MetadataResponse(); metadata.setAuthenticatedUser(true); metadata.setMessage("Enjoy your destination %s! ");