Skip to content

Commit

Permalink
Merge pull request #128 from TeamPINGLE/fix/127
Browse files Browse the repository at this point in the history
[fix] 외부 api 호출시 에러처리
  • Loading branch information
Parkjyun authored Mar 14, 2024
2 parents 49ce595 + 92f4cd8 commit 30e3e75
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/main/java/org/pingle/pingleserver/utils/NaverUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ public class NaverUtil {
private String naverLocationSearchUrl;

public List<LocationResponse> getLocationInfo(String location) {

NaverLocationResponse naverLocationResponse = locationSearch(NaverLocationRequest.of(location));

NaverLocationResponse naverLocationResponse;
try {
naverLocationResponse = locationSearch(NaverLocationRequest.of(location));
} catch (RuntimeException e) {
return List.of();
}
return convertResponse(naverLocationResponse);
}

private NaverLocationResponse locationSearch(NaverLocationRequest request) {
private NaverLocationResponse locationSearch(NaverLocationRequest request) throws RuntimeException {
URI uri = UriComponentsBuilder
.fromUriString(naverLocationSearchUrl)
.queryParams(request.toMap())
Expand All @@ -50,22 +53,23 @@ private NaverLocationResponse locationSearch(NaverLocationRequest request) {
HttpEntity<Object> httpEntity = new HttpEntity<>(httpHeaders);
ParameterizedTypeReference<NaverLocationResponse> responseType = new ParameterizedTypeReference<>() {
};

ResponseEntity<NaverLocationResponse> responseEntity = new RestTemplate()
.exchange(uri, HttpMethod.GET, httpEntity, responseType);

ResponseEntity<NaverLocationResponse> responseEntity;

try {
responseEntity = new RestTemplate()
.exchange(uri, HttpMethod.GET, httpEntity, responseType);
} catch (Exception e) {
throw new RuntimeException("Naver API 호출 중 오류가 발생했습니다.");
}
return responseEntity.getBody();
}

private List<LocationResponse> convertResponse(NaverLocationResponse response) {

List<NaverLocationResponse.SearchLocationItem> items = response.items();
List<LocationResponse> responseList = items
.stream()

return items.stream()
.map(item -> new LocationResponse(convertX(item.getMapx()), convertY(item.getMapy()), trim(item.getTitle()), trim(item.getAddress()), trim(item.getRoadAddress())))
.collect(Collectors.toList());

return responseList;
}
private double convertX(int x) {

Expand Down

0 comments on commit 30e3e75

Please sign in to comment.