Open all-cloudz opened 4 months ago
URI 준비하고 예외처리를 위한 작업을 하는 코드
API를 실행하고, 서버로부터 받은 응답을 가져오는 코드
JSON 문자열을 파싱하고 필요한 환율 정보를 추출하는 코드
예시 코드
package tobyspring.hellospring.exrate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import tobyspring.hellospring.payment.ExRateProvider;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.stream.Collectors;
@Component
public class WebApiExRateProvider implements ExRateProvider {
@Override
public BigDecimal getExRate(String currency) {
String url = "https://open.er-api.com/v6/latest/" + currency;
URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
String response;
try {
// 2번
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
response = in.lines().collect(Collectors.joining());
}
// 2번 끝
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
// 3번
ObjectMapper mapper = new ObjectMapper();
ExRateData data = mapper.readValue(response, ExRateData.class);
return data.rates().get("KRW");
// 3번 끝
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
메서드 추출
예시 코드
package tobyspring.hellospring.exrate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import tobyspring.hellospring.payment.ExRateProvider;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.stream.Collectors;
@Component
public class WebApiExRateProvider implements ExRateProvider {
@Override
public BigDecimal getExRate(String currency) {
String url = "https://open.er-api.com/v6/latest/" + currency;
return runApiForExRate(url);
}
private static BigDecimal runApiForExRate(String url) {
URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
String response;
try {
response = executeApi(uri);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
return extractExRate(response);
} 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");
}
private static String executeApi(URI uri) throws IOException {
String response;
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
response = in.lines().collect(Collectors.joining());
}
return response;
}
}
추가적으로 정리할 것
섹션4. 템플릿