


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");
}
}

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);
}
}
}