-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from TravelCompass-UMC/feature/20-naverApi
[#20] 네이버 경로 api
- Loading branch information
Showing
8 changed files
with
137 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/travelcompass/api/feign/Naver/NaverDurationClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.travelcompass.api.feign.Naver; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@FeignClient(name = "naverDurationClient", url = "${naver.directions.api-url}") | ||
public interface NaverDurationClient { | ||
|
||
@GetMapping("/v1/driving") | ||
ResponseEntity<String> getDuration(@RequestParam("start") String start, | ||
@RequestParam("goal") String goal, | ||
@RequestHeader("X-NCP-APIGW-API-KEY-ID") String clientId, | ||
@RequestHeader("X-NCP-APIGW-API-KEY") String clientSecret); | ||
} | ||
/* 요청 예시 | ||
curl -X GET "https://naveropenapi.apigw.ntruss.com/map-direction/v1/driving?start=127.12345,37.12345&goal=128.12345,37.12345" \ | ||
-H "X-NCP-APIGW-API-KEY-ID: g4444lcndr" \ | ||
-H "X-NCP-APIGW-API-KEY: qXHf876pnArw9AQ36pfsfVmL0kqCB8ppEqj5A2Lm" | ||
*/ |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/travelcompass/api/feign/Naver/NaverPathController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.travelcompass.api.feign.Naver; | ||
|
||
import com.travelcompass.api.global.api_payload.ApiResponse; | ||
import com.travelcompass.api.global.api_payload.SuccessCode; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.Parameters; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Tag(name = "여행계획 경로", description = "경로(자가용) 관련 api 입니다. - 양지원") | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class NaverPathController { | ||
|
||
private final NaverPathService naverPathService; | ||
|
||
@Operation(summary = "여행계획 경로", description = "장소 id를 파라미터로 주면, 자가용 소요시간을 반환하는 메서드입니다.") | ||
@ApiResponses({ | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "PLAN_2031", description = "네이버 경로탐색이 완료되었습니다.(단위: 1/1000초)"), | ||
}) | ||
@Parameters({ | ||
@Parameter(name = "start", description = "출발지의 location-id"), | ||
@Parameter(name = "goal", description = "도착지의 location-id"), | ||
}) | ||
@GetMapping("/getCarDuration") // http://dev.enble.site:8080/getCarDuration?start=1&goal=2 | ||
public ApiResponse<Integer> getDuration(@RequestParam("start") Long startLocationId, | ||
@RequestParam("goal") Long goalLocationId) { | ||
|
||
// 파라미터로 받은 locaion-id값 좌표로 변환 | ||
String start = naverPathService.getCoordinateById(startLocationId); | ||
String goal = naverPathService.getCoordinateById(goalLocationId); | ||
|
||
// 시작, 도착지 좌표 네이버 경로 서비스로 전달 | ||
Integer duration = naverPathService.getDuration(start, goal); | ||
|
||
return ApiResponse.onSuccess(SuccessCode.PLAN_NAVER_GET_DURATION, duration); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/travelcompass/api/feign/Naver/NaverPathService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.travelcompass.api.feign.Naver; | ||
|
||
import com.travelcompass.api.location.service.LocationService; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class NaverPathService { | ||
|
||
private final NaverDurationClient naverDurationClient; | ||
private final String clientId; | ||
private final String clientSecret; | ||
private final LocationService locationService; | ||
|
||
public NaverPathService(NaverDurationClient naverDurationClient, | ||
LocationService locationService, | ||
@Value("${naver.directions.client-id}") String clientId, | ||
@Value("${naver.directions.client-secret}") String clientSecret) { | ||
this.naverDurationClient = naverDurationClient; | ||
this.locationService = locationService; | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
} | ||
|
||
public String getCoordinateById(Long id) { | ||
|
||
String longitude = locationService.findById(id).getLongitude().toString(); | ||
String latitude = locationService.findById(id).getLatitude().toString(); | ||
|
||
return longitude + "," + latitude; | ||
} | ||
|
||
public int getDuration(String start, String goal) { | ||
// 네이버 API에 요청을 보내기 전에 클라이언트 ID와 시크릿을 헤더에 설정 | ||
ResponseEntity<String> response = naverDurationClient.getDuration(start, goal, clientId, clientSecret); | ||
|
||
// API Response로부터 body 뽑아내기 | ||
String body = response.getBody(); | ||
JSONObject json = new JSONObject(body); | ||
|
||
// body에서 duration 추출하기 | ||
int duration = json.getJSONObject("route").getJSONArray("traoptimal") | ||
.getJSONObject(0).getJSONObject("summary").getInt("duration"); | ||
|
||
return duration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters