Bael 4398 retrofit dynamic url (#10090)

* BAEL-2495 fixing InvalidDefinitionException

* BAEL-2495 minor changes

* Added solution for dynamic Urls using retrofit 2.

* Revert "BAEL-2495 minor changes"

This reverts commit 174d8664e2612d9d578c72b33571b61dc3a27dbf.

* Revert "BAEL-2495 fixing InvalidDefinitionException"

This reverts commit 20e1b768e6c50529cb0af250ca437b94a9f01cd9.

* BAEL-4398 moved repository to libraries-http-2
This commit is contained in:
patkorek 2020-10-26 20:54:34 +01:00 committed by GitHub
parent 0e92c3511b
commit 4a36ad4b79
6 changed files with 152 additions and 0 deletions

View File

@ -66,6 +66,18 @@
<artifactId>reactive-streams</artifactId>
<version>${reactive.stream.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>
</dependencies>
<properties>
@ -76,6 +88,7 @@
<jetty.httpclient.version>1.0.3</jetty.httpclient.version>
<jetty.server.version>9.4.19.v20190610</jetty.server.version>
<rxjava2.version>2.2.11</rxjava2.version>
<retrofit.version>2.3.0</retrofit.version>
<spring.webflux.version>5.1.9.RELEASE</spring.webflux.version>
<reactive.stream.version>1.0.3</reactive.stream.version>
<reactor.version>3.2.12.RELEASE</reactor.version>

View File

@ -0,0 +1,19 @@
package com.baeldung.retrofit.dynamic;
import com.baeldung.retrofit.models.Contributor;
import com.baeldung.retrofit.models.Repository;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Url;
import java.util.List;
public interface GitHubDynamicApi {
@GET
Call<List<Repository>> reposList(@Url String url);
@GET("{fullUrl}")
Call<List<Contributor>> contributorsList(@Path(value = "fullUrl", encoded = true) String fullUrl);
}

View File

@ -0,0 +1,13 @@
package com.baeldung.retrofit.dynamic;
import java.io.IOException;
import java.util.List;
public class GitHubDynamicApiApp {
public static void main(String[] args) throws IOException {
String url = "https://api.github.com/users/eugenp/repos";
List<String> topContributors = new GitHubDynamicApiService().getTopContributors(url);
topContributors.forEach(System.out::println);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.retrofit.dynamic;
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.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class GitHubDynamicApiService {
private GitHubDynamicApi gitHubDynamicApi;
GitHubDynamicApiService() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create()).build();
gitHubDynamicApi = retrofit.create(GitHubDynamicApi.class);
}
List<String> getTopContributors(String url) throws IOException {
List<Repository> repos = gitHubDynamicApi.reposList(url).execute().body();
repos = repos != null ? repos : Collections.emptyList();
return repos.stream().flatMap(repo -> getContributors("repos/eugenp/"+repo+"/contributors")).sorted((a, b) -> b.getContributions() - a.getContributions()).map(com.baeldung.retrofit.models.Contributor::getName).distinct().sorted().collect(Collectors.toList());
}
private Stream<Contributor> getContributors(String fullUrl) {
List<Contributor> contributors = null;
try {
contributors = gitHubDynamicApi.contributorsList(fullUrl).execute().body();
} catch (IOException e) {
e.printStackTrace();
}
contributors = contributors != null ? contributors : Collections.emptyList();
return contributors.stream().filter(c -> c.getContributions() > 100);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.retrofit.models;
import com.google.gson.annotations.SerializedName;
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 + "]";
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.retrofit.models;
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 + "]";
}
}