Issue #135

From https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter/blob/master/src/main/java/com/jakewharton/retrofit2/adapter/kotlin/coroutines/experimental/CoroutineCallAdapterFactory.kt#L86

override fun adapt(call: Call<T>): Deferred<T> {
  val deferred = CompletableDeferred<T>()

  deferred.invokeOnCompletion {
    if (deferred.isCancelled) {
      call.cancel()
    }
  }

  call.enqueue(object : Callback<T> {
    override fun onFailure(call: Call<T>, t: Throwable) {
      deferred.completeExceptionally(t)
    }

    override fun onResponse(call: Call<T>, response: Response<T>) {
      if (response.isSuccessful) {
        deferred.complete(response.body()!!)
      } else {
        deferred.completeExceptionally(HttpException(response))
      }
    }
  })

  return deferred
}