Skip to content

Commit 5971567

Browse files
committed
feat(be): URL 빌더 구현 및 테스트 컨트롤러 추가 (#61)
1 parent 88a7c15 commit 5971567

File tree

7 files changed

+83
-34
lines changed

7 files changed

+83
-34
lines changed

src/main/kotlin/com/back/koreaTravelGuide/domain/ai/weather/client/WeatherApiClient.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package com.back.koreaTravelGuide.domain.ai.weather.client
22

33
// TODO: 기상청 API 클라이언트 - HTTP 요청으로 날씨 데이터 조회 및 JSON 파싱
44
import com.back.koreaTravelGuide.common.logging.log
5+
import com.back.koreaTravelGuide.domain.ai.weather.client.builder.UrlBuilder
56
import com.back.koreaTravelGuide.domain.ai.weather.client.parser.DataParser
67
import com.back.koreaTravelGuide.domain.ai.weather.client.tools.Tools
78
import com.back.koreaTravelGuide.domain.ai.weather.dto.LandForecastData
89
import com.back.koreaTravelGuide.domain.ai.weather.dto.TemperatureData
9-
import org.springframework.beans.factory.annotation.Value
1010
import org.springframework.stereotype.Component
1111
import org.springframework.web.client.RestTemplate
1212

@@ -15,16 +15,15 @@ class WeatherApiClient(
1515
private val restTemplate: RestTemplate,
1616
private val tools: Tools,
1717
private val dataParser: DataParser,
18-
@Value("\${weather.api.key}") private val serviceKey: String,
19-
@Value("\${weather.api.base-url}") private val apiUrl: String,
18+
private val builder: UrlBuilder,
2019
) {
2120
// 1. 중기전망조회 (getMidFcst) - 텍스트 기반 전망
2221
fun fetchMidForecast(
2322
regionId: String,
2423
baseTime: String,
2524
): String? {
2625
val stnId = tools.getStnIdFromRegionCode(regionId)
27-
val url = "$apiUrl/getMidFcst?serviceKey=$serviceKey&numOfRows=10&pageNo=1&stnId=$stnId&tmFc=$baseTime&dataType=JSON"
26+
val url = builder.buildMidFcstUrl(stnId, baseTime)
2827

2928
return try {
3029
@Suppress("UNCHECKED_CAST")
@@ -51,7 +50,7 @@ class WeatherApiClient(
5150
regionId: String,
5251
baseTime: String,
5352
): TemperatureData? {
54-
val url = "$apiUrl/getMidTa?serviceKey=$serviceKey&numOfRows=10&pageNo=1&regId=$regionId&tmFc=$baseTime&dataType=JSON"
53+
val url = builder.buildMidTaUrl(regionId, baseTime)
5554

5655
return try {
5756
@Suppress("UNCHECKED_CAST")
@@ -69,7 +68,7 @@ class WeatherApiClient(
6968
regionId: String,
7069
baseTime: String,
7170
): LandForecastData? {
72-
val url = "$apiUrl/getMidLandFcst?serviceKey=$serviceKey&numOfRows=10&pageNo=1&regId=$regionId&tmFc=$baseTime&dataType=JSON"
71+
val url = builder.buildMidLandFcstUrl(regionId, baseTime)
7372

7473
return try {
7574
@Suppress("UNCHECKED_CAST")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.back.koreaTravelGuide.domain.ai.weather.client.builder
2+
3+
import org.springframework.beans.factory.annotation.Value
4+
import org.springframework.stereotype.Component
5+
import org.springframework.web.util.UriComponentsBuilder
6+
7+
@Component
8+
class UrlBuilder(
9+
@Value("\${weather.api.key}") private val serviceKey: String,
10+
@Value("\${weather.api.base-url}") private val apiUrl: String,
11+
) {
12+
fun buildMidFcstUrl(
13+
stnId: String,
14+
tmFc: String,
15+
): String =
16+
UriComponentsBuilder.fromUriString("$apiUrl/getMidFcst")
17+
.queryParam("pageNo", 1)
18+
.queryParam("numOfRows", 10)
19+
.queryParam("dataType", "JSON")
20+
.queryParam("stnId", stnId)
21+
.queryParam("tmFc", tmFc)
22+
.queryParam("authKey", serviceKey)
23+
.toUriString()
24+
25+
fun buildMidTaUrl(
26+
regId: String,
27+
tmFc: String,
28+
): String =
29+
UriComponentsBuilder.fromUriString("$apiUrl/getMidTa")
30+
.queryParam("pageNo", 1)
31+
.queryParam("numOfRows", 10)
32+
.queryParam("dataType", "JSON")
33+
.queryParam("regId", regId)
34+
.queryParam("tmFc", tmFc)
35+
.queryParam("authKey", serviceKey)
36+
.toUriString()
37+
38+
fun buildMidLandFcstUrl(
39+
regId: String,
40+
tmFc: String,
41+
): String =
42+
UriComponentsBuilder.fromUriString("$apiUrl/getMidLandFcst")
43+
.queryParam("pageNo", 1)
44+
.queryParam("numOfRows", 10)
45+
.queryParam("dataType", "JSON")
46+
.queryParam("regId", regId)
47+
.queryParam("tmFc", tmFc)
48+
.queryParam("authKey", serviceKey)
49+
.toUriString()
50+
}

src/main/kotlin/com/back/koreaTravelGuide/domain/ai/weather/config/RegionCodeProperties.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.back.koreaTravelGuide.domain.ai.weather.controller
2+
3+
import com.back.koreaTravelGuide.domain.ai.weather.dto.MidForecastDto
4+
import com.back.koreaTravelGuide.domain.ai.weather.dto.TemperatureAndLandForecastDto
5+
import com.back.koreaTravelGuide.domain.ai.weather.service.WeatherService
6+
import org.springframework.web.bind.annotation.GetMapping
7+
import org.springframework.web.bind.annotation.RequestMapping
8+
import org.springframework.web.bind.annotation.RestController
9+
10+
@RestController
11+
@RequestMapping("/weather")
12+
class TestController(
13+
private val weatherService: WeatherService,
14+
) {
15+
@GetMapping("/test1")
16+
fun test1(): List<MidForecastDto>? {
17+
return weatherService.getWeatherForecast()
18+
}
19+
20+
@GetMapping("/test2")
21+
fun test2(): List<TemperatureAndLandForecastDto>? {
22+
return weatherService.getTemperatureAndLandForecast("11B10101")
23+
}
24+
}

src/main/kotlin/com/back/koreaTravelGuide/domain/ai/weather/service/WeatherService.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ class WeatherService(
1616
return weatherServiceCore.fetchMidForecast(actualBaseTime)
1717
}
1818

19-
fun getTemperatureAndLandForecast(location: String?): List<TemperatureAndLandForecastDto>? {
20-
val actualLocation = location ?: "서울"
21-
val actualRegionCode = tools.getRegionCodeFromLocation(actualLocation)
22-
19+
fun getTemperatureAndLandForecast(actualRegionCode: String): List<TemperatureAndLandForecastDto>? {
2320
val actualBaseTime = tools.getCurrentBaseTime()
2421

2522
return weatherServiceCore.fetchTemperatureAndLandForecast(actualRegionCode, actualBaseTime)

src/main/kotlin/com/back/koreaTravelGuide/domain/ai/weather/service/tools/Tools.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package com.back.koreaTravelGuide.domain.ai.weather.service.tools
22

3-
import com.back.koreaTravelGuide.domain.ai.weather.config.RegionCodeProperties
43
import org.springframework.stereotype.Component
54
import java.time.ZoneId
65
import java.time.ZonedDateTime
76
import java.time.format.DateTimeFormatter
87

98
@Component("serviceTools")
10-
class Tools(
11-
private val regionCodeProperties: RegionCodeProperties,
12-
) {
13-
fun getRegionCodeFromLocation(location: String): String {
14-
return regionCodeProperties.getCodeByLocation(location)
15-
}
16-
9+
class Tools() {
1710
fun getCurrentBaseTime(): String {
1811
val now = ZonedDateTime.now(ZoneId.of("Asia/Seoul"))
1912
val baseHour =

src/main/resources/application.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ springdoc:
131131
# Weather API 설정
132132
weather:
133133
api:
134-
key: ${WEATHER_API_KEY}
135-
base-url: http://apis.data.go.kr/1360000/MidFcstInfoService
134+
key: ${WEATHER__API__KEY}
135+
base-url: https://apihub.kma.go.kr/api/typ02/openApi/MidFcstInfoService
136136

137137
# Tour API 설정
138138
tour:

0 commit comments

Comments
 (0)