2

I am using Retrofit + RxJava + GSON to consume a rest api for search. The way the API is built, it returns a list of objects and the total results as a header in the response (total-results):

@GET("search/events?eager=[competition,tv]")
    fun searchEvents(@Query("q") q: String, @Query("category") category: String, @Query("order") order: String): Single<List<Event>>

I would like to instead return Single, where SearchResult is:

SearchResult {
 var total: Int
var events: List<Event>
}

Is this possible?

M Rajoy
  • 4,028
  • 14
  • 54
  • 111

1 Answers1

5

Instead of Single<List<Event>> modify your interface to return Single<Response<List<Event>>>.

The Response class contains both the body and the headers returned from the API call.

You can check for success with response.isSuccessful() then you can access the body using response.body() and the headers with response.headers().

To get your desired output from there you can use map() from RxJava.

LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56