diff --git a/server/src/main/java/com/talkka/server/api/core/exception/ApiClientException.java b/server/src/main/java/com/talkka/server/api/core/exception/ApiClientException.java new file mode 100644 index 00000000..50b60e56 --- /dev/null +++ b/server/src/main/java/com/talkka/server/api/core/exception/ApiClientException.java @@ -0,0 +1,7 @@ +package com.talkka.server.api.core.exception; + +public class ApiClientException extends RuntimeException { + public ApiClientException(String message) { + super(message); + } +} diff --git a/server/src/main/java/com/talkka/server/api/datagg/service/BusApiService.java b/server/src/main/java/com/talkka/server/api/datagg/service/BusApiService.java index 19ead347..aeeab52e 100644 --- a/server/src/main/java/com/talkka/server/api/datagg/service/BusApiService.java +++ b/server/src/main/java/com/talkka/server/api/datagg/service/BusApiService.java @@ -2,19 +2,20 @@ import java.util.List; +import com.talkka.server.api.core.exception.ApiClientException; import com.talkka.server.api.datagg.dto.BusLocationBodyDto; import com.talkka.server.api.datagg.dto.BusRouteInfoBodyDto; import com.talkka.server.api.datagg.dto.BusRouteSearchBodyDto; import com.talkka.server.api.datagg.dto.BusRouteStationBodyDto; public interface BusApiService { - List getSearchedRouteInfo(String routeName); + List getSearchedRouteInfo(String routeName) throws ApiClientException; - List getRouteInfo(String apiRouteId); + List getRouteInfo(String apiRouteId) throws ApiClientException; - List getRouteStationInfo(String apiRouteId); + List getRouteStationInfo(String apiRouteId) throws ApiClientException; - List getBusLocationInfo(String apiRouteId); + List getBusLocationInfo(String apiRouteId) throws ApiClientException; // List getBusStationArrivalInfo(String routeId); } diff --git a/server/src/main/java/com/talkka/server/api/datagg/service/BusApiServiceImpl.java b/server/src/main/java/com/talkka/server/api/datagg/service/BusApiServiceImpl.java index 5d722a48..fb8f3ede 100644 --- a/server/src/main/java/com/talkka/server/api/datagg/service/BusApiServiceImpl.java +++ b/server/src/main/java/com/talkka/server/api/datagg/service/BusApiServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.util.DefaultUriBuilderFactory; +import com.talkka.server.api.core.exception.ApiClientException; import com.talkka.server.api.datagg.config.BusApiKeyProperty; import com.talkka.server.api.datagg.dto.BusLocationBodyDto; import com.talkka.server.api.datagg.dto.BusLocationRespDto; @@ -30,43 +31,59 @@ public class BusApiServiceImpl implements BusApiService { private static final String host = "apis.data.go.kr"; @Override - public List getSearchedRouteInfo(String keyword) { + public List getSearchedRouteInfo(String keyword) throws ApiClientException { final String path = "/6410000/busrouteservice/getBusRouteList"; MultiValueMap params = new LinkedMultiValueMap<>(); params.add("keyword", keyword); - URI uri = this.getOpenApiURI(path, params); - ResponseEntity resp = restTemplate.getForEntity(uri, BusRouteSearchRespDto.class); - return resp.getBody().msgBody(); + try { + URI uri = this.getOpenApiUri(path, params); + ResponseEntity resp = restTemplate.getForEntity(uri, BusRouteSearchRespDto.class); + return resp.getBody().msgBody(); + } catch (Exception exception) { + throw new ApiClientException(exception.getMessage()); + } } @Override - public List getRouteInfo(String apiRouteId) { + public List getRouteInfo(String apiRouteId) throws ApiClientException { final String path = "/6410000/busrouteservice/getBusRouteInfoItem"; MultiValueMap params = new LinkedMultiValueMap<>(); params.add("routeId", apiRouteId); - URI uri = this.getOpenApiURI(path, params); - ResponseEntity resp = restTemplate.getForEntity(uri, BusRouteInfoRespDto.class); - return resp.getBody().msgBody(); + try { + URI uri = this.getOpenApiUri(path, params); + ResponseEntity resp = restTemplate.getForEntity(uri, BusRouteInfoRespDto.class); + return resp.getBody().msgBody(); + } catch (Exception exception) { + throw new ApiClientException(exception.getMessage()); + } } @Override - public List getRouteStationInfo(String apiRouteId) { + public List getRouteStationInfo(String apiRouteId) throws ApiClientException { final String path = "/6410000/busrouteservice/getBusRouteStationList"; MultiValueMap params = new LinkedMultiValueMap<>(); params.add("routeId", apiRouteId); - URI uri = this.getOpenApiURI(path, params); - ResponseEntity resp = restTemplate.getForEntity(uri, BusRouteStationRespDto.class); - return resp.getBody().msgBody(); + try { + URI uri = this.getOpenApiUri(path, params); + ResponseEntity resp = restTemplate.getForEntity(uri, BusRouteStationRespDto.class); + return resp.getBody().msgBody(); + } catch (Exception exception) { + throw new ApiClientException(exception.getMessage()); + } } @Override - public List getBusLocationInfo(String apiRouteId) { + public List getBusLocationInfo(String apiRouteId) throws ApiClientException { final String path = "/6410000/buslocationservice/getBusLocationList"; MultiValueMap params = new LinkedMultiValueMap<>(); params.add("routeId", apiRouteId); - URI uri = this.getOpenApiURI(path, params); - ResponseEntity resp = restTemplate.getForEntity(uri, BusLocationRespDto.class); - return resp.getBody().msgBody(); + try { + URI uri = this.getOpenApiUri(path, params); + ResponseEntity resp = restTemplate.getForEntity(uri, BusLocationRespDto.class); + return resp.getBody().msgBody(); + } catch (Exception exception) { + throw new ApiClientException(exception.getMessage()); + } } // @Override @@ -74,7 +91,7 @@ public List getBusLocationInfo(String apiRouteId) { // return null; // } - private URI getOpenApiURI(String path, MultiValueMap params) { + private URI getOpenApiUri(String path, MultiValueMap params) { final var builder = new DefaultUriBuilderFactory(); builder.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE); return builder.builder()