|
| 1 | +package com.baeldung.retrofit.basic; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 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 | + |
| 15 | +public class GitHubBasicService { |
| 16 | + |
| 17 | + private GitHubBasicApi gitHubApi; |
| 18 | + |
| 19 | + public GitHubBasicService() { |
| 20 | + Retrofit retrofit = new Retrofit.Builder() |
| 21 | + .baseUrl("https://api.github.com/") |
| 22 | + .addConverterFactory(GsonConverterFactory.create()) |
| 23 | + .build(); |
| 24 | + |
| 25 | + gitHubApi = retrofit.create(GitHubBasicApi.class); |
| 26 | + } |
| 27 | + |
| 28 | + public List<String> getTopContributors(String userName) throws IOException { |
| 29 | + List<Repository> repos = gitHubApi |
| 30 | + .listRepos(userName) |
| 31 | + .execute() |
| 32 | + .body(); |
| 33 | + |
| 34 | + List<Contributor> topContributors = new ArrayList<>(); |
| 35 | + for(Repository repo : repos) { |
| 36 | + List<Contributor> contributers = gitHubApi |
| 37 | + .listRepoContributors(userName, repo.getName()) |
| 38 | + .execute() |
| 39 | + .body(); |
| 40 | + |
| 41 | + List<Contributor> repoTopContributors = contributers.stream() |
| 42 | + .filter(c -> c.getContributions() > 100) |
| 43 | + .collect(Collectors.toList()); |
| 44 | + topContributors.addAll(repoTopContributors); |
| 45 | + } |
| 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 | +} |
0 commit comments