프로젝트 개요
기능
- 레오폴드 홈페이지에서 공지사항을 읽어와서 새로운 공지사항 혹은 변경된 공지사항이 있다면 슬랙으로 알림이 온다.
목적
- 레오폴드에서는 제품 입고를 공지사항을 통해 알린다. 내가 원하는 제품이 입고가 되었는지 직접 확인하는 것이 아니라, 입고가 되었을때 슬랙에 자동으로 메세지를 전송해서 알림을 받을 수 있도록 구현한다. 이를 통해 직접 공지사항을 확인해야하는 번거로움을 줄일 수 있다.
방법
- Retrofit2 를 이용한 HTTP 통신
- Parser
- Slack api 사용
- slack의 레오폴드 채널에 post message
1. Leopold 공지사항 가져오기
참고
retrofit2 3요소
- interface
- 응답을 바로 string으로 매칭시킬 것이기 때문에
ResponseBody
클래스 사용
public interface LeopoldApi{
@GET("Shop/Board.php?BoTable=Notice")
Call<ResponseBody> getLeopoldNotice();
}
- retrofit2 구현
- html페이지를 String 으로 읽어올 것이기 때문에
converter
를 추가하지 않음.
execute().body().string()
으로 바로 응답 페이지를 반환.
@Component
@Slf4j
public class LeopoldApiCaller {
private final LeopoldApileopoldApi;
public LeopoldApiCaller(@Value("${api.leopold.base-url}") StringbaseUrl) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.build();
leopoldApi = retrofit.create(LeopoldApi.class);
}
public String getLeopoldNotice() {
try {
var call = leopoldApi.getLeopoldNotice();
return call.execute().body().string();
} catch (IOExceptione) {
log.error("Get Leopold Notice Exception : " +e.getMessage());
throw new RuntimeException("Get Leopold Notice Exception : " +e.getMessage());
}
}
}