-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from KU-Stacks/develop
version 2.0.1
- Loading branch information
Showing
25 changed files
with
228 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 11 additions & 11 deletions
22
src/main/java/com/kustacks/kuring/config/MappedBeanConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
package com.kustacks.kuring.config; | ||
|
||
import com.kustacks.kuring.kuapi.CategoryName; | ||
import com.kustacks.kuring.kuapi.api.notice.NoticeAPIClient; | ||
import com.kustacks.kuring.kuapi.api.notice.NoticeApiClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class MappedBeanConfig { | ||
|
||
private final NoticeAPIClient kuisNoticeAPIClient; | ||
private final NoticeAPIClient libraryNoticeAPIClient; | ||
private final NoticeApiClient kuisNoticeApiClient; | ||
|
||
public MappedBeanConfig(NoticeAPIClient kuisNoticeAPIClient, NoticeAPIClient libraryNoticeAPIClient) { | ||
this.kuisNoticeAPIClient = kuisNoticeAPIClient; | ||
this.libraryNoticeAPIClient = libraryNoticeAPIClient; | ||
} | ||
private final NoticeApiClient libraryNoticeApiClient; | ||
|
||
@Bean | ||
public Map<CategoryName, NoticeAPIClient> noticeAPIClientMap() { | ||
HashMap<CategoryName, NoticeAPIClient> map = new HashMap<>(); | ||
public Map<CategoryName, NoticeApiClient> noticeApiClientMap() { | ||
HashMap<CategoryName, NoticeApiClient> map = new HashMap<>(); | ||
|
||
for (CategoryName categoryName : CategoryName.values()) { | ||
if(categoryName.equals(CategoryName.LIBRARY)) { | ||
map.put(categoryName, libraryNoticeAPIClient); | ||
map.put(categoryName, libraryNoticeApiClient); | ||
} else { | ||
map.put(categoryName, kuisNoticeAPIClient); | ||
map.put(categoryName, kuisNoticeApiClient); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../kustacks/kuring/kuapi/api/APIClient.java → .../kustacks/kuring/kuapi/api/ApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.kustacks.kuring.kuapi.api; | ||
|
||
public interface APIClient { | ||
public interface ApiClient { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 0 additions & 79 deletions
79
src/main/java/com/kustacks/kuring/kuapi/api/notice/LibraryNoticeAPIClient.java
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
src/main/java/com/kustacks/kuring/kuapi/api/notice/LibraryNoticeApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.kustacks.kuring.kuapi.api.notice; | ||
|
||
import com.kustacks.kuring.common.error.ErrorCode; | ||
import com.kustacks.kuring.common.error.InternalLogicException; | ||
import com.kustacks.kuring.common.utils.converter.DTOConverter; | ||
import com.kustacks.kuring.common.utils.converter.LibraryNoticeDTOToCommonFormatDTOConverter; | ||
import com.kustacks.kuring.kuapi.CategoryName; | ||
import com.kustacks.kuring.kuapi.notice.dto.response.CommonNoticeFormatDTO; | ||
import com.kustacks.kuring.kuapi.notice.dto.response.LibraryNoticeDTO; | ||
import com.kustacks.kuring.kuapi.notice.dto.response.LibraryNoticeResponseDTO; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Component | ||
public class LibraryNoticeApiClient implements NoticeApiClient { | ||
|
||
private static final int MAX_REQUEST_COUNT = 2; | ||
|
||
private final DTOConverter dtoConverter; | ||
private final RestTemplate restTemplate; | ||
|
||
@Value("${library.request-url}") | ||
private String libraryUrl; | ||
|
||
public LibraryNoticeApiClient(LibraryNoticeDTOToCommonFormatDTOConverter dtoConverter, RestTemplate restTemplate) { | ||
this.dtoConverter = dtoConverter; | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
@Override | ||
public List<CommonNoticeFormatDTO> getNotices(CategoryName categoryName) throws InternalLogicException { | ||
List<LibraryNoticeDTO> libraryNoticeDtoList = scrapLibraryNoticeDtos(); | ||
return convertToCommonFormatDto(libraryNoticeDtoList); | ||
} | ||
|
||
private List<LibraryNoticeDTO> scrapLibraryNoticeDtos() { | ||
int offset = 0; | ||
int max = 20; | ||
|
||
List<LibraryNoticeDTO> libraryNoticeDTOList = new LinkedList<>(); | ||
for (int requestIndex = 0; requestIndex < MAX_REQUEST_COUNT; requestIndex++) { | ||
String completeLibraryUrl = buildUrl(libraryUrl, offset, max); | ||
LibraryNoticeResponseDTO libraryNoticeResponseDTO = restTemplate | ||
.getForEntity(completeLibraryUrl, LibraryNoticeResponseDTO.class) | ||
.getBody(); | ||
|
||
validateResponse(requestIndex, libraryNoticeResponseDTO); | ||
|
||
offset = max; | ||
max = libraryNoticeResponseDTO.getTotalCount() - max; | ||
|
||
libraryNoticeDTOList.addAll(libraryNoticeResponseDTO.getData().getList()); | ||
} | ||
|
||
return libraryNoticeDTOList; | ||
} | ||
|
||
private String buildUrl(String url, int offset, int max) { | ||
return UriComponentsBuilder.fromUriString(url) | ||
.queryParam("offset", offset) | ||
.queryParam("max", max) | ||
.build().toString(); | ||
} | ||
|
||
private List<CommonNoticeFormatDTO> convertToCommonFormatDto(List<LibraryNoticeDTO> libraryNoticeDtoList) { | ||
return libraryNoticeDtoList.stream() | ||
.map(dto -> (CommonNoticeFormatDTO) dtoConverter.convert(dto)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private void validateResponse(int requestIndex, LibraryNoticeResponseDTO libraryNoticeResponseDTO) { | ||
if (libraryNoticeResponseDTO == null) { | ||
log.error("도서관 공지 {}번째 요청에 대한 응답의 body가 없습니다.", requestIndex + 1); | ||
throw new InternalLogicException(ErrorCode.LIB_CANNOT_PARSE_JSON); | ||
} | ||
|
||
if (!libraryNoticeResponseDTO.isSuccess()) { | ||
log.error("도서관 공지 {}번째 요청에 대한 응답이 fail입니다.", requestIndex + 1); | ||
throw new InternalLogicException(ErrorCode.LIB_BAD_RESPONSE); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...uring/kuapi/api/staff/StaffAPIClient.java → ...uring/kuapi/api/staff/StaffApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.