Refactor Retrofit samples (#2568)
This commit is contained in:
parent
216f7d8976
commit
e0c1678dfb
@ -7,8 +7,8 @@ public class GitHubBasicApp {
|
|||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
String userName = "eugenp";
|
String userName = "eugenp";
|
||||||
List<String> topContributors = new GitHubBasicService().getTopContributors(userName);
|
List<String> topContributors = new GitHubBasicService()
|
||||||
topContributors.stream().forEach(System.out::println);
|
.getTopContributors(userName);
|
||||||
|
topContributors.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,21 @@
|
|||||||
package com.baeldung.retrofit.basic;
|
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.Contributor;
|
||||||
import com.baeldung.retrofit.models.Repository;
|
import com.baeldung.retrofit.models.Repository;
|
||||||
|
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
import retrofit2.converter.gson.GsonConverterFactory;
|
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;
|
private GitHubBasicApi gitHubApi;
|
||||||
|
|
||||||
public GitHubBasicService() {
|
GitHubBasicService() {
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
.baseUrl("https://api.github.com/")
|
.baseUrl("https://api.github.com/")
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
@ -25,30 +24,37 @@ public class GitHubBasicService {
|
|||||||
gitHubApi = retrofit.create(GitHubBasicApi.class);
|
gitHubApi = retrofit.create(GitHubBasicApi.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getTopContributors(String userName) throws IOException {
|
List<String> getTopContributors(String userName) throws IOException {
|
||||||
List<Repository> repos = gitHubApi
|
List<Repository> repos = gitHubApi
|
||||||
.listRepos(userName)
|
.listRepos(userName)
|
||||||
.execute()
|
.execute()
|
||||||
.body();
|
.body();
|
||||||
|
|
||||||
List<Contributor> topContributors = new ArrayList<>();
|
repos = repos != null ? repos : Collections.emptyList();
|
||||||
for(Repository repo : repos) {
|
|
||||||
List<Contributor> contributers = gitHubApi
|
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())
|
.listRepoContributors(userName, repo.getName())
|
||||||
.execute()
|
.execute()
|
||||||
.body();
|
.body();
|
||||||
|
} catch (IOException e) {
|
||||||
List<Contributor> repoTopContributors = contributers.stream()
|
e.printStackTrace();
|
||||||
.filter(c -> c.getContributions() > 100)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
topContributors.addAll(repoTopContributors);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.sort(topContributors, (a, b) -> b.getContributions() - a.getContributions());
|
contributors = contributors != null ? contributors : Collections.emptyList();
|
||||||
return topContributors.stream()
|
|
||||||
.map(c -> c.getName())
|
|
||||||
.distinct()
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return contributors.stream()
|
||||||
|
.filter(c -> c.getContributions() > 100);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,6 @@ package com.baeldung.retrofit.models;
|
|||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
/**
|
|
||||||
* GitHub Contributer
|
|
||||||
* @author hany
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class Contributor {
|
public class Contributor {
|
||||||
|
|
||||||
@SerializedName("login")
|
@SerializedName("login")
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package com.baeldung.retrofit.models;
|
package com.baeldung.retrofit.models;
|
||||||
|
|
||||||
/**
|
|
||||||
* GitHub Repository
|
|
||||||
* @author hany
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class Repository {
|
public class Repository {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -9,5 +9,4 @@ public class GitHubRxApp {
|
|||||||
new GitHubRxService().getTopContributors(userName)
|
new GitHubRxService().getTopContributors(userName)
|
||||||
.subscribe(System.out::println);
|
.subscribe(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
package com.baeldung.retrofit.rx;
|
package com.baeldung.retrofit.rx;
|
||||||
|
|
||||||
|
import com.baeldung.retrofit.models.Contributor;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
||||||
import retrofit2.converter.gson.GsonConverterFactory;
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
import rx.Observable;
|
import rx.Observable;
|
||||||
|
|
||||||
public class GitHubRxService {
|
class GitHubRxService {
|
||||||
|
|
||||||
private GitHubRxApi gitHubApi;
|
private GitHubRxApi gitHubApi;
|
||||||
|
|
||||||
public GitHubRxService() {
|
GitHubRxService() {
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
.baseUrl("https://api.github.com/")
|
.baseUrl("https://api.github.com/")
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
@ -19,15 +20,14 @@ public class GitHubRxService {
|
|||||||
gitHubApi = retrofit.create(GitHubRxApi.class);
|
gitHubApi = retrofit.create(GitHubRxApi.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Observable<String> getTopContributors(String userName) {
|
Observable<String> getTopContributors(String userName) {
|
||||||
return gitHubApi.listRepos(userName)
|
return gitHubApi.listRepos(userName)
|
||||||
.flatMap( repos -> Observable.from(repos))
|
.flatMapIterable(x -> x)
|
||||||
.flatMap( repo -> gitHubApi.listRepoContributors(userName, repo.getName()) )
|
.flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName()))
|
||||||
.flatMap( contributers -> Observable.from(contributers))
|
.flatMapIterable(x -> x)
|
||||||
.filter( c -> c.getContributions() > 100)
|
.filter(c -> c.getContributions() > 100)
|
||||||
.sorted( (a, b) -> b.getContributions() - a.getContributions() )
|
.sorted((a, b) -> b.getContributions() - a.getContributions())
|
||||||
.map( c -> c.getName())
|
.map(Contributor::getName)
|
||||||
.distinct();
|
.distinct();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user