Skip to content

Commit

Permalink
feat: kakao-navi-api 연동 DistanceSearchService 및 distanceInfo Dto생성
Browse files Browse the repository at this point in the history
  • Loading branch information
CMSSKKK committed Jun 1, 2022
1 parent b469744 commit b216c59
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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();

}
}
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;
}
}
2 changes: 2 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ spring:

logging.level:
org.hibernate.SQL: debug

kakao.rest.api.key: ${KAKAO_REST_KEY}

0 comments on commit b216c59

Please sign in to comment.