Refactor Retrofit samples (#2568)
This commit is contained in:
committed by
GitHub
parent
216f7d8976
commit
e0c1678dfb
@@ -1,54 +1,60 @@
|
||||
package com.baeldung.retrofit.basic;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.baeldung.retrofit.models.Contributor;
|
||||
import com.baeldung.retrofit.models.Repository;
|
||||
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class GitHubBasicService {
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class GitHubBasicService {
|
||||
|
||||
private GitHubBasicApi gitHubApi;
|
||||
|
||||
public GitHubBasicService() {
|
||||
|
||||
GitHubBasicService() {
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
gitHubApi = retrofit.create(GitHubBasicApi.class);
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
gitHubApi = retrofit.create(GitHubBasicApi.class);
|
||||
}
|
||||
|
||||
public List<String> getTopContributors(String userName) throws IOException {
|
||||
|
||||
List<String> getTopContributors(String userName) throws IOException {
|
||||
List<Repository> repos = gitHubApi
|
||||
.listRepos(userName)
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
List<Contributor> topContributors = new ArrayList<>();
|
||||
for(Repository repo : repos) {
|
||||
List<Contributor> contributers = gitHubApi
|
||||
.listRepoContributors(userName, repo.getName())
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
List<Contributor> repoTopContributors = contributers.stream()
|
||||
.filter(c -> c.getContributions() > 100)
|
||||
.collect(Collectors.toList());
|
||||
topContributors.addAll(repoTopContributors);
|
||||
}
|
||||
|
||||
Collections.sort(topContributors, (a, b) -> b.getContributions() - a.getContributions());
|
||||
return topContributors.stream()
|
||||
.map(c -> c.getName())
|
||||
|
||||
repos = repos != null ? repos : Collections.emptyList();
|
||||
|
||||
return repos.stream()
|
||||
.flatMap(repo -> getContributors(userName, repo))
|
||||
.sorted((a, b) -> b.getContributions() - a.getContributions())
|
||||
.map(Contributor::getName)
|
||||
.distinct()
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Stream<Contributor> getContributors(String userName, Repository repo) {
|
||||
List<Contributor> contributors = null;
|
||||
try {
|
||||
contributors = gitHubApi
|
||||
.listRepoContributors(userName, repo.getName())
|
||||
.execute()
|
||||
.body();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
contributors = contributors != null ? contributors : Collections.emptyList();
|
||||
|
||||
return contributors.stream()
|
||||
.filter(c -> c.getContributions() > 100);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user