Skip to content

Commit d2f80be

Browse files
committed
Feat(#31): OpenweatherClient 기능 구현
Resolvers: #31
1 parent c679f95 commit d2f80be

File tree

4 files changed

+70
-64
lines changed

4 files changed

+70
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,8 @@
11
package com.wypl.openweatherclient;
22

3-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4-
import org.springframework.http.ResponseEntity;
5-
import org.springframework.stereotype.Component;
6-
import org.springframework.web.client.RestTemplate;
7-
83
import com.wypl.openweatherclient.data.OpenWeatherCond;
94
import com.wypl.openweatherclient.data.OpenWeatherResponse;
10-
import com.wypl.openweatherclient.exception.OpenWeatherErrorCode;
11-
import com.wypl.openweatherclient.exception.OpenWeatherException;
12-
import com.wypl.openweatherclient.type.WeatherRegion;
13-
14-
import lombok.RequiredArgsConstructor;
15-
16-
@EnableConfigurationProperties(OpenWeatherProperties.class)
17-
@RequiredArgsConstructor
18-
@Component
19-
public class OpenWeatherClient {
20-
21-
private final RestTemplate restTemplate;
22-
private final OpenWeatherProperties properties;
23-
24-
public OpenWeatherResponse fetchWeather(OpenWeatherCond cond) {
25-
String url = getUrl(cond);
26-
ResponseEntity<OpenWeatherResponse> response = restTemplate.getForEntity(
27-
url,
28-
OpenWeatherResponse.class
29-
);
30-
if (response.getStatusCode().is2xxSuccessful()) {
31-
return response.getBody();
32-
}
33-
if (response.getStatusCode().is5xxServerError()) {
34-
throw new OpenWeatherException(OpenWeatherErrorCode.INTERNAL_SERVER_ERROR);
35-
}
36-
throw new OpenWeatherException(OpenWeatherErrorCode.INVALID_OPEN_WEATHER_REQUEST);
37-
}
38-
39-
private String getUrl(OpenWeatherCond cond) {
40-
StringBuilder url = new StringBuilder(properties.getBaseUrl())
41-
.append("?appid=")
42-
.append(properties.getKey());
43-
44-
addParamByCity(url, cond.city());
45-
addParamByLang(url, cond.isLangKr());
46-
addParamByUnits(url, cond.isMetric());
47-
48-
return url.toString();
49-
}
50-
51-
private void addParamByCity(StringBuilder url, WeatherRegion region) {
52-
url.append("&q=").append(region.getCityEn());
53-
}
54-
55-
private void addParamByLang(StringBuilder url, boolean isLangKr) {
56-
if (isLangKr) {
57-
url.append("&lang=kr");
58-
}
59-
}
605

61-
private void addParamByUnits(StringBuilder url, boolean isMetric) {
62-
if (isMetric) {
63-
url.append("&units=metric");
64-
}
65-
}
6+
public interface OpenWeatherClient {
7+
OpenWeatherResponse fetchWeather(OpenWeatherCond cond);
668
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.wypl.openweatherclient;
2+
3+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
import com.wypl.openweatherclient.data.OpenWeatherCond;
9+
import com.wypl.openweatherclient.data.OpenWeatherResponse;
10+
import com.wypl.openweatherclient.exception.OpenWeatherErrorCode;
11+
import com.wypl.openweatherclient.exception.OpenWeatherException;
12+
import com.wypl.openweatherclient.type.WeatherRegion;
13+
14+
import lombok.RequiredArgsConstructor;
15+
16+
@EnableConfigurationProperties(OpenWeatherProperties.class)
17+
@RequiredArgsConstructor
18+
@Component
19+
public class OpenWeatherClientImpl implements OpenWeatherClient {
20+
21+
private final RestTemplate restTemplate;
22+
private final OpenWeatherProperties properties;
23+
24+
public OpenWeatherResponse fetchWeather(OpenWeatherCond cond) {
25+
String url = getUrl(cond);
26+
ResponseEntity<OpenWeatherResponse> response = restTemplate.getForEntity(
27+
url,
28+
OpenWeatherResponse.class
29+
);
30+
if (response.getStatusCode().is2xxSuccessful()) {
31+
return response.getBody();
32+
}
33+
if (response.getStatusCode().is5xxServerError()) {
34+
throw new OpenWeatherException(OpenWeatherErrorCode.INTERNAL_SERVER_ERROR);
35+
}
36+
throw new OpenWeatherException(OpenWeatherErrorCode.INVALID_OPEN_WEATHER_REQUEST);
37+
}
38+
39+
private String getUrl(OpenWeatherCond cond) {
40+
StringBuilder url = new StringBuilder(properties.getBaseUrl())
41+
.append("?appid=")
42+
.append(properties.getKey());
43+
44+
addParamByCity(url, cond.city());
45+
addParamByLang(url, cond.isLangKr());
46+
addParamByUnits(url, cond.isMetric());
47+
48+
return url.toString();
49+
}
50+
51+
private void addParamByCity(StringBuilder url, WeatherRegion region) {
52+
url.append("&q=").append(region.getCityEn());
53+
}
54+
55+
private void addParamByLang(StringBuilder url, boolean isLangKr) {
56+
if (isLangKr) {
57+
url.append("&lang=kr");
58+
}
59+
}
60+
61+
private void addParamByUnits(StringBuilder url, boolean isMetric) {
62+
if (isMetric) {
63+
url.append("&units=metric");
64+
}
65+
}
66+
}

client/openweather-client/src/main/java/com/wypl/openweatherclient/exception/OpenWeatherErrorCode.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
@Getter
88
public enum OpenWeatherErrorCode implements ServerErrorCode {
99
INVALID_OPEN_WEATHER_REQUEST(500, "OPEN_WEATHER_001", "외부 서비스의 오류입니다."),
10-
INTERNAL_SERVER_ERROR(400, "OPEN_WEATHER_002", "올바르지 않은 요청입니다."),
11-
;
10+
INTERNAL_SERVER_ERROR(400, "OPEN_WEATHER_002", "올바르지 않은 요청입니다.");
1211

1312
private final int statusCode;
1413
private final String errorCode;

client/openweather-client/src/main/java/com/wypl/openweatherclient/type/WeatherType.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public enum WeatherType {
2323
MIST(6, new HashSet<>(List.of(701, 711, 721, 731, 741, 751, 761, 762)), "안개"),
2424
SQUALL(7, new HashSet<>(List.of(771)), "스콜"),
2525
TORNADO(8, new HashSet<>(List.of(781)), "태풍"),
26-
THUNDERSTORM(9, new HashSet<>(List.of(200, 201, 202, 210, 211, 212, 221, 230, 231, 232)), "천둥"),
27-
;
26+
THUNDERSTORM(9, new HashSet<>(List.of(200, 201, 202, 210, 211, 212, 221, 230, 231, 232)), "천둥");
2827

2928
private final int weatherId;
3029
private final Set<Integer> ids;

0 commit comments

Comments
 (0)