Retrofit and RxJava (Netflix Open Source Meetup S02E02)

Retrofit and RxJava (Netflix Open Source Meetup S02E02)

A 5-minute lightning introduction to Retrofit, its RxJava integration, and how to use it for quick-and-easy composition of API calls.

Video: https://www.youtube.com/watch?v=aEuNBk1b5OE#t=2480

http://www.meetup.com/Netflix-Open-Source-Platform/events/184153592/

E68309f117985270285ade8082f4877d?s=128

Jake Wharton

July 09, 2014
Tweet

Transcript

  1. Retrofit Jake Wharton

  2. Retrofit Example: GitHub API List Contributors

  3. Retrofit Example: GitHub API List Contributors GET /repos/:owner/:repo/contributors

  4. Retrofit GET /repos/:owner/:repo/contributors interface GitHub { List<Contributor> contributors( String owner,

    String repo); }
  5. Retrofit GET /repos/:owner/:repo/contributors interface GitHub { List<Contributor> contributors( String owner,

    String repo); } class Contributor { String login; long contributions; }
  6. Retrofit GET /repos/:owner/:repo/contributors interface GitHub { List<Contributor> contributors( String owner,

    String repo); }
  7. Retrofit GET /repos/:owner/:repo/contributors interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors( String

    owner, String repo); }
  8. Retrofit GET /repos/:owner/:repo/contributors interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors( @Path("owner")

    String owner, @Path("repo") String repo); }
  9. Retrofit RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com/") .build();

  10. Retrofit RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com/") .build(); GitHub gitHub

    = restAdapter.create(GitHub.class);
  11. Retrofit RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com/") .build(); GitHub gitHub

    = restAdapter.create(GitHub.class); List<Contributor> contributors = gitHub.contributors("netflix", "rxjava");
  12. Retrofit List<Contributor> contributors = gitHub.contributors("netflix", "rxjava"); for (Contributor c :

    contributors) { println(c.login + '\t' + c.contributions); }
  13. Retrofit List<Contributor> contributors = gitHub.contributors("netflix", "rxjava"); for (Contributor c :

    contributors) { println(c.login + '\t' + c.contributions); } 1483 benjchristensen 225 zsxwing 167 samuelgruetter 146 jmhofer 137 akarnokd 105 DavidMGross 102 AppliedDuality ...
  14. Retrofit interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors( @Path("owner") String owner,

    @Path("repo") String repo); }
  15. Retrofit interface GitHub { @GET("/repos/{owner}/{repo}/contributors?anon=true") List<Contributor> contributors( @Path("owner") String owner,

    @Path("repo") String repo); }
  16. Retrofit interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors( @Path("owner") String owner,

    @Path("repo") String repo, @Query("anon") boolean includeAnonymous); }
  17. Retrofit interface GitHub { @POST("/repos/{owner}/{repo}/hooks") Response createHook( @Path("owner") String owner,

    @Path("repo") String repo, @Body Hook hook); }
  18. Retrofit interface GitHub { @POST("/repos/{owner}/{repo}/hooks") Response createHook( @Path("owner") String owner,

    @Path("repo") String repo, @Body Hook hook); } class Hook { String name; Map<String, Object> config; List<String> events; boolean active; }
  19. Retrofit RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://drive.google.com/api/") .setConverter(new ProtoConverter()) .build();

  20. Retrofit RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://drive.google.com/api/") .setConverter(new ProtoConverter()) .setClient(new

    OkClient()) .build();
  21. Retrofit OkHttpClient client = new OkHttpClient(); client.setProtocols(Arrays.asList(HTTP_2)); RestAdapter restAdapter =

    new RestAdapter.Builder() .setEndpoint("https://drive.google.com/api/") .setConverter(new ProtoConverter()) .setClient(new OkClient(client)) .build();
  22. Retrofit • Form URL encoding • Response streaming • Request

    interceptors • Dynamic headers • Android • Multipart • Logging • Mock RestAdapter • Asynchronous invoke • AppEngine ...and more!
  23. Retrofit + RxJava

  24. Retrofit + RxJava interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors( @Path("owner")

    String owner, @Path("repo") String repo); }
  25. Retrofit + RxJava interface GitHub { @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> contributors( @Path("owner")

    String owner, @Path("repo") String repo); }
  26. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .forEach(c -> println(c.login +

    '\t' + c.contributions));
  27. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .forEach(c -> println(c.login +

    '\t' + c.contributions)); 1483 benjchristensen 225 zsxwing 167 samuelgruetter 146 jmhofer 137 akarnokd 105 DavidMGross 102 AppliedDuality ...
  28. Retrofit + RxJava interface GitHub { @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> contributors( @Path("owner")

    String owner, @Path("repo") String repo); @GET("/users/{user}") Observable<User> user( @Path("user") String user); }
  29. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .flatMap(c -> gitHub.user(c.login)) .forEach(user

    -> println(user.name));
  30. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .flatMap(c -> gitHub.user(c.login)) .forEach(user

    -> println(user.name)); Ben Christensen Shixiong Zhu null Joachim Hofer null David Gross null ...
  31. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .flatMap(c -> gitHub.user(c.login)) .filter(user

    -> user.name != null) .forEach(user -> println(user.name));
  32. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .flatMap(c -> gitHub.user(c.login)) .filter(user

    -> user.name != null) .forEach(user -> println(user.name)); Ben Christensen Shixiong Zhu Joachim Hofer David Gross Matthias Käppler Justin Ryan Mairbek Khadikov ...
  33. Retrofit + RxJava interface GitHub { @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> contributors( @Path("owner")

    String owner, @Path("repo") String repo); @GET("/users/{user}/starred") Observable<List<Repo>> starred( @Path("user") String user); }
  34. Retrofit + RxJava gitHub.contributors("netflix", "rxjava") .lift(flattenList()) .flatMap(c -> gitHub.starred(c.login)) .lift(flattenList())

    .groupBy(r -> r.full_name) .flatMap(g -> g.count().map(c -> c + "\t" + g.getKey())) .toSortedList((a, b) -> b.compareTo(a)) .lift(flattenList()) .take(8) .forEach(Main::println);
  35. Retrofit + RxJava 7 Netflix/RxJava 2 twitter/finagle 2 scala/scala 2

    mbostock/d3 2 kpelykh/docker-java 2 Netflix/zuul 2 Netflix/feign 2 Netflix/archaius
  36. Retrofit + RxJava gitHub.contributors("square", "retrofit") .lift(flattenList()) .flatMap(c -> gitHub.starred(c.login)) .lift(flattenList())

    .groupBy(r -> r.full_name) .flatMap(g -> g.count().map(c -> c + "\t" + g.getKey())) .toSortedList((a, b) -> b.compareTo(a)) .lift(flattenList()) .take(8) .forEach(Main::println);
  37. gitHub.contributors("square", "retrofit") .lift(flattenList()) .flatMap(c -> gitHub.starred(c.login)) .lift(flattenList()) .filter(r -> !r.full_name.startsWith("square/"))

    .groupBy(r -> r.full_name) .flatMap(g -> g.count().map(c -> c + "\t" + g.getKey())) .toSortedList((a, b) -> b.compareTo(a)) .lift(flattenList()) .take(8) .forEach(Main::println); Retrofit + RxJava
  38. Retrofit + RxJava 4 frankiesardo/auto-parcel 4 Comcast/FreeFlow 3 xxv/android-lifecycle 3

    robolectric/robolectric 3 inmite/android-butterknife-zelezny 3 google/auto 3 facebook/rebound 3 etsy/AndroidStaggeredGrid
  39. Retrofit + RxJava 4 frankiesardo/auto-parcel 4 Comcast/FreeFlow 3 xxv/android-lifecycle 3

    robolectric/robolectric 3 inmite/android-butterknife-zelezny 3 google/auto 3 facebook/rebound 3 etsy/AndroidStaggeredGrid 3 Netflix/RxJava
  40. Retrofit square.github.io/retrofit