@@ -35,7 +35,7 @@ public class GitHubService {
3535
3636 private static final int TIMEOUT = 10 ;
3737
38- public BasicStatistic fetchBasicStatistic (String usernameGitHub ) {
38+ public Mono < BasicStatistic > fetchBasicStatistic (String usernameGitHub ) {
3939 var query = graphQlReader .reading (BASIC_STATISTIC .getMessage ());
4040
4141 Map <String , Object > body = Map .of (
@@ -50,26 +50,28 @@ public BasicStatistic fetchBasicStatistic(String usernameGitHub) {
5050 .timeout (Duration .ofSeconds (TIMEOUT ))
5151 .map (basicStatisticService ::create )
5252 .doOnError (error -> log .error ("GitHub API Exception: {}" , error .getMessage ()))
53- .onErrorMap (error -> new GitHubAPIException ("" ))
54- .block ();
53+ .onErrorMap (error -> new GitHubAPIException ("GitHub API Exception" ));
5554 }
5655
57- public ContributeStatistic fetchContributeStatistic (String usernameGitHub ) {
58- return fetchCreatedAt (usernameGitHub )
59- .flatMapMany (createdAtStr -> {
60- Instant createdAt = Instant .parse (createdAtStr );
61- var startYear = createdAt .atZone (ZoneOffset .UTC ).getYear ();
62- var endYear = ZonedDateTime .now (ZoneOffset .UTC ).getYear ();
63-
64- return Flux
65- .range (startYear , endYear - startYear + 1 )
66- .flatMap (year -> fetchContributionsForYear (usernameGitHub , year ));
67- })
68- .reduce (new ContributeStatistic (), (acc , current ) -> {
69- acc .add (current );
70- return acc ;
71- })
72- .block ();
56+ public Mono <ContributeStatistic > fetchContributeStatistic (String usernameGitHub ) {
57+ return fetchCreatedAt (usernameGitHub )
58+ .flatMapMany (createdAtStr -> {
59+ if (createdAtStr == null || createdAtStr .isBlank ()) {
60+ log .error ("CreatedAt is empty for user: " + usernameGitHub );
61+ throw new GitHubAPIException ("CreatedAt is empty for user: " + usernameGitHub );
62+ }
63+ Instant createdAt = Instant .parse (createdAtStr );
64+ var startYear = createdAt .atZone (ZoneOffset .UTC ).getYear ();
65+ var endYear = ZonedDateTime .now (ZoneOffset .UTC ).getYear ();
66+
67+ return Flux
68+ .range (startYear , endYear - startYear + 1 )
69+ .flatMap (year -> fetchContributionsForYear (usernameGitHub , year ));
70+ })
71+ .reduce (new ContributeStatistic (), (acc , current ) -> {
72+ acc .add (current );
73+ return acc ;
74+ });
7375 }
7476
7577 private Mono <String > fetchCreatedAt (String usernameGitHub ) {
@@ -87,7 +89,7 @@ private Mono<String> fetchCreatedAt(String usernameGitHub) {
8789 .timeout (Duration .ofSeconds (TIMEOUT ))
8890 .map (json -> json .path ("data" ).path ("user" ).path ("createdAt" ).asText ())
8991 .doOnError (error -> log .error ("GitHub API Exception: {}" , error .getMessage ()))
90- .onErrorMap (error -> new GitHubAPIException ("" ));
92+ .onErrorMap (error -> new GitHubAPIException ("GitHub API Exception " ));
9193 }
9294
9395 private Mono <ContributeStatistic > fetchContributionsForYear (String usernameGitHub , int year ) {
@@ -119,6 +121,6 @@ private Mono<ContributeStatistic> fetchContributionsForYear(String usernameGitHu
119121 .timeout (Duration .ofSeconds (TIMEOUT ))
120122 .map (contributeService ::create )
121123 .doOnError (error -> log .error ("GitHub API Exception: {}" , error .getMessage ()))
122- .onErrorMap (error -> new GitHubAPIException ("" ));
124+ .onErrorMap (error -> new GitHubAPIException ("GitHub API Exception " ));
123125 }
124126}
0 commit comments