55import io .netty .handler .timeout .ReadTimeoutHandler ;
66import io .netty .handler .timeout .WriteTimeoutHandler ;
77import java .net .URI ;
8+ import java .time .Duration ;
89import java .util .ArrayList ;
910import java .util .LinkedHashMap ;
1011import java .util .List ;
1112import java .util .Map ;
13+ import java .util .Objects ;
1214import java .util .concurrent .TimeUnit ;
1315import java .util .stream .Collectors ;
1416import lombok .RequiredArgsConstructor ;
17+ import lombok .extern .slf4j .Slf4j ;
1518import org .apache .commons .collections4 .CollectionUtils ;
1619import org .apache .commons .lang3 .StringUtils ;
1720import org .occidere .githubnotifier .vo .GithubFollower ;
2326import org .springframework .http .client .reactive .ReactorClientHttpConnector ;
2427import org .springframework .stereotype .Service ;
2528import org .springframework .web .reactive .function .client .WebClient ;
26- import reactor .core .publisher .Flux ;
2729import reactor .core .publisher .Mono ;
2830import reactor .netty .http .client .HttpClient ;
2931import reactor .netty .tcp .TcpClient ;
32+ import reactor .util .retry .Retry ;
3033
3134/**
3235 * @author occidere
3336 * @Blog: https://blog.naver.com/occidere
3437 * @Github: https://github.com/occidere
3538 * @since 2019. 12. 02.
3639 */
40+ @ Slf4j
3741@ Service
3842@ RequiredArgsConstructor
3943public class GithubApiService implements GithubApiRepository {
@@ -104,15 +108,13 @@ public List<String> getForksLogins(String login, String repoName) {
104108 }
105109
106110 private LinkedHashMap <String , Object > getRawData (String url ) {
107- return Mono . from ( getFluxBody ( GITHUB_API_URL + url )) .block ();
111+ return Objects . requireNonNull ( getBody ( url ).block ()). get ( 0 );
108112 }
109113
110114 private List <LinkedHashMap <String , Object >> getAllRawData (String url ) {
111115 List <LinkedHashMap <String , Object >> data = new ArrayList <>();
112116 for (int page = 1 ; ; page ++) {
113- List <LinkedHashMap <String , Object >> body = getFluxBody (GITHUB_API_URL + url + "?page=" + page )
114- .collectList ()
115- .block ();
117+ List <LinkedHashMap <String , Object >> body = getBody (url + "?page=" + page ).block ();
116118 if (CollectionUtils .isEmpty (body )) {
117119 break ;
118120 } else {
@@ -122,15 +124,18 @@ private List<LinkedHashMap<String, Object>> getAllRawData(String url) {
122124 return data ;
123125 }
124126
125- private Flux < LinkedHashMap <String , Object >> getFluxBody (final String absoluteUrl ) {
127+ private Mono < List < LinkedHashMap <String , Object >>> getBody (final String uri ) {
126128 return webClient .get ()
127- .uri (URI .create (absoluteUrl ))
129+ .uri (URI .create (GITHUB_API_URL + uri ))
128130 .headers (httpHeaders -> {
129131 if (StringUtils .isNotBlank (githubApiToken )) {
130132 httpHeaders .setBearerAuth (githubApiToken );
131133 }
132134 })
133135 .retrieve ()
134- .bodyToFlux (new ParameterizedTypeReference <>() {});
136+ .bodyToFlux (new ParameterizedTypeReference <LinkedHashMap <String , Object >>() {})
137+ .collectList ()
138+ .retryWhen (Retry .backoff (3 , Duration .ofSeconds (5 ))) // will throw exception if failed
139+ .defaultIfEmpty (new ArrayList <>());
135140 }
136141}
0 commit comments