BayernMessenger/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/Request.kt

83 lines
2.9 KiB
Kotlin
Raw Normal View History

2019-01-18 10:12:08 +00:00
/*
* Copyright 2019 New Vector Ltd
2019-01-18 10:12:08 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2019-01-18 10:12:08 +00:00
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2019-01-18 10:12:08 +00:00
*/
package im.vector.matrix.android.internal.network
2018-10-03 15:56:33 +00:00
import arrow.core.Try
import arrow.core.failure
import arrow.core.recoverWith
import arrow.effects.IO
import arrow.effects.fix
import arrow.effects.instances.io.async.async
import arrow.integrations.retrofit.adapter.runAsync
2019-06-13 14:39:35 +00:00
import com.squareup.moshi.JsonDataException
2018-10-03 15:56:33 +00:00
import com.squareup.moshi.Moshi
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.MatrixError
2018-10-13 09:18:49 +00:00
import im.vector.matrix.android.internal.di.MoshiProvider
2018-10-03 15:56:33 +00:00
import okhttp3.ResponseBody
import retrofit2.Call
2019-06-13 14:39:35 +00:00
import timber.log.Timber
2018-10-03 15:56:33 +00:00
import java.io.IOException
internal inline fun <DATA> executeRequest(block: Request<DATA>.() -> Unit) = Request<DATA>().apply(block).execute()
2018-10-03 15:56:33 +00:00
internal class Request<DATA> {
2018-10-03 15:56:33 +00:00
private val moshi: Moshi = MoshiProvider.providesMoshi()
lateinit var apiCall: Call<DATA>
fun execute(): Try<DATA> {
return Try {
val response = apiCall.runAsync(IO.async()).fix().unsafeRunSync()
2018-10-03 15:56:33 +00:00
if (response.isSuccessful) {
response.body() ?: throw IllegalStateException("The request returned a null body")
2018-10-03 15:56:33 +00:00
} else {
2019-06-13 14:39:35 +00:00
throw manageFailure(response.errorBody(), response.code())
2018-10-03 15:56:33 +00:00
}
}.recoverWith {
when (it) {
2019-06-13 14:39:35 +00:00
is IOException -> Failure.NetworkConnection(it)
is Failure.ServerError,
is Failure.OtherServerError -> it
else -> Failure.Unknown(it)
}.failure()
2018-10-03 15:56:33 +00:00
}
}
2019-06-13 14:39:35 +00:00
private fun manageFailure(errorBody: ResponseBody?, httpCode: Int): Throwable {
if (errorBody == null) {
return RuntimeException("Error body should not be null")
}
val errorBodyStr = errorBody.string()
val matrixErrorAdapter = moshi.adapter(MatrixError::class.java)
try {
val matrixError = matrixErrorAdapter.fromJson(errorBodyStr)
2018-10-03 15:56:33 +00:00
2019-06-13 14:39:35 +00:00
if (matrixError != null) {
return Failure.ServerError(matrixError, httpCode)
}
} catch (ex: JsonDataException) {
// This is not a MatrixError
Timber.w("The error returned by the server is not a MatrixError")
}
return Failure.OtherServerError(errorBodyStr, httpCode)
}
2018-10-03 15:56:33 +00:00
}