title: How to use rxjava2 with retrofit in android
body: <p>Hi I am trying to learn rxjava2. I am trying to call API's using rxjava2 and using retrofit for building URL and converting JSON into Moshi.</p>
  
  <p>I want to use <code>Observable</code> pattern with <code>retrofit</code>. Does anyone know whats way to do it ? Any standard and best approach like wrapper for error handling and all ?</p>
  
  <p><strong>AppModule.kt</strong></p>
  
  <pre><code>@Provides
  @Singleton
  fun provideRetrofit(moshi: Moshi, okHttpClient: OkHttpClient): Retrofit {
      return Retrofit.Builder()
              .addConverterFactory(MoshiConverterFactory.create(moshi))
              .baseUrl(BuildConfig.BASE_URL)
              .client(okHttpClient)
              .build()
  }
  </code></pre>
  
  <p><strong>ApiHelperImpl.kt</strong></p>
  
  <pre><code>@Inject
  lateinit var retrofit: Retrofit
  
  override fun doServerLoginApiCall(email: String, password: String): Observable&lt;LoginResponse&gt; {
      retrofit.create(RestApi::class.java).login(email, password)
  }
  </code></pre>
  
  <p>I am calling <code>doServerLoginApiCall</code> from the <code>LoginViewModel</code> like below</p>
  
  <p><strong>LoginViewModel.kt</strong></p>
  
  <pre><code>fun login(view: View) {
      if (isEmailAndPasswordValid(email, password)) {
          ApiHelperImpl().doServerLoginApiCall(email, password)
      }
  }
  </code></pre>
  
  <p><strong>RestApi.kt</strong></p>
  
  <pre><code>interface RestApi {
  
      @FormUrlEncoded
      @POST("/partner_login")
      fun login(@Field("email") email: String, @Field("password") password: String): Call&lt;LoginResponse&gt;
  }
  </code></pre>
  
  <p><strong>LoginResponse.kt</strong></p>
  
  <pre><code>data class LoginResponse(
  
          @Json(name = "code")
          val code: Int? = null,
  
          @Json(name = "otp_verify")
          val otpVerify: Int? = null,
  
          @Json(name = "data")
          val userDetails: UserDetails? = null,
  
          @Json(name = "message")
          val message: String? = null,
  
          @Json(name = "status")
          val status: String? = null
  )
  </code></pre>