Skip to content

Commit fd37ffb

Browse files
Namozagpedja4
authored andcommitted
Retrofit rx (eugenp#2519)
* Retrofit with RxJava * Correct spelling mistake * Use spaces for indentation instead of tabs in pom.xml * Use spaces for indentation instead of tabs in pom.xml * Add Retrofit integration with RxJava to libraries module * Remove standalone project for Retrofit integration with RxJava * remove retrofit-rxjava module * Fixed error in pom.xml caused by an issue while merging * Retrofit integration with RxJava * Fix test cases * Fix merge issues * BAEL-1016 Merging master
1 parent b49ad4e commit fd37ffb

11 files changed

Lines changed: 886 additions & 504 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 521 additions & 504 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.retrofit.basic;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.retrofit.models.Contributor;
6+
import com.baeldung.retrofit.models.Repository;
7+
8+
import retrofit2.Call;
9+
import retrofit2.http.GET;
10+
import retrofit2.http.Path;
11+
12+
public interface GitHubBasicApi {
13+
14+
/**
15+
* List GitHub repositories of user
16+
* @param user GitHub Account
17+
* @return GitHub repositories
18+
*/
19+
@GET("users/{user}/repos")
20+
Call<List<Repository>> listRepos(@Path("user") String user);
21+
22+
/**
23+
* List Contributors of a GitHub Repository
24+
* @param user GitHub Account
25+
* @param repo GitHub Repository
26+
* @return GitHub Repository Contributors
27+
*/
28+
@GET("repos/{user}/{repo}/contributors")
29+
Call<List<Contributor>> listRepoContributors(
30+
@Path("user") String user,
31+
@Path("repo") String repo);
32+
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.retrofit.basic;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
public class GitHubBasicApp {
7+
8+
public static void main(String[] args) throws IOException {
9+
String userName = "eugenp";
10+
List<String> topContributors = new GitHubBasicService().getTopContributors(userName);
11+
topContributors.stream().forEach(System.out::println);
12+
}
13+
14+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.retrofit.models;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
/**
6+
* GitHub Contributer
7+
* @author hany
8+
*
9+
*/
10+
public class Contributor {
11+
12+
@SerializedName("login")
13+
private String name;
14+
15+
private Integer contributions;
16+
17+
public String getName() {
18+
return name;
19+
}
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
public Integer getContributions() {
24+
return contributions;
25+
}
26+
public void setContributions(Integer contributions) {
27+
this.contributions = contributions;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "Contributer [name=" + name + ", contributions=" + contributions + "]";
33+
}
34+
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.retrofit.models;
2+
3+
/**
4+
* GitHub Repository
5+
* @author hany
6+
*
7+
*/
8+
public class Repository {
9+
10+
private String name;
11+
12+
private String description;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
public String getDescription() {
21+
return description;
22+
}
23+
public void setDescription(String description) {
24+
this.description = description;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Repository [name=" + name + ", description=" + description + "]";
30+
}
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.retrofit.rx;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.retrofit.models.Contributor;
6+
import com.baeldung.retrofit.models.Repository;
7+
8+
import retrofit2.http.GET;
9+
import retrofit2.http.Path;
10+
import rx.Observable;
11+
12+
public interface GitHubRxApi {
13+
14+
/**
15+
* List GitHub repositories of user
16+
* @param user GitHub Account
17+
* @return GitHub repositories
18+
*/
19+
@GET("users/{user}/repos")
20+
Observable<List<Repository>> listRepos(@Path("user") String user);
21+
22+
/**
23+
* List Contributors of a GitHub Repository
24+
* @param user GitHub Account
25+
* @param repo GitHub Repository
26+
* @return GitHub Repository Contributors
27+
*/
28+
@GET("repos/{user}/{repo}/contributors")
29+
Observable<List<Contributor>> listRepoContributors(
30+
@Path("user") String user,
31+
@Path("repo") String repo);
32+
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.retrofit.rx;
2+
3+
import java.io.IOException;
4+
5+
public class GitHubRxApp {
6+
7+
public static void main(String[] args) throws IOException {
8+
String userName = "eugenp";
9+
new GitHubRxService().getTopContributors(userName)
10+
.subscribe(System.out::println);
11+
}
12+
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.retrofit.rx;
2+
3+
import retrofit2.Retrofit;
4+
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
5+
import retrofit2.converter.gson.GsonConverterFactory;
6+
import rx.Observable;
7+
8+
public class GitHubRxService {
9+
10+
private GitHubRxApi gitHubApi;
11+
12+
public GitHubRxService() {
13+
Retrofit retrofit = new Retrofit.Builder()
14+
.baseUrl("https://api.github.com/")
15+
.addConverterFactory(GsonConverterFactory.create())
16+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
17+
.build();
18+
19+
gitHubApi = retrofit.create(GitHubRxApi.class);
20+
}
21+
22+
public Observable<String> getTopContributors(String userName) {
23+
return gitHubApi.listRepos(userName)
24+
.flatMap( repos -> Observable.from(repos))
25+
.flatMap( repo -> gitHubApi.listRepoContributors(userName, repo.getName()) )
26+
.flatMap( contributers -> Observable.from(contributers))
27+
.filter( c -> c.getContributions() > 100)
28+
.sorted( (a, b) -> b.getContributions() - a.getContributions() )
29+
.map( c -> c.getName())
30+
.distinct();
31+
}
32+
33+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.retrofit.basic;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.fail;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import com.baeldung.retrofit.basic.GitHubBasicApi;
13+
import com.baeldung.retrofit.models.Contributor;
14+
import com.baeldung.retrofit.models.Repository;
15+
16+
import retrofit2.Retrofit;
17+
import retrofit2.converter.gson.GsonConverterFactory;
18+
19+
public class GitHubBasicApiTest {
20+
21+
GitHubBasicApi gitHub;
22+
23+
@Before
24+
public void init() {
25+
Retrofit retrofit = new Retrofit.Builder()
26+
.baseUrl("https://api.github.com/")
27+
.addConverterFactory(GsonConverterFactory.create())
28+
.build();
29+
30+
gitHub = retrofit.create(GitHubBasicApi.class);
31+
}
32+
33+
@Test
34+
public void whenListRepos_thenExpectReposThatContainTutorials() {
35+
try {
36+
List<Repository> repos = gitHub
37+
.listRepos("eugenp")
38+
.execute()
39+
.body();
40+
41+
assertThat(repos)
42+
.isNotEmpty()
43+
.extracting(Repository::getName).contains("tutorials");
44+
} catch (IOException e) {
45+
fail("Can not communicate with GitHub API");
46+
}
47+
}
48+
49+
@Test
50+
public void whenListRepoContributers_thenExpectContributorsThatContainEugenp() {
51+
try {
52+
List<Contributor> contributors = gitHub
53+
.listRepoContributors("eugenp", "tutorials")
54+
.execute()
55+
.body();
56+
57+
assertThat(contributors)
58+
.isNotEmpty()
59+
.extracting(Contributor::getName).contains("eugenp");
60+
} catch (IOException e) {
61+
fail("Can not communicate with GitHub API");
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)