ExchangeService.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.danielbohry.stocks.service;
  2. import com.danielbohry.stocks.client.ExchangeRateClient;
  3. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  4. import com.fasterxml.jackson.annotation.JsonProperty;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.cache.annotation.Cacheable;
  11. import org.springframework.stereotype.Service;
  12. import java.math.BigDecimal;
  13. import java.util.Map;
  14. @Slf4j
  15. @Service
  16. public class ExchangeService {
  17. private final ExchangeRateClient client;
  18. private final String key;
  19. public ExchangeService(ExchangeRateClient client, @Value("${clients.exchange.key}") String key) {
  20. this.client = client;
  21. this.key = key;
  22. }
  23. @Cacheable(value = "exchangeRates", key = "#currency")
  24. public ExchangeRateResponse getCurrentRate(String currency) {
  25. return client.getRate(currency, key);
  26. }
  27. @Data
  28. @AllArgsConstructor
  29. @NoArgsConstructor
  30. @JsonIgnoreProperties(ignoreUnknown = true)
  31. public static class ExchangeRateResponse {
  32. @JsonProperty("result")
  33. private String result;
  34. @JsonProperty("base_code")
  35. private String code;
  36. @JsonProperty("conversion_rates")
  37. private Map<String, BigDecimal> conversionRates;
  38. }
  39. }