Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 길찾기 Client 추가 #115

Merged
merged 14 commits into from
Aug 2, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.piikii.application.domain.course

data class Coordinate(
val x: Double?,
val y: Double?,
) {
fun isValid(): Boolean {
return x != null && y != null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.piikii.application.domain.course

data class Distance(
K-Diger marked this conversation as resolved.
Show resolved Hide resolved
val totalDistanceMeter: Int?,
val totalTimeMinute: Int?,
) {
companion object {
val EMPTY = Distance(null, null)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.piikii.application.port.output.web

import com.piikii.application.domain.course.Coordinate
import com.piikii.application.domain.course.Distance

interface NavigationClient {
fun getDistance(
start: Coordinate,
end: Coordinate,
): Distance
}
1 change: 1 addition & 0 deletions piikii-bootstrap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
implementation(project(":piikii-output-storage:ncp"))
implementation(project(":piikii-output-web:avocado"))
implementation(project(":piikii-output-web:lemon"))
implementation(project(":piikii-output-web:tmap"))

implementation("org.springframework.boot:spring-boot-starter-web")
}
Expand Down
1 change: 1 addition & 0 deletions piikii-bootstrap/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ spring:
- classpath:storage-config/application.yml
- classpath:lemon-config/application.yml
- classpath:avocado-config/application.yml
- classpath:tmap-config/application.yml
application:
name: "piikii"
messages:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ enum class ExceptionCode(
// 500
SECRET_MANAGER_CONFIG_NOT_SET(500, "시크릿 매니저 설정 값이 입력되지 않았습니다."),
URL_PROCESS_ERROR(500, "URL에 해당하는 장소의 정보를 불러오는 중 예기치 못한 오류가 발생했습니다."),
ROUTE_PROCESS_ERROR(500, "경로 거리 정보를 불러오는 중 예기치 못한 오류가 발생했습니다."),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.piikii.output.web.tmap.adapter

import com.piikii.application.domain.course.Coordinate
import com.piikii.application.domain.course.Distance
import com.piikii.application.port.output.web.NavigationClient
import com.piikii.common.exception.ExceptionCode
import com.piikii.common.exception.PiikiiException
import org.springframework.stereotype.Component
import org.springframework.web.client.RestClient
import org.springframework.web.client.body

@Component
class TmapNavigationClient(
private val tmapApiClient: RestClient,
) : NavigationClient {
override fun getDistance(
start: Coordinate,
end: Coordinate,
): Distance {
return if (start.isValid() && end.isValid()) {
getDistanceFromTmap(start, end)
} else {
Distance.EMPTY
}
}

private fun getDistanceFromTmap(
start: Coordinate,
end: Coordinate,
): Distance {
return tmapApiClient.post()
.uri("")
.body(
TmapRouteInfoRequest(
startX = start.x!!,
startY = start.y!!,
endX = end.x!!,
endY = end.y!!,
),
)
.retrieve()
.body<TmapRouteInfoResponse>()
?.toDistance()
?: throw PiikiiException(
exceptionCode = ExceptionCode.ROUTE_PROCESS_ERROR,
detailMessage = "fail to request tmap api call, start(${start.x}, ${start.y}), end(${end.x}, ${end.y})",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.piikii.output.web.tmap.adapter

class TmapRouteInfoRequest(
val startX: Double,
val startY: Double,
val endX: Double,
val endY: Double,
val startName: String? = "default",
val endName: String? = "default",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.piikii.output.web.tmap.adapter

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.piikii.application.domain.course.Distance

@JsonIgnoreProperties(ignoreUnknown = true)
data class TmapRouteInfoResponse(
val type: String,
val features: List<Feature>,
) {
fun toDistance(): Distance {
val featureWithTotalDistance = features.firstOrNull { it.properties.totalDistance != null }
return featureWithTotalDistance?.let {
Distance(
totalDistanceMeter = it.properties.totalDistance,
totalTimeMinute = it.properties.totalTime,
)
} ?: Distance.EMPTY
}

@JsonIgnoreProperties(ignoreUnknown = true)
data class Feature(
val type: String,
val geometry: Geometry,
val properties: Properties,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Geometry(
val type: String,
val coordinates: List<Any>,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Properties(
val totalDistance: Int? = null,
val totalTime: Int? = null,
val index: Int,
val pointIndex: Int? = null,
val name: String? = null,
val description: String,
val direction: String? = null,
val nearPoiName: String? = null,
val nearPoiX: String? = null,
val nearPoiY: String? = null,
val intersectionName: String? = null,
val facilityType: String,
val facilityName: String? = null,
val turnType: Int? = null,
val pointType: String? = null,
val lineIndex: Int? = null,
val distance: Int? = null,
val time: Int? = null,
val roadType: Int? = null,
val categoryRoadType: Int? = null,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.piikii.output.web.tmap.config

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.web.client.RestClient

@Configuration
@EnableConfigurationProperties(TmapProperties::class)
class TmapConfig {
@Bean
fun tmapApiClient(tmapProperties: TmapProperties): RestClient {
return RestClient.builder()
.baseUrl(tmapProperties.url.api)
.defaultHeaders {
it.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
it.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
it.add("appKey", tmapProperties.key.app)
}
.build()
}
}

@ConfigurationProperties(prefix = "tmap")
data class TmapProperties(
val url: TmapUrl,
val key: TmapKey,
)

data class TmapUrl(
val api: String,
)

data class TmapKey(
val app: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tmap:
url:
api: ${TMAP_API_URL}
key:
app: ${TMAP_APP_KEY}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.piikii.output.web.tmap

import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.piikii.output.web.tmap")
class TestConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.piikii.output.web.tmap

import com.piikii.application.domain.course.Coordinate
import com.piikii.output.web.tmap.adapter.TmapNavigationClient
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration

@Disabled
thguss marked this conversation as resolved.
Show resolved Hide resolved
@SpringBootTest
@ActiveProfiles("test")
@ContextConfiguration(classes = [TestConfiguration::class])
class TmapNavigationClientTest {
@Autowired
lateinit var tmapNavigationClient: TmapNavigationClient

@Test
fun getDistanceTest() {
val start = Coordinate(x = 126.9246033, y = 33.45241976)
val end = Coordinate(x = 126.9041895, y = 33.4048969)

val distance = tmapNavigationClient.getDistance(start, end)
println("distance = $distance")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tmap:
url:
api: ${TMAP_API_URL}
key:
app: ${TMAP_APP_KEY}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ include("piikii-output-storage:ncp")
include("piikii-output-web")
include("piikii-output-web:avocado")
include("piikii-output-web:lemon")
include("piikii-output-web:tmap")