Skip to content

Commit

Permalink
- Fix ( Hiding weather forecast for country)
Browse files Browse the repository at this point in the history
  • Loading branch information
FarukBraimo committed Jun 18, 2024
1 parent 2afef92 commit 4ea1c06
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
@AllArgsConstructor
public class MetadataResponse {
private boolean isAuthenticatedUser;
private boolean isCountry;
private String message;
}
Original file line number Diff line number Diff line change
@@ -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<String> response = APICaller.getData(url);
if (response != null) {
Map<Object, Object> data = deserializeByTypeReference(response.body(), new TypeReference<>() {
});
if (data != null) {
Map<Object, Object> 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<String> response = APICaller.getData(url);
if (response != null) {
Map<Object, Object> data = deserializeByTypeReference(response.body(), new TypeReference<>() {
});
if (data != null) {
Map<Object, Object> currency = deserializeByTypeReference(JsonUtil.serialize(data.get("data")), new TypeReference<>() {
});
if (currency != null) return currency.get("currency").toString();
}
}
return null;
}
}
24 changes: 7 additions & 17 deletions src/main/java/com/vodacom/falcon/service/ExchangeRateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
// }
Expand All @@ -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<String> response = APICaller.getData(url);
if (response != null) {
Map<Object, Object> data = deserializeByTypeReference(response.body(), new TypeReference<>() {
});
if (data != null) {
Map<Object, Object> currency = deserializeByTypeReference(JsonUtil.serialize(data.get("data")), new TypeReference<>() {
});
if (currency != null) return currency.get("currency").toString();
}
}
return null;
}
}
22 changes: 16 additions & 6 deletions src/main/java/com/vodacom/falcon/service/InsightService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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! ");

Expand Down

0 comments on commit 4ea1c06

Please sign in to comment.