Skip to content

Commit

Permalink
Merge pull request #15 from hanium-haemil/feature/alert
Browse files Browse the repository at this point in the history
[FEAT] change sending method on alert api
  • Loading branch information
devyubin authored Aug 12, 2023
2 parents 542b9ba + 66fb93c commit c672381
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public ResponseEntity<BaseResponse> sendGetRequest() {

try {
String jsonString = alertService.getAlertInfo(getApiDto);
log.debug("jsonString: "+jsonString);
// log.debug("jsonString: "+jsonString);
alertService.isJson(jsonString);

List<ApiInfoDto> infoList = alertService.ParsingJson(jsonString);
return new BaseResponse<>(infoList).convert();
ApiInfoDto apiInfoDto = alertService.ParsingJson(jsonString);
return new BaseResponse<>(apiInfoDto).convert();
} catch (BaseException e){
// 실패시 custom한 status로 code 헤더 설정, body로 메세지 반환
return new BaseResponse<>(e.getStatus()).convert();
Expand Down
53 changes: 39 additions & 14 deletions src/main/java/com/haemil/backend/alert/service/AlertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.UnknownContentTypeException;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,28 +42,50 @@ public String getAlertInfo(GetApiDto reqGetApiDto) throws BaseException {
String pageNo = reqGetApiDto.getPageNo();
String numOfRows = reqGetApiDto.getNumOfRows();

log.debug("serviceKey: " + serviceKey);

StringBuilder urlBuilder = new StringBuilder(apiUrl);
urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + "=" + serviceKey);
urlBuilder.append("&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode(type, "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode(pageNo, "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "=" + URLEncoder.encode(numOfRows, "UTF-8"));

ResponseEntity<String> response = restTemplate.getForEntity(urlBuilder.toString(), String.class);
// log.debug("urlBuilder: {}", urlBuilder);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "*/*;q=0.9"); // HTTP_ERROR 방지
HttpEntity<String> httpRequest = new HttpEntity<>(null, headers);
// log.debug("httpRequest = {}", httpRequest);

RestTemplate restTemplate = new RestTemplate();
// log.debug("restTemplate = {}", restTemplate);

responseBody = response.getBody();
HttpStatus httpStatus = null;
ResponseEntity<String> httpResponse = null;

URI uri = new URI(urlBuilder.toString()); // service key is not registered 오류 방지
// log.debug("uri = {}", uri);
httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpRequest, new ParameterizedTypeReference<String>(){});
// log.debug("httpResponse = {}", httpResponse);

if (httpResponse != null && httpResponse.getBody() != null) {
responseBody = httpResponse.getBody();
}
// log.debug("responseBody = {}",responseBody);

} catch (UnsupportedEncodingException e) { // 에러가 발생했을 때 예외 status 명시
log.debug("UnsupportedEncodingException 발생 ");
throw new BaseException(ResponseStatus.UNSUPPORTED_ENCODING);
} catch (URISyntaxException e) {
log.debug("URISyntaxException 발생 ");
throw new BaseException(ResponseStatus.UNSUPPORTED_ENCODING);
}

return responseBody;
}

public List<ApiInfoDto> ParsingJson(String responseBody) throws BaseException {
public ApiInfoDto ParsingJson(String responseBody) throws BaseException {
ApiInfoDto apiInfoDto;

List<ApiInfoDto> apiInfoDtoList;
try {
List<AlertApi> alertApiList = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -72,8 +98,8 @@ public List<ApiInfoDto> ParsingJson(String responseBody) throws BaseException {
String msg = nextNode.get("msg").asText();
String location = nextNode.get("location_name").asText(); // 예시로 location_name을 사용하여 location 값을 가져옴

log.debug("msg: " + msg);
log.debug("location: " + location);
// log.debug("msg: " + msg);
// log.debug("location: " + location);

AlertApi alertApi = AlertApi.builder()
.msg(msg)
Expand All @@ -83,19 +109,18 @@ public List<ApiInfoDto> ParsingJson(String responseBody) throws BaseException {
alertApiList.add(alertApi);

// 변환된 데이터를 ApiInfoDto 형태로 리스트로 반환
apiInfoDtoList = new ArrayList<>();
apiInfoDto = new ApiInfoDto();

for (AlertApi a : alertApiList) {
ApiInfoDto apiInfoDto = new ApiInfoDto();
apiInfoDto.setMsg(a.getMsg());
apiInfoDto.setLocation(a.getLocation());
apiInfoDtoList.add(apiInfoDto);
}
log.debug("apiInfoDtoList: " + apiInfoDtoList);
// log.debug("apiInfoDto: " + apiInfoDto);

} catch (JsonProcessingException e) { // 에러가 발생했을 때 예외 status 명시
throw new BaseException(ResponseStatus.CANNOT_CONVERT_JSON);
}
return apiInfoDtoList;
return apiInfoDto;
}

public boolean isJson(String xmlString) throws BaseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum ResponseStatus {
// --- 55x Custom Error --
CANNOT_CONVERT_JSON(false, 550, "JSON 문자열로 변경할 수 없습니다."),
UNSUPPORTED_ENCODING(false, 551, "지원되지 않는 인코딩 형식입니다."),
URI_SYNT(false, 551, "URISyntaxException이 발생했습니다."),
INVALID_XML_FORMAT(false, 552, "SERVICE ERROR가 발생했습니다."),
UNKNOWN_ADDR(false, 553, "알 수 없는 주소를 입력받았습니다.");

Expand Down

0 comments on commit c672381

Please sign in to comment.