Ниже вставлен фрагмент кода из приложения Google Plaid , которое обертывает функции приостановки и безопасно выполняет сетевые операции. Какие изменения необходимо внести, чтобы он работал с функциями Rx Java, а не с сопрограммами, и ждал результата сети, любая помощь приветствуется.
/**
* Wrap a suspending API [call] in try/catch. In case an exception is thrown, a [Result.Error] is
* created based on the [errorMessage].
*/
suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> {
return try {
call()
} catch (e: Exception) {
// An exception was thrown when calling the API so we're converting this to an IOException
Result.Error(IOException(errorMessage, e))
}
}
Пример использования в ProductHuntRemoteDataSource.kt:
class ProductHuntRemoteDataSource @Inject constructor(private val service: ProductHuntService) {
/**
* Load Product Hunt data for a specific page.
*/
suspend fun loadData(page: Int) = safeApiCall(
call = { requestData(page) },
errorMessage = "Error loading ProductHunt data"
)
private suspend fun requestData(page: Int): Result<GetPostsResponse> {
val response = service.getPostsAsync(page)
if (response.isSuccessful) {
val body = response.body()
if (body != null) {
return Result.Success(body)
}
}
return Result.Error(IOException("Error loading ProductHunt data " +
"${response.code()} ${response.message()}"))
}
}