Retrofit rx (#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
This commit is contained in:
parent
b49ad4ea59
commit
fd37ffb7c2
|
@ -502,6 +502,22 @@
|
|||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<!-- Retrofit -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>retrofit</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-gson</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>adapter-rxjava</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.darwinsys</groupId>
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
|
@ -612,6 +628,7 @@
|
|||
<streamex.version>0.6.5</streamex.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
<geotools.version>15.2</geotools.version>
|
||||
<retrofit.version>2.3.0</retrofit.version>
|
||||
<joda-time.version>2.9.9</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
<protonpack.version>1.14</protonpack.version>
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.retrofit.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.retrofit.models.Contributor;
|
||||
import com.baeldung.retrofit.models.Repository;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Path;
|
||||
|
||||
public interface GitHubBasicApi {
|
||||
|
||||
/**
|
||||
* List GitHub repositories of user
|
||||
* @param user GitHub Account
|
||||
* @return GitHub repositories
|
||||
*/
|
||||
@GET("users/{user}/repos")
|
||||
Call<List<Repository>> listRepos(@Path("user") String user);
|
||||
|
||||
/**
|
||||
* List Contributors of a GitHub Repository
|
||||
* @param user GitHub Account
|
||||
* @param repo GitHub Repository
|
||||
* @return GitHub Repository Contributors
|
||||
*/
|
||||
@GET("repos/{user}/{repo}/contributors")
|
||||
Call<List<Contributor>> listRepoContributors(
|
||||
@Path("user") String user,
|
||||
@Path("repo") String repo);
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.retrofit.basic;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class GitHubBasicApp {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String userName = "eugenp";
|
||||
List<String> topContributors = new GitHubBasicService().getTopContributors(userName);
|
||||
topContributors.stream().forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
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 {
|
||||
|
||||
private GitHubBasicApi gitHubApi;
|
||||
|
||||
public GitHubBasicService() {
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
gitHubApi = retrofit.create(GitHubBasicApi.class);
|
||||
}
|
||||
|
||||
public 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())
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.retrofit.models;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* GitHub Contributer
|
||||
* @author hany
|
||||
*
|
||||
*/
|
||||
public class Contributor {
|
||||
|
||||
@SerializedName("login")
|
||||
private String name;
|
||||
|
||||
private Integer contributions;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Integer getContributions() {
|
||||
return contributions;
|
||||
}
|
||||
public void setContributions(Integer contributions) {
|
||||
this.contributions = contributions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Contributer [name=" + name + ", contributions=" + contributions + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.retrofit.models;
|
||||
|
||||
/**
|
||||
* GitHub Repository
|
||||
* @author hany
|
||||
*
|
||||
*/
|
||||
public class Repository {
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository [name=" + name + ", description=" + description + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.retrofit.rx;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.retrofit.models.Contributor;
|
||||
import com.baeldung.retrofit.models.Repository;
|
||||
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Path;
|
||||
import rx.Observable;
|
||||
|
||||
public interface GitHubRxApi {
|
||||
|
||||
/**
|
||||
* List GitHub repositories of user
|
||||
* @param user GitHub Account
|
||||
* @return GitHub repositories
|
||||
*/
|
||||
@GET("users/{user}/repos")
|
||||
Observable<List<Repository>> listRepos(@Path("user") String user);
|
||||
|
||||
/**
|
||||
* List Contributors of a GitHub Repository
|
||||
* @param user GitHub Account
|
||||
* @param repo GitHub Repository
|
||||
* @return GitHub Repository Contributors
|
||||
*/
|
||||
@GET("repos/{user}/{repo}/contributors")
|
||||
Observable<List<Contributor>> listRepoContributors(
|
||||
@Path("user") String user,
|
||||
@Path("repo") String repo);
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.retrofit.rx;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class GitHubRxApp {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String userName = "eugenp";
|
||||
new GitHubRxService().getTopContributors(userName)
|
||||
.subscribe(System.out::println);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.retrofit.rx;
|
||||
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import rx.Observable;
|
||||
|
||||
public class GitHubRxService {
|
||||
|
||||
private GitHubRxApi gitHubApi;
|
||||
|
||||
public GitHubRxService() {
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
gitHubApi = retrofit.create(GitHubRxApi.class);
|
||||
}
|
||||
|
||||
public 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())
|
||||
.distinct();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.retrofit.basic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.retrofit.basic.GitHubBasicApi;
|
||||
import com.baeldung.retrofit.models.Contributor;
|
||||
import com.baeldung.retrofit.models.Repository;
|
||||
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class GitHubBasicApiTest {
|
||||
|
||||
GitHubBasicApi gitHub;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
gitHub = retrofit.create(GitHubBasicApi.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListRepos_thenExpectReposThatContainTutorials() {
|
||||
try {
|
||||
List<Repository> repos = gitHub
|
||||
.listRepos("eugenp")
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
assertThat(repos)
|
||||
.isNotEmpty()
|
||||
.extracting(Repository::getName).contains("tutorials");
|
||||
} catch (IOException e) {
|
||||
fail("Can not communicate with GitHub API");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListRepoContributers_thenExpectContributorsThatContainEugenp() {
|
||||
try {
|
||||
List<Contributor> contributors = gitHub
|
||||
.listRepoContributors("eugenp", "tutorials")
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
assertThat(contributors)
|
||||
.isNotEmpty()
|
||||
.extracting(Contributor::getName).contains("eugenp");
|
||||
} catch (IOException e) {
|
||||
fail("Can not communicate with GitHub API");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.retrofit.rx;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.retrofit.models.Contributor;
|
||||
import com.baeldung.retrofit.models.Repository;
|
||||
import com.baeldung.retrofit.rx.GitHubRxApi;
|
||||
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class GitHubRxApiTest {
|
||||
|
||||
GitHubRxApi gitHub;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
gitHub = retrofit.create(GitHubRxApi.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListRepos_thenExpectReposThatContainTutorials() {
|
||||
gitHub
|
||||
.listRepos("eugenp")
|
||||
.subscribe( repos -> {
|
||||
assertThat(repos)
|
||||
.isNotEmpty()
|
||||
.extracting(Repository::getName).contains("tutorials");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListRepoContributers_thenExpectContributorsThatContainEugenp() {
|
||||
gitHub
|
||||
.listRepoContributors("eugenp", "tutorials")
|
||||
.subscribe(contributors -> {
|
||||
assertThat(contributors)
|
||||
.isNotEmpty()
|
||||
.extracting(Contributor::getName).contains("eugenp");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue