Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.back.koreaTravelGuide.domain.ai.weather.client

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

Expand All @@ -15,16 +15,15 @@ class WeatherApiClient(
private val restTemplate: RestTemplate,
private val tools: Tools,
private val dataParser: DataParser,
@Value("\${weather.api.key}") private val serviceKey: String,
@Value("\${weather.api.base-url}") private val apiUrl: String,
private val builder: UrlBuilder,
) {
// 1. 중기전망조회 (getMidFcst) - 텍스트 기반 전망
fun fetchMidForecast(
regionId: String,
baseTime: String,
): String? {
val stnId = tools.getStnIdFromRegionCode(regionId)
val url = "$apiUrl/getMidFcst?serviceKey=$serviceKey&numOfRows=10&pageNo=1&stnId=$stnId&tmFc=$baseTime&dataType=JSON"
val url = builder.buildMidFcstUrl(stnId, baseTime)

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

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

return try {
@Suppress("UNCHECKED_CAST")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.back.koreaTravelGuide.domain.ai.weather.client.builder

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.web.util.UriComponentsBuilder

@Component
class UrlBuilder(
@Value("\${weather.api.key}") private val serviceKey: String,
@Value("\${weather.api.base-url}") private val apiUrl: String,
) {
fun buildMidFcstUrl(
stnId: String,
tmFc: String,
): String =
UriComponentsBuilder.fromUriString("$apiUrl/getMidFcst")
.queryParam("pageNo", 1)
.queryParam("numOfRows", 10)
.queryParam("dataType", "JSON")
.queryParam("stnId", stnId)
.queryParam("tmFc", tmFc)
.queryParam("authKey", serviceKey)
.toUriString()

fun buildMidTaUrl(
regId: String,
tmFc: String,
): String =
UriComponentsBuilder.fromUriString("$apiUrl/getMidTa")
.queryParam("pageNo", 1)
.queryParam("numOfRows", 10)
.queryParam("dataType", "JSON")
.queryParam("regId", regId)
.queryParam("tmFc", tmFc)
.queryParam("authKey", serviceKey)
.toUriString()

fun buildMidLandFcstUrl(
regId: String,
tmFc: String,
): String =
UriComponentsBuilder.fromUriString("$apiUrl/getMidLandFcst")
.queryParam("pageNo", 1)
.queryParam("numOfRows", 10)
.queryParam("dataType", "JSON")
.queryParam("regId", regId)
.queryParam("tmFc", tmFc)
.queryParam("authKey", serviceKey)
.toUriString()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.back.koreaTravelGuide.domain.ai.weather.controller

import com.back.koreaTravelGuide.domain.ai.weather.dto.MidForecastDto
import com.back.koreaTravelGuide.domain.ai.weather.dto.TemperatureAndLandForecastDto
import com.back.koreaTravelGuide.domain.ai.weather.service.WeatherService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/weather")
class TestController(
private val weatherService: WeatherService,
) {
@GetMapping("/test1")
fun test1(): List<MidForecastDto>? {
return weatherService.getWeatherForecast()
}

@GetMapping("/test2")
fun test2(): List<TemperatureAndLandForecastDto>? {
return weatherService.getTemperatureAndLandForecast("11B10101")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ class WeatherService(
return weatherServiceCore.fetchMidForecast(actualBaseTime)
}

fun getTemperatureAndLandForecast(location: String?): List<TemperatureAndLandForecastDto>? {
val actualLocation = location ?: "서울"
val actualRegionCode = tools.getRegionCodeFromLocation(actualLocation)

fun getTemperatureAndLandForecast(actualRegionCode: String): List<TemperatureAndLandForecastDto>? {
val actualBaseTime = tools.getCurrentBaseTime()

return weatherServiceCore.fetchTemperatureAndLandForecast(actualRegionCode, actualBaseTime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package com.back.koreaTravelGuide.domain.ai.weather.service.tools

import com.back.koreaTravelGuide.domain.ai.weather.config.RegionCodeProperties
import org.springframework.stereotype.Component
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

@Component("serviceTools")
class Tools(
private val regionCodeProperties: RegionCodeProperties,
) {
fun getRegionCodeFromLocation(location: String): String {
return regionCodeProperties.getCodeByLocation(location)
}

class Tools() {
fun getCurrentBaseTime(): String {
val now = ZonedDateTime.now(ZoneId.of("Asia/Seoul"))
val baseHour =
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ springdoc:
# Weather API 설정
weather:
api:
key: ${WEATHER_API_KEY}
base-url: http://apis.data.go.kr/1360000/MidFcstInfoService
key: ${WEATHER__API__KEY}
base-url: https://apihub.kma.go.kr/api/typ02/openApi/MidFcstInfoService

# Tour API 설정
tour:
Expand Down