feat: NbpService & tests implementation

This commit is contained in:
Piotr Dec 2024-05-23 23:30:02 +02:00
parent 9e69a47b3e
commit 1ae5dd9957
Signed by: stawros
GPG key ID: F89F27AD8F881A91
2 changed files with 20 additions and 5 deletions

View file

@ -1,14 +1,17 @@
package eu.ztsh.wymiana.service;
import eu.ztsh.wymiana.exception.NoDataException;
import eu.ztsh.wymiana.model.Rates;
import lombok.RequiredArgsConstructor;
import org.assertj.core.util.VisibleForTesting;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
/**
@ -21,13 +24,14 @@ public class NbpService {
private final Clock clock;
private final RestClient restClient;
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public double getSellRate(String currency) {
throw new UnsupportedOperationException("Not implemented yet");
return fetchData(currency, dtf.format(getFetchDate())).getRates().get(0).getAsk();
}
public double getBuyRate(String currency) {
throw new UnsupportedOperationException("Not implemented yet");
return fetchData(currency, dtf.format(getFetchDate())).getRates().get(0).getBid();
}
/**
@ -45,7 +49,12 @@ public class NbpService {
@VisibleForTesting
Rates fetchData(String code, String date) {
throw new UnsupportedOperationException("Not implemented yet");
return restClient.get().uri(URI_PATTERN, code.toLowerCase(), date)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, ((request, response) -> {
throw new NoDataException(code, date);
}))
.body(Rates.class);
}
private static boolean isWeekend(LocalDate today) {