|
1 | 1 | package com.baeldung.retrofit.basic; |
2 | 2 |
|
| 3 | +import com.baeldung.retrofit.models.Contributor; |
| 4 | +import com.baeldung.retrofit.models.Repository; |
| 5 | +import retrofit2.Retrofit; |
| 6 | +import retrofit2.converter.gson.GsonConverterFactory; |
| 7 | + |
3 | 8 | import java.io.IOException; |
4 | | -import java.util.ArrayList; |
5 | 9 | import java.util.Collections; |
6 | 10 | import java.util.List; |
7 | 11 | import java.util.stream.Collectors; |
| 12 | +import java.util.stream.Stream; |
8 | 13 |
|
9 | | -import com.baeldung.retrofit.models.Contributor; |
10 | | -import com.baeldung.retrofit.models.Repository; |
11 | | - |
12 | | -import retrofit2.Retrofit; |
13 | | -import retrofit2.converter.gson.GsonConverterFactory; |
| 14 | +class GitHubBasicService { |
14 | 15 |
|
15 | | -public class GitHubBasicService { |
16 | | - |
17 | 16 | private GitHubBasicApi gitHubApi; |
18 | | - |
19 | | - public GitHubBasicService() { |
| 17 | + |
| 18 | + GitHubBasicService() { |
20 | 19 | Retrofit retrofit = new Retrofit.Builder() |
21 | | - .baseUrl("https://api.github.com/") |
22 | | - .addConverterFactory(GsonConverterFactory.create()) |
23 | | - .build(); |
24 | | - |
25 | | - gitHubApi = retrofit.create(GitHubBasicApi.class); |
| 20 | + .baseUrl("https://api.github.com/") |
| 21 | + .addConverterFactory(GsonConverterFactory.create()) |
| 22 | + .build(); |
| 23 | + |
| 24 | + gitHubApi = retrofit.create(GitHubBasicApi.class); |
26 | 25 | } |
27 | | - |
28 | | - public List<String> getTopContributors(String userName) throws IOException { |
| 26 | + |
| 27 | + List<String> getTopContributors(String userName) throws IOException { |
29 | 28 | List<Repository> repos = gitHubApi |
30 | 29 | .listRepos(userName) |
31 | 30 | .execute() |
32 | 31 | .body(); |
33 | | - |
34 | | - List<Contributor> topContributors = new ArrayList<>(); |
35 | | - for(Repository repo : repos) { |
36 | | - List<Contributor> contributers = gitHubApi |
| 32 | + |
| 33 | + repos = repos != null ? repos : Collections.emptyList(); |
| 34 | + |
| 35 | + return repos.stream() |
| 36 | + .flatMap(repo -> getContributors(userName, repo)) |
| 37 | + .sorted((a, b) -> b.getContributions() - a.getContributions()) |
| 38 | + .map(Contributor::getName) |
| 39 | + .distinct() |
| 40 | + .sorted() |
| 41 | + .collect(Collectors.toList()); |
| 42 | + } |
| 43 | + |
| 44 | + private Stream<Contributor> getContributors(String userName, Repository repo) { |
| 45 | + List<Contributor> contributors = null; |
| 46 | + try { |
| 47 | + contributors = gitHubApi |
37 | 48 | .listRepoContributors(userName, repo.getName()) |
38 | 49 | .execute() |
39 | 50 | .body(); |
40 | | - |
41 | | - List<Contributor> repoTopContributors = contributers.stream() |
42 | | - .filter(c -> c.getContributions() > 100) |
43 | | - .collect(Collectors.toList()); |
44 | | - topContributors.addAll(repoTopContributors); |
| 51 | + } catch (IOException e) { |
| 52 | + e.printStackTrace(); |
45 | 53 | } |
46 | | - |
47 | | - Collections.sort(topContributors, (a, b) -> b.getContributions() - a.getContributions()); |
48 | | - return topContributors.stream() |
49 | | - .map(c -> c.getName()) |
50 | | - .distinct() |
51 | | - .collect(Collectors.toList()); |
52 | | - } |
53 | 54 |
|
| 55 | + contributors = contributors != null ? contributors : Collections.emptyList(); |
| 56 | + |
| 57 | + return contributors.stream() |
| 58 | + .filter(c -> c.getContributions() > 100); |
| 59 | + } |
54 | 60 | } |
0 commit comments