스크린샷 2024-07-28 오후 10.47.15.png

스크린샷 2024-07-28 오후 10.48.24.png

스크린샷 2024-07-28 오후 10.49.20.png

public class WebApiExRateProvider implements ExRateProvider {
    @Override
    public BigDecimal getExRate(String currency) {
        String url = "<https://open.er-api.com/v6/latest/>" + currency;

        return runApiForExRate(url, new SimpleApiExecutor());
    }

    private static BigDecimal runApiForExRate(String url, ApiExecutor apiExecutor) {
        URI uri;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        String response;
        try {
            response = apiExecutor.execute(uri);
            System.out.println(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            BigDecimal exRate = extractExRate(response);
            System.out.println("API ExRate: " + exRate);
            return exRate;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    private static BigDecimal extractExRate(String response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        ExRateData data = mapper.readValue(response, ExRateData.class);
        return data.rates().get("KRW");
    }
}

스크린샷 2024-07-28 오후 11.14.46.png

public class WebApiExRateProvider implements ExRateProvider {
    @Override
    public BigDecimal getExRate(String currency) {
        String url = "<https://open.er-api.com/v6/latest/>" + currency;

        return runApiForExRate(url, new SimpleApiExecutor(), new ExRateExtractor() {
            @Override
            public BigDecimal extract(String response) throws JsonProcessingException {
                ObjectMapper mapper = new ObjectMapper();
                ExRateData data = mapper.readValue(response, ExRateData.class);
                return data.rates().get("KRW");
            }
        });
    }

    private static BigDecimal runApiForExRate(String url, ApiExecutor apiExecutor, ExRateExtractor exRateExtractor) {
        URI uri;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        String response;
        try {
            response = apiExecutor.execute(uri);
            System.out.println(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            BigDecimal exRate = exRateExtractor.extract(response);
            System.out.println("API ExRate: " + exRate);
            return exRate;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
public class WebApiExRateProvider implements ExRateProvider {
    @Override
    public BigDecimal getExRate(String currency) {
        String url = "<https://open.er-api.com/v6/latest/>" + currency;

        return runApiForExRate(url, new SimpleApiExecutor(), response -> {
            ObjectMapper mapper = new ObjectMapper();
            ExRateData data = mapper.readValue(response, ExRateData.class);
            return data.rates().get("KRW");
        });
    }

    private static BigDecimal runApiForExRate(String url, ApiExecutor apiExecutor, ExRateExtractor exRateExtractor) {
        URI uri;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        String response;
        try {
            response = apiExecutor.execute(uri);
            System.out.println(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            BigDecimal exRate = exRateExtractor.extract(response);
            System.out.println("API ExRate: " + exRate);
            return exRate;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
public class ErApiExRateExtractor implements ExRateExtractor {
    @Override
    public BigDecimal extract(String response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        ExRateData data = mapper.readValue(response, ExRateData.class);
        return data.rates().get("KRW");
    }
}

public class WebApiExRateProvider implements ExRateProvider {
    @Override
    public BigDecimal getExRate(String currency) {
        String url = "<https://open.er-api.com/v6/latest/>" + currency;

        return runApiForExRate(url, new SimpleApiExecutor(), new ErApiExRateExtractor());
    }

    private static BigDecimal runApiForExRate(String url, ApiExecutor apiExecutor, ExRateExtractor exRateExtractor) {
        URI uri;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        String response;
        try {
            response = apiExecutor.execute(uri);
            System.out.println(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            BigDecimal exRate = exRateExtractor.extract(response);
            System.out.println("API ExRate: " + exRate);
            return exRate;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}