fuel-rxjava

The rxjava extension package for Fuel.

Installation

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

  1. compile 'com.github.kittinunf.fuel:fuel:<latest-version>'
  2. compile 'com.github.kittinunf.fuel:fuel-rxjava:<latest-version>'

Usage

See RxFuel.kt

Responses

  • Fuel supports RxJava right off the box.
  1. "https://www.example.com/photos/1".httpGet()
  2. .rxObject(Photo.Deserializer())
  3. .subscribe { /* do something */ }
  • There are extensions over Request that provide RxJava 2.x Single<Result<T, FuelError>> as return type.
  1. /**
  2. * Returns a reactive stream for a [Single] value response [ByteArray]
  3. *
  4. * @see rxBytes
  5. * @return [Single<T>] the [ByteArray] wrapped into a [Pair] and [Result]
  6. */
  7. fun Request.rxResponse() = rxResponseSingle(ByteArrayDeserializer())
  8. fun Request.rxResponsePair() = rxResponsePair(ByteArrayDeserializer())
  9. fun Request.rxResponseTriple() = rxResponseTriple(ByteArrayDeserializer())
  10.  
  11. /**
  12. * Returns a reactive stream for a [Single] value response [String]
  13. *
  14. * @see rxString
  15. *
  16. * @param charset [Charset] the character set to deserialize with
  17. * @return [Single<Pair<Response, Result<String, FuelError>>>] the [String] wrapped into a [Pair] and [Result]
  18. */
  19. fun Request.rxResponseString(charset: Charset = Charsets.UTF_8) = rxResponseSingle(StringDeserializer(charset))
  20. fun Request.rxResponseStringPair(charset: Charset = Charsets.UTF_8) = rxResponsePair(StringDeserializer(charset))
  21. fun Request.rxResponseStringTriple(charset: Charset = Charsets.UTF_8) = rxResponseTriple(StringDeserializer(charset))
  22.  
  23. /**
  24. * Returns a reactive stream for a [Single] value response object [T]
  25. *
  26. * @see rxObject
  27. *
  28. * @param deserializable [Deserializable<T>] something that can deserialize the [Response] to a [T]
  29. * @return [Single<Pair<Response, Result<T, FuelError>>>] the [T] wrapped into a [Pair] and [Result]
  30. */
  31. fun <T : Any> Request.rxResponseObject(deserializable: Deserializable<T>) = rxResponseSingle(deserializable)
  32. fun <T : Any> Request.rxResponseObjectPair(deserializable: Deserializable<T>) = rxResponsePair(deserializable)
  33. fun <T : Any> Request.rxResponseObjectTriple(deserializable: Deserializable<T>) = rxResponseTriple(deserializable)
  34.  
  35. /**
  36. * Returns a reactive stream for a [Single] value response [ByteArray]
  37. *
  38. * @see rxResponse
  39. * @return [Single<Result<ByteArray, FuelError>>] the [ByteArray] wrapped into a [Result]
  40. */
  41. fun Request.rxBytes() = rxResult(ByteArrayDeserializer())
  42.  
  43. /**
  44. * Returns a reactive stream for a [Single] value response [ByteArray]
  45. *
  46. * @see rxResponseString
  47. *
  48. * @param charset [Charset] the character set to deserialize with
  49. * @return [Single<Result<String, FuelError>>] the [String] wrapped into a [Result]
  50. */
  51. fun Request.rxString(charset: Charset = Charsets.UTF_8) = rxResult(StringDeserializer(charset))
  52.  
  53. /**
  54. * Returns a reactive stream for a [Single] value response [T]
  55. *
  56. * @see rxResponseObject
  57. *
  58. * @param deserializable [Deserializable<T>] something that can deserialize the [Response] to a [T]
  59. * @return [Single<Result<T, FuelError>>] the [T] wrapped into a [Result]
  60. */
  61. fun <T : Any> Request.rxObject(deserializable: Deserializable<T>) = rxResult(deserializable)