Browse Source

Updates the GitHub example to use Java 8 default methods

Shows scenario described in #393
pull/403/head
Adrian Cole 8 years ago
parent
commit
aa66358584
  1. 4
      example-github/build.gradle
  2. 14
      example-github/pom.xml
  3. 59
      example-github/src/main/java/feign/example/github/GitHubExample.java

4
example-github/build.gradle

@ -12,8 +12,8 @@ configurations { @@ -12,8 +12,8 @@ configurations {
}
dependencies {
compile 'com.netflix.feign:feign-core:8.7.0'
compile 'com.netflix.feign:feign-gson:8.7.0'
compile 'com.netflix.feign:feign-core:8.16.2'
compile 'com.netflix.feign:feign-gson:8.16.2'
}
// create a self-contained jar that is executable

14
example-github/pom.xml

@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
<groupId>com.netflix.feign</groupId>
<artifactId>feign-example-github</artifactId>
<packaging>jar</packaging>
<version>8.7.0</version>
<version>8.16.2</version>
<name>GitHub Example</name>
<dependencies>
@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
@ -55,7 +55,7 @@ @@ -55,7 +55,7 @@
<plugin>
<groupId>org.skife.maven</groupId>
<artifactId>really-executable-jar-maven-plugin</artifactId>
<version>1.4.1</version>
<version>1.5.0</version>
<configuration>
<programFile>github</programFile>
</configuration>
@ -68,6 +68,14 @@ @@ -68,6 +68,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

59
example-github/src/main/java/feign/example/github/GitHubExample.java

@ -15,9 +15,6 @@ @@ -15,9 +15,6 @@
*/
package feign.example.github;
import java.io.IOException;
import java.util.List;
import feign.Feign;
import feign.Logger;
import feign.Param;
@ -26,6 +23,9 @@ import feign.Response; @@ -26,6 +23,9 @@ import feign.Response;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.gson.GsonDecoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
/**
* Inspired by {@code com.example.retrofit.GitHubClient}
@ -33,15 +33,42 @@ import feign.gson.GsonDecoder; @@ -33,15 +33,42 @@ import feign.gson.GsonDecoder;
public class GitHubExample {
interface GitHub {
class Repository {
String name;
}
class Contributor {
String login;
}
@RequestLine("GET /users/{username}/repos?sort=full_name")
List<Repository> repos(@Param("username") String owner);
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
static class Contributor {
String login;
int contributions;
/** Lists all contributors for all repos owned by a user. */
default List<String> contributors(String owner) {
return repos(owner).stream()
.flatMap(repo -> contributors(owner, repo.name).stream())
.map(c -> c.login)
.distinct()
.collect(Collectors.toList());
}
static GitHub connect() {
Decoder decoder = new GsonDecoder();
return Feign.builder()
.decoder(decoder)
.errorDecoder(new GitHubErrorDecoder(decoder))
.logger(new Logger.ErrorLogger())
.logLevel(Logger.Level.BASIC)
.target(GitHub.class, "https://api.github.com");
}
}
static class GitHubClientError extends RuntimeException {
private String message; // parsed from json
@ -52,18 +79,12 @@ public class GitHubExample { @@ -52,18 +79,12 @@ public class GitHubExample {
}
public static void main(String... args) {
Decoder decoder = new GsonDecoder();
GitHub github = Feign.builder()
.decoder(decoder)
.errorDecoder(new GitHubErrorDecoder(decoder))
.logger(new Logger.ErrorLogger())
.logLevel(Logger.Level.BASIC)
.target(GitHub.class, "https://api.github.com");
System.out.println("Let's fetch and print a list of the contributors to this library.");
List<Contributor> contributors = github.contributors("netflix", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
GitHub github = GitHub.connect();
System.out.println("Let's fetch and print a list of the contributors to this org.");
List<String> contributors = github.contributors("netflix");
for (String contributor : contributors) {
System.out.println(contributor);
}
System.out.println("Now, let's cause an error.");

Loading…
Cancel
Save