fuel-reactor

The reactor extension package for Fuel.

Installation

You can download and install fuel-reactor with Maven and Gradle. The reactor package has the following dependencies:

  • Fuel
  • Project Reactor: 3.2.2.RELEASE
  1. implementation 'com.github.kittinunf.fuel:fuel:<latest-version>'
  2. implementation 'com.github.kittinunf.fuel:fuel-reactor:<latest-version>'

Usage

See FuelReactor.kt

Responses

The Reactor module API provides functions starting with the prefix mono to handle instances of Response, Result<T, FuelError> and values directly (String, ByteArray, Any). All functions expose exceptions as FuelError instance.

Data handling example

  1. Fuel.get("https://icanhazdadjoke.com")
  2. .header(Headers.ACCEPT to "text/plain")
  3. .monoString()
  4. .subscribe(::println)

Error handling example

  1. data class Guest(val name: String)
  2.  
  3. object GuestMapper : ResponseDeserializable<Guest> {
  4. override fun deserialize(content: String) =
  5. jacksonObjectMapper().readValue<Guest>(content)
  6. }
  7.  
  8. Fuel.get("/guestName").monoResultObject(GuestMapper)
  9. .map(Result<Guest, FuelError>::get)
  10. .map { (name) -> "Welcome to the party, $name!" }
  11. .onErrorReturn("I'm sorry, your name is not on the list.")
  12. .subscribe(::println)

Response handling example

  1. FuelManager.instance.basePath = "https://httpbin.org"
  2.  
  3. Fuel.get("/status/404").monoResponse()
  4. .filter(Response::isSuccessful)
  5. .switchIfEmpty(Fuel.get("/status/200").monoResponse())
  6. .map(Response::statusCode)
  7. .subscribe(::println)