title: Chaining API Requests with Retrofit + Rx
body: <p>I am trying to develop a client for Hacker News using <a href="https://github.com/HackerNews/API" rel="nofollow noreferrer">this</a> API, just for learning how Android works, as a personal project. I tried following some tutorials, but I am stuck at a certain point.</p>
  <p>I want to retrieve the top N stories' titles, upvotes, etc. This would be done, using this api by:</p>
  <ol>
  <li>Making a request to the api to retrieve the ID's of top posts (500 of them, to be exact)</li>
  <li>For each ID, make a request to the api's posts endpoint to retrieve the details.</li>
  </ol>
  <p><strong>It seems that I am stuck on how to create N different network requests for the posts that I want, retrieving them and putting them on a List, then displaying them on my Fragment.</strong>
  I am trying to follow an MVVM pattern, with Repositories. The relevant files are here:</p>
  <p>NewsApi.kt:</p>
  <pre><code>interface NewsApi {
  
      @GET(&quot;topstories.json&quot;)
      fun getTopStories() : Single&lt;List&lt;Int&gt;&gt;
  
      @GET(&quot;item/{id}&quot;)
      fun getItem(@Path(&quot;id&quot;) id: String): Single&lt;News&gt;
  }
  </code></pre>
  <p>MainRepository.kt (I):</p>
  <pre><code>interface MainRepository {
  
      fun getTopStoryIDs(): Single&lt;List&lt;Int&gt;&gt;
  
      fun getStory(storyId: Int): Single&lt;News&gt;
  
      fun getTop20Stories(): Single&lt;List&lt;News&gt;&gt;
  }
  </code></pre>
  <p>The <code>News</code> object is a simple data class with all the JSON fields that are returned from <code>item/{id}</code>, so I am omitting it.</p>
  <p>Here is my Repository, the implementation:</p>
  <pre><code>class DefaultMainRepository @Inject constructor(
      private val api: NewsApi
  ) : MainRepository {
      override fun getTopStoryIDs(): Single&lt;List&lt;Int&gt;&gt; {
          return api.getTopStories()
      }
  
      override fun getStory(storyId: Int): Single&lt;News&gt; {
          return api.getItem(storyId.toString())
      }
  
      override fun getTop20Stories(): Single&lt;List&lt;News&gt;&gt; {
          TODO(&quot;HOW?&quot;)
      }
  }
  </code></pre>
  <p>The top questions I have are:</p>
  <ol>
  <li>How can I make chained API calls in this way, using Retrofit / RxJava? I have reviewed previous answers using flatMap, but in my case, using a List of Int's, I do not actually know how to do that correctly.</li>
  <li>Is this the right way to go about this? Should I just ditch the architectural choices I've made, and try to think in a wholly new way?</li>
  <li>Say I can complete <code>getTop20Stories</code> (which, as the name implies, should retrieve 20 of the news, using the result from <code>getTopStoryIDs</code>, first 20 elements for the time should do the trick), how would I be able to retrieve data from it? Who should do the honors of retrieving the response? VM? Fragment?</li>
  </ol>
  <p>Thanks in advance.</p>