forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Retrofit samples (eugenp#2568)
- Loading branch information
Showing
6 changed files
with
62 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 39 additions & 33 deletions
72
libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,60 @@ | ||
package com.baeldung.retrofit.basic; | ||
|
||
import com.baeldung.retrofit.models.Contributor; | ||
import com.baeldung.retrofit.models.Repository; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.baeldung.retrofit.models.Contributor; | ||
import com.baeldung.retrofit.models.Repository; | ||
|
||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
class GitHubBasicService { | ||
|
||
public 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 | ||
|
||
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(); | ||
|
||
List<Contributor> repoTopContributors = contributers.stream() | ||
.filter(c -> c.getContributions() > 100) | ||
.collect(Collectors.toList()); | ||
topContributors.addAll(repoTopContributors); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
Collections.sort(topContributors, (a, b) -> b.getContributions() - a.getContributions()); | ||
return topContributors.stream() | ||
.map(c -> c.getName()) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
contributors = contributors != null ? contributors : Collections.emptyList(); | ||
|
||
return contributors.stream() | ||
.filter(c -> c.getContributions() > 100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
libraries/src/main/java/com/baeldung/retrofit/models/Repository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 18 additions & 18 deletions
36
libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
package com.baeldung.retrofit.rx; | ||
|
||
import com.baeldung.retrofit.models.Contributor; | ||
import retrofit2.Retrofit; | ||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
import rx.Observable; | ||
|
||
public class GitHubRxService { | ||
class GitHubRxService { | ||
|
||
private GitHubRxApi gitHubApi; | ||
public GitHubRxService() { | ||
|
||
GitHubRxService() { | ||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl("https://api.github.com/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | ||
.build(); | ||
.baseUrl("https://api.github.com/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | ||
.build(); | ||
|
||
gitHubApi = retrofit.create(GitHubRxApi.class); | ||
} | ||
public Observable<String> getTopContributors(String userName) { | ||
|
||
Observable<String> getTopContributors(String userName) { | ||
return gitHubApi.listRepos(userName) | ||
.flatMap( repos -> Observable.from(repos)) | ||
.flatMap( repo -> gitHubApi.listRepoContributors(userName, repo.getName()) ) | ||
.flatMap( contributers -> Observable.from(contributers)) | ||
.filter( c -> c.getContributions() > 100) | ||
.sorted( (a, b) -> b.getContributions() - a.getContributions() ) | ||
.map( c -> c.getName()) | ||
.flatMapIterable(x -> x) | ||
.flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName())) | ||
.flatMapIterable(x -> x) | ||
.filter(c -> c.getContributions() > 100) | ||
.sorted((a, b) -> b.getContributions() - a.getContributions()) | ||
.map(Contributor::getName) | ||
.distinct(); | ||
} | ||
|
||
} |