Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Retrofit and RxJava (Netflix Open Source Meetup...

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/

Jake Wharton

July 09, 2014
Tweet

More Decks by Jake Wharton

Other Decks in Technology

Transcript

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

    String repo); } class Contributor { String login; long contributions; }
  2. 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");
  3. 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 ...
  4. 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; }
  5. 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();
  6. Retrofit • Form URL encoding • Response streaming • Request

    interceptors • Dynamic headers • Android • Multipart • Logging • Mock RestAdapter • Asynchronous invoke • AppEngine ...and more!
  7. 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 ...
  8. 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); }
  9. 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 ...
  10. 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 ...
  11. 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); }
  12. 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);
  13. 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
  14. 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);
  15. 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
  16. 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
  17. 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