Skip to content

Commit

Permalink
✨ feat: Accuweather Location Api 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored Apr 14, 2024
2 parents c15673f + fb82e85 commit efbe645
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.waither.weatherservice.dto;

public record AccuweatherTestRequest(
double latitude,
double longitude
) {
}
Original file line number Diff line number Diff line change
@@ -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> supplementalAdminAreas;
@JsonProperty("DataSets")
private List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,9 @@ public class OpenApiUtil {
@Value("${openapi.disasterMsg.key}")
private String disasterMsgKey;

@Value("${openapi.accuweather.key}")
private String accuweatherKey;

// 기상청 Api (초단기, 단기)
public List<ForeCastOpenApiResponse.Item> callForeCastApi(
int nx,
Expand Down Expand Up @@ -188,4 +192,38 @@ public Map<String, String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +28,6 @@
public class WeatherService {

private final OpenApiUtil openApiUtil;

private final DailyWeatherRepository dailyWeatherRepository;
private final ExpectedWeatherRepository expectedWeatherRepository;
private final DisasterMessageRepository disasterMessageRepository;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit efbe645

Please sign in to comment.