1515 */
1616package feign .example .github ;
1717
18- import java .io .IOException ;
19- import java .util .List ;
20-
2118import feign .Feign ;
2219import feign .Logger ;
2320import feign .Param ;
2623import feign .codec .Decoder ;
2724import feign .codec .ErrorDecoder ;
2825import 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 */
3333public 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