Skip to content

Commit 74ae7d1

Browse files
author
Adrian Cole
committed
Updates the GitHub example to use Java 8 default methods
Shows scenario described in OpenFeign#393
1 parent bc9948f commit 74ae7d1

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

example-github/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ configurations {
1212
}
1313

1414
dependencies {
15-
compile 'com.netflix.feign:feign-core:8.7.0'
16-
compile 'com.netflix.feign:feign-gson:8.7.0'
15+
compile 'com.netflix.feign:feign-core:8.16.2'
16+
compile 'com.netflix.feign:feign-gson:8.16.2'
1717
}
1818

1919
// create a self-contained jar that is executable

example-github/pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<groupId>com.netflix.feign</groupId>
1313
<artifactId>feign-example-github</artifactId>
1414
<packaging>jar</packaging>
15-
<version>8.7.0</version>
15+
<version>8.16.2</version>
1616
<name>GitHub Example</name>
1717

1818
<dependencies>
@@ -34,7 +34,7 @@
3434
<plugin>
3535
<groupId>org.apache.maven.plugins</groupId>
3636
<artifactId>maven-shade-plugin</artifactId>
37-
<version>2.4.1</version>
37+
<version>2.4.3</version>
3838
<executions>
3939
<execution>
4040
<phase>package</phase>
@@ -55,7 +55,7 @@
5555
<plugin>
5656
<groupId>org.skife.maven</groupId>
5757
<artifactId>really-executable-jar-maven-plugin</artifactId>
58-
<version>1.4.1</version>
58+
<version>1.5.0</version>
5959
<configuration>
6060
<programFile>github</programFile>
6161
</configuration>
@@ -68,6 +68,14 @@
6868
</execution>
6969
</executions>
7070
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-compiler-plugin</artifactId>
74+
<configuration>
75+
<source>1.8</source>
76+
<target>1.8</target>
77+
</configuration>
78+
</plugin>
7179
</plugins>
7280
</build>
7381
</project>

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

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package feign.example.github;
1717

18-
import java.io.IOException;
19-
import java.util.List;
20-
2118
import feign.Feign;
2219
import feign.Logger;
2320
import feign.Param;
@@ -26,22 +23,52 @@
2623
import feign.codec.Decoder;
2724
import feign.codec.ErrorDecoder;
2825
import feign.gson.GsonDecoder;
26+
import java.io.IOException;
27+
import java.util.List;
28+
import java.util.stream.Collectors;
2929

3030
/**
3131
* Inspired by {@code com.example.retrofit.GitHubClient}
3232
*/
3333
public class GitHubExample {
3434

3535
interface GitHub {
36+
37+
class Repository {
38+
String name;
39+
}
40+
41+
class Contributor {
42+
String login;
43+
}
44+
45+
@RequestLine("GET /users/{username}/repos?sort=full_name")
46+
List<Repository> repos(@Param("username") String owner);
47+
3648
@RequestLine("GET /repos/{owner}/{repo}/contributors")
3749
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
38-
}
3950

40-
static class Contributor {
41-
String login;
42-
int contributions;
51+
/** Lists all contributors for all repos owned by a user. */
52+
default List<String> contributors(String owner) {
53+
return repos(owner).stream()
54+
.flatMap(repo -> contributors(owner, repo.name).stream())
55+
.map(c -> c.login)
56+
.distinct()
57+
.collect(Collectors.toList());
58+
}
59+
60+
static GitHub connect() {
61+
Decoder decoder = new GsonDecoder();
62+
return Feign.builder()
63+
.decoder(decoder)
64+
.errorDecoder(new GitHubErrorDecoder(decoder))
65+
.logger(new Logger.ErrorLogger())
66+
.logLevel(Logger.Level.BASIC)
67+
.target(GitHub.class, "https://api.github.com");
68+
}
4369
}
4470

71+
4572
static class GitHubClientError extends RuntimeException {
4673
private String message; // parsed from json
4774

@@ -52,18 +79,12 @@ public String getMessage() {
5279
}
5380

5481
public static void main(String... args) {
55-
Decoder decoder = new GsonDecoder();
56-
GitHub github = Feign.builder()
57-
.decoder(decoder)
58-
.errorDecoder(new GitHubErrorDecoder(decoder))
59-
.logger(new Logger.ErrorLogger())
60-
.logLevel(Logger.Level.BASIC)
61-
.target(GitHub.class, "https://api.github.com");
62-
63-
System.out.println("Let's fetch and print a list of the contributors to this library.");
64-
List<Contributor> contributors = github.contributors("netflix", "feign");
65-
for (Contributor contributor : contributors) {
66-
System.out.println(contributor.login + " (" + contributor.contributions + ")");
82+
GitHub github = GitHub.connect();
83+
84+
System.out.println("Let's fetch and print a list of the contributors to this org.");
85+
List<String> contributors = github.contributors("netflix");
86+
for (String contributor : contributors) {
87+
System.out.println(contributor);
6788
}
6889

6990
System.out.println("Now, let's cause an error.");

0 commit comments

Comments
 (0)