|
| 1 | +package eatda.client.map; |
| 2 | + |
| 3 | +import eatda.domain.store.Coordinates; |
| 4 | +import java.util.List; |
| 5 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 6 | +import org.springframework.http.HttpStatusCode; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | +import org.springframework.web.client.RestClient; |
| 9 | + |
| 10 | +@Component |
| 11 | +@EnableConfigurationProperties(KakaoProperties.class) |
| 12 | +public class MapClient { |
| 13 | + |
| 14 | + private final RestClient restClient; |
| 15 | + private final KakaoProperties kakaoProperties; |
| 16 | + |
| 17 | + public MapClient(RestClient.Builder restClient, KakaoProperties properties) { |
| 18 | + this.restClient = restClient |
| 19 | + .defaultStatusHandler(HttpStatusCode::is5xxServerError, new MapServerErrorHandler()) |
| 20 | + .build(); |
| 21 | + this.kakaoProperties = properties; |
| 22 | + } |
| 23 | + |
| 24 | + public List<StoreSearchResult> searchShops(String query) { |
| 25 | + return restClient.get() |
| 26 | + .uri("https://dapi.kakao.com/v2/local/search/keyword.json", builder -> builder |
| 27 | + .queryParam("query", query) |
| 28 | + .queryParam("category", "FD6") |
| 29 | + .queryParam("rect", "%s,%s,%s,%s".formatted( |
| 30 | + Coordinates.getMinLongitude(), Coordinates.getMinLatitude(), |
| 31 | + Coordinates.getMaxLongitude(), Coordinates.getMaxLatitude())) |
| 32 | + .queryParam("page", 1) |
| 33 | + .queryParam("size", 15) |
| 34 | + .queryParam("sort", "accuracy") |
| 35 | + .build()) |
| 36 | + .header("Authorization", "KakaoAK " + kakaoProperties.getApiKey()) |
| 37 | + .retrieve() |
| 38 | + .body(StoreSearchResults.class) |
| 39 | + .results(); |
| 40 | + } |
| 41 | +} |
0 commit comments