-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kakao-navi-api 연동 DistanceSearchService 및 distanceInfo Dto생성
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...end/src/main/java/com/team14/cherrybnb/revervation/application/DistanceSearchService.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,58 @@ | ||
package com.team14.cherrybnb.revervation.application; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.team14.cherrybnb.revervation.dto.DistanceInfo; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Service | ||
public class DistanceSearchService { | ||
|
||
@Value("${kakao.rest.api.key}") | ||
private String kakaoKey; | ||
|
||
public DistanceInfo navi(double originX, double originY, double destinationX, double destionationY) throws JsonProcessingException { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
String url = getUrl(originX, originY, destinationX, destionationY); | ||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHeaders()), String.class); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
JsonNode body = objectMapper.readTree(response.getBody()); | ||
JsonNode summary = body.get("Summary"); | ||
double distance = summary.get("distance").asDouble(); | ||
double duration = summary.get("duration").asDouble(); | ||
|
||
return new DistanceInfo(distance, duration); | ||
} | ||
|
||
private MultiValueMap<String, String> getHeaders() { | ||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
headers.add("host", "apis-navi.kakaomobility.com"); | ||
headers.add("Authorization", "KakaoAK " + kakaoKey); | ||
|
||
return headers; | ||
} | ||
|
||
private String getUrl(double originX, double originY, double destinationX, double destinationY) { | ||
String url = "https://apis-navi.kakaomobility.com/v1/directions"; | ||
String origin = originX + ", " + originY; | ||
String destination = destinationX + ", " + destinationY; | ||
return UriComponentsBuilder.fromHttpUrl(url) | ||
.queryParam("origin", origin) | ||
.queryParam("destination", destination) | ||
.queryParam("summary", true) | ||
.encode() | ||
.toUriString(); | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/com/team14/cherrybnb/revervation/dto/DistanceInfo.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,23 @@ | ||
package com.team14.cherrybnb.revervation.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class DistanceInfo { | ||
|
||
private double distance; | ||
private double duration; | ||
|
||
public DistanceInfo(double distance, double duration) { | ||
this.distance = toKm(distance); | ||
this.duration = toMin(duration); | ||
} | ||
|
||
private double toKm(double distance) { | ||
return distance/1000; | ||
} | ||
|
||
private double toMin(double duration) { | ||
return duration/60; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -27,3 +27,5 @@ spring: | |
|
||
logging.level: | ||
org.hibernate.SQL: debug | ||
|
||
kakao.rest.api.key: ${KAKAO_REST_KEY} |