Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

はやい・やすい・うまい!スタートアップでも使える Retrofit + RxJava で瞬間A...

はやい・やすい・うまい!スタートアップでも使える Retrofit + RxJava で瞬間APIクッキングレシピ

はやい・やすい・うまい!スタートアップでも使える Retrofit + RxJava で瞬間APIクッキングレシピ

Fumihiko Shiroyama

July 14, 2017
Tweet

More Decks by Fumihiko Shiroyama

Other Decks in Technology

Transcript

  1. How to implement REST Client? • HTTP Client • Thread

    Executer • JSON Deserializer • Integration with RxJava
  2. Retrofit • A type-safe HTTP client for Android and Java

    • http://square.github.io/retrofit/ • by Square
  3. Retrofit GitHubService service = retrofit.create(GitHubService.class); Call<List<Repo>> repos = service.listRepos("srym"); //

    synchronous call Response<List<Repo>> response = repos.execute(); // asynchronous call repos.enqueue( /* callback here */ );
  4. Subscribe Single<List<Repo>> repos = service.listRepos("srym") repos .subscribe( list -> doSomethingToList(list),

    throwable -> Timber.d(throwable.getMessage(), throwable), () -> Timber.d("complete") );
  5. What's Good about Retrofit + Rx • Easy to define

    • Easy to compose • Easy to test
  6. MockWebServer • A scriptable web server for testing HTTP clients

    • https://github.com/square/okhttp/tree/ master/mockwebserver
  7. MockWebServer Dispatcher dispatcher = new Dispatcher() { @Override public MockResponse

    dispatch(RecordedRequest request) throws InterruptedException { return new MockResponse().setResponseCode(404); } }; mockWebServer.setDispatcher(dispatcher); mockWebServer.start();
  8. MockWebServer @Override public MockResponse dispatch(RecordedRequest request) { if (request ==

    null || request.getPath() == null) { return new MockResponse().setResponseCode(400); } if (request.getPath().matches("/users/.+/repos")) { return new MockResponse().setBody( readJsonFromResources("users_repos.json") ).setResponseCode(200); } return new MockResponse().setResponseCode(404); }
  9. TestSubscriber • A TestSubscriber is a variety of Subscriber that

    you can use for unit testing, to perform assertions, inspect received events, or wrap a mocked Subscriber. • http://reactivex.io/RxJava/javadoc/rx/ observers/TestSubscriber.html
  10. Easy to compose public class GitHubInfraRepository implements GitHubRepository { private

    final RemoteGitHubDataSource remoteDataSource; private final RepoMapper mapper; @Inject public GitHubInfraRepository(RemoteGitHubDataSource remoteDataSource, RepoMapper mapper) { this.remoteDataSource = remoteDataSource; this.mapper = mapper; } @Override public Single<List<RepoModel>> listRepos(@NonNull String user) { return remoteDataSource .listRepos(user) .map(mapper::convert); } }
  11. Easy to compose private final RemoteGitHubDataSource remoteDataSource; private final LocalGitHubDataSource

    localGitHubDataSource; private final RepoMapper mapper; @Inject public GitHubInfraRepository(RemoteGitHubDataSource remoteDataSource, LocalGitHubDataSource localGitHubDataSource, RepoMapper mapper) { this.remoteDataSource = remoteDataSource; this.localGitHubDataSource = localGitHubDataSource; this.mapper = mapper; }
  12. Easy to compose @Override public Single<List<RepoModel>> listRepos(@NonNull String user) {

    return remoteDataSource .listRepos(user) .map(mapper::convert);
  13. Easy to compose public Single<List<RepoModel>> listRepos(@NonNull String user) { return

    localGitHubDataSource .listRepos(user) .onErrorResumeNext( remoteDataSource .listRepos(user) .retry(DEFAULT_RETRY) .doOnSuccess(localGitHubDataSource::save) ) .map(mapper::convert);