diff --git a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java index c79e3d6c..c37eae4e 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java +++ b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import com.waither.weatherservice.dto.AccuweatherTestRequest; import com.waither.weatherservice.dto.AirTestRequest; import com.waither.weatherservice.dto.ForeCastTestRequest; import com.waither.weatherservice.dto.MsgTestRequest; @@ -38,7 +39,12 @@ public void createDisasterMsgTest(@RequestBody MsgTestRequest request) throws UR } @PostMapping("/air") - public void airKoreaTest(@RequestBody AirTestRequest request) throws URISyntaxException, IOException { + public void airKoreaTest(@RequestBody AirTestRequest request) throws URISyntaxException { weatherService.createAirKorea(request.searchDate()); } + + @PostMapping("/accuweather") + public void accuweatherTest(@RequestBody AccuweatherTestRequest request) throws URISyntaxException, IOException { + weatherService.convertLocation(request.latitude(), request.longitude()); + } } diff --git a/weather-service/src/main/java/com/waither/weatherservice/dto/AccuweatherTestRequest.java b/weather-service/src/main/java/com/waither/weatherservice/dto/AccuweatherTestRequest.java new file mode 100644 index 00000000..b61db344 --- /dev/null +++ b/weather-service/src/main/java/com/waither/weatherservice/dto/AccuweatherTestRequest.java @@ -0,0 +1,7 @@ +package com.waither.weatherservice.dto; + +public record AccuweatherTestRequest( + double latitude, + double longitude +) { +} diff --git a/weather-service/src/main/java/com/waither/weatherservice/openapi/AccuweatherLocationApiResponse.java b/weather-service/src/main/java/com/waither/weatherservice/openapi/AccuweatherLocationApiResponse.java new file mode 100644 index 00000000..e978419b --- /dev/null +++ b/weather-service/src/main/java/com/waither/weatherservice/openapi/AccuweatherLocationApiResponse.java @@ -0,0 +1,170 @@ +package com.waither.weatherservice.openapi; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@NoArgsConstructor // 역직렬화를 위한 기본 생성자 +@JsonIgnoreProperties(ignoreUnknown = true) +@ToString +public class AccuweatherLocationApiResponse { + + @JsonProperty("Version") + private int version; + @JsonProperty("Key") + private String key; + @JsonProperty("Type") + private String type; + @JsonProperty("Rank") + private int rank; + @JsonProperty("LocalizedName") + private String localizedName; + @JsonProperty("EnglishName") + private String englishName; + @JsonProperty("PrimaryPostalCode") + private String primaryPostalCode; + @JsonProperty("Region") + private Region region; + @JsonProperty("Country") + private Country country; + @JsonProperty("AdministrativeArea") + private AdministrativeArea administrativeArea; + @JsonProperty("TimeZone") + private TimeZone timeZone; + @JsonProperty("GeoPosition") + private GeoPosition geoPosition; + @JsonProperty("IsAlias") + private boolean isAlias; + @JsonProperty("ParentCity") + private ParentCity parentCity; + @JsonProperty("SupplementalAdminAreas") + private List supplementalAdminAreas; + @JsonProperty("DataSets") + private List dataSets; + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Region { + @JsonProperty("ID") + private String id; + @JsonProperty("LocalizedName") + private String localizedName; + @JsonProperty("EnglishName") + private String englishName; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Country { + @JsonProperty("ID") + private String id; + @JsonProperty("LocalizedName") + private String localizedName; + @JsonProperty("EnglishName") + private String englishName; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class AdministrativeArea { + @JsonProperty("ID") + private String id; + @JsonProperty("LocalizedName") + private String localizedName; + @JsonProperty("EnglishName") + private String englishName; + @JsonProperty("Level") + private int level; + @JsonProperty("LocalizedType") + private String localizedType; + @JsonProperty("EnglishType") + private String englishType; + @JsonProperty("CountryID") + private String countryID; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class TimeZone { + @JsonProperty("Code") + private String code; + @JsonProperty("Name") + private String name; + @JsonProperty("GmtOffset") + private int gmtOffset; + @JsonProperty("IsDaylightSaving") + private boolean isDaylightSaving; + @JsonProperty("NextOffsetChange") + private String nextOffsetChange; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class GeoPosition { + @JsonProperty("Latitude") + private double latitude; + @JsonProperty("Longitude") + private double longitude; + @JsonProperty("Elevation") + private Elevation elevation; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Elevation { + @JsonProperty("Metric") + private Metric metric; + @JsonProperty("Imperial") + private Imperial imperial; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Metric { + @JsonProperty("Value") + private int value; + @JsonProperty("Unit") + private String unit; + @JsonProperty("UnitType") + private int unitType; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Imperial { + @JsonProperty("Value") + private int value; + @JsonProperty("Unit") + private String unit; + @JsonProperty("UnitType") + private int unitType; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class ParentCity { + @JsonProperty("Key") + private String key; + @JsonProperty("LocalizedName") + private String localizedName; + @JsonProperty("EnglishName") + private String englishName; + } + + @Getter + @JsonIgnoreProperties(ignoreUnknown = true) + public static class SupplementalAdminAreas { + @JsonProperty("Level") + private int level; + @JsonProperty("LocalizedName") + private String localizedName; + @JsonProperty("EnglishName") + private String englishName; + } +} diff --git a/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java b/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java index 2786f5f2..cffc1825 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java +++ b/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; @@ -32,6 +33,9 @@ public class OpenApiUtil { @Value("${openapi.disasterMsg.key}") private String disasterMsgKey; + @Value("${openapi.accuweather.key}") + private String accuweatherKey; + // 기상청 Api (초단기, 단기) public List callForeCastApi( int nx, @@ -188,4 +192,38 @@ public Map parseAirKoreaResponseToMap(String data) { return map; } + + // Accuweather Api 호출 + public String callAccuweatherLocationApi(double latitude, double longitude) throws + URISyntaxException, + JsonProcessingException { + + WebClient webClient = WebClient.create(); + String uriString = "http://dataservice.accuweather.com/locations/v1/cities/geoposition/search" + + "?apikey=" + accuweatherKey + + "&q=" + latitude + "," + longitude + + "&language=" + "ko-kr" + + "&details=" + "false" + + "&toplevel=" + "false"; + + URI uri = new URI(uriString); + + log.info("[*] Accuweather Location Api : {}", uri); + + String jsonString = webClient.get() + .uri(uri) + .accept(MediaType.APPLICATION_JSON) + .retrieve().bodyToMono(String.class) + .onErrorResume(throwable -> { + throw new OpenApiException(RESPONSE_EXCEPTION_MSG); + }) + .block(); + + ObjectMapper objectMapper = new ObjectMapper(); + AccuweatherLocationApiResponse response = objectMapper.readValue(jsonString, + AccuweatherLocationApiResponse.class); + + log.info("[*] 위도, 경도 -> 지역명 : {}", response.getAdministrativeArea().getLocalizedName()); + return response.getAdministrativeArea().getLocalizedName(); + } } diff --git a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java index 50a04744..8e47a67f 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java +++ b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.fasterxml.jackson.core.JsonProcessingException; import com.waither.weatherservice.entity.DailyWeather; import com.waither.weatherservice.entity.DisasterMessage; import com.waither.weatherservice.entity.ExpectedWeather; @@ -27,7 +28,6 @@ public class WeatherService { private final OpenApiUtil openApiUtil; - private final DailyWeatherRepository dailyWeatherRepository; private final ExpectedWeatherRepository expectedWeatherRepository; private final DisasterMessageRepository disasterMessageRepository; @@ -120,4 +120,8 @@ public void createDisasterMsg(String location) throws URISyntaxException, IOExce public void createAirKorea(String searchTime) throws URISyntaxException { openApiUtil.callAirKorea(searchTime); } + + public void convertLocation(double latitude, double longitude) throws URISyntaxException, JsonProcessingException { + openApiUtil.callAccuweatherLocationApi(latitude, longitude); + } }