| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package com.danielbohry.stocks.service;
- import com.danielbohry.stocks.client.ExchangeRateClient;
- import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
- import com.fasterxml.jackson.annotation.JsonProperty;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.stereotype.Service;
- import java.math.BigDecimal;
- import java.util.Map;
- @Slf4j
- @Service
- public class ExchangeService {
- private final ExchangeRateClient client;
- private final String key;
- public ExchangeService(ExchangeRateClient client, @Value("${clients.exchange.key}") String key) {
- this.client = client;
- this.key = key;
- }
- @Cacheable(value = "exchangeRates", key = "#currency")
- public ExchangeRateResponse getCurrentRate(String currency) {
- return client.getRate(currency, key);
- }
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @JsonIgnoreProperties(ignoreUnknown = true)
- public static class ExchangeRateResponse {
- @JsonProperty("result")
- private String result;
- @JsonProperty("base_code")
- private String code;
- @JsonProperty("conversion_rates")
- private Map<String, BigDecimal> conversionRates;
- }
- }
|