Search code examples
androidretrofit2github-apirx-java2clean-architecture

How do I access response headers while using RxJava2 and Retrofit2?


The Premise

I'm working on a simple app where I want to list out a user's GitHub repos in a RecyclerView. I'm using this as my endpoint while building this.

The Problem

The problem I'm facing is that the GitHub API returns only 30 repos in one go. To get more, I can append a per_page=100 (100 is the maximum) to my query string; But, what do we do about users with more than 100 repos?

The solution the API docs provide is to get the next; url from the "Link" response header to make a second API call.

How does one go about this? Thanks!


Solution

  • The Response class will let you access the headers. The GitHub client would have a method like:

    @GET("/search/code")
    Observable<Response<T>> getUser(...)
    

    Inside onNext(), you'd do something like:

    @Override
    public void onNext(Response<T> response) {
        String next = response.headers().get("next");
    }