Skip to content

Commit

Permalink
Minor formatting and clean up for ResultCall (#3684)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront authored Aug 6, 2024
1 parent f110687 commit 0216702
Showing 1 changed file with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.HttpException
import retrofit2.Response
import retrofit2.Response.success
import java.lang.reflect.Type

/**
Expand All @@ -23,63 +22,51 @@ class ResultCall<T>(

override fun clone(): Call<Result<T>> = ResultCall(backingCall, successType)

@Suppress("UNCHECKED_CAST")
private fun createResult(body: T?): Result<T> {
return when {
body != null -> body.asSuccess()
successType == Unit::class.java -> (Unit as T).asSuccess()
else -> IllegalStateException("Unexpected null body!").asFailure()
}
}

override fun enqueue(callback: Callback<Result<T>>): Unit = backingCall.enqueue(
object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
val body = response.body()
val result: Result<T> = if (!response.isSuccessful) {
HttpException(response).asFailure()
} else {
createResult(body)
}
callback.onResponse(this@ResultCall, success(result))
callback.onResponse(this@ResultCall, Response.success(response.toResult()))
}

override fun onFailure(call: Call<T>, t: Throwable) {
val result: Result<T> = t.asFailure()
callback.onResponse(this@ResultCall, success(result))
callback.onResponse(this@ResultCall, Response.success(t.asFailure()))
}
},
)

/**
* Synchronously send the request and return its response as a [Result].
*/
fun executeForResult(): Result<T> = requireNotNull(execute().body())

@Suppress("TooGenericExceptionCaught")
override fun execute(): Response<Result<T>> {
val response = try {
backingCall.execute()
override fun execute(): Response<Result<T>> =
try {
Response.success(backingCall.execute().toResult())
} catch (ioException: IOException) {
return success(ioException.asFailure())
Response.success(ioException.asFailure())
} catch (runtimeException: RuntimeException) {
return success(runtimeException.asFailure())
Response.success(runtimeException.asFailure())
}

return success(
if (!response.isSuccessful) {
HttpException(response).asFailure()
} else {
createResult(response.body())
},
)
}

override fun isCanceled(): Boolean = backingCall.isCanceled

override fun isExecuted(): Boolean = backingCall.isExecuted

override fun request(): Request = backingCall.request()

override fun timeout(): Timeout = backingCall.timeout()

/**
* Synchronously send the request and return its response as a [Result].
*/
fun executeForResult(): Result<T> = requireNotNull(execute().body())

private fun Response<T>.toResult(): Result<T> =
if (!this.isSuccessful) {
HttpException(this).asFailure()
} else {
val body = this.body()
@Suppress("UNCHECKED_CAST")
when {
body != null -> body.asSuccess()
successType == Unit::class.java -> (Unit as T).asSuccess()
else -> IllegalStateException("Unexpected null body!").asFailure()
}
}
}

0 comments on commit 0216702

Please sign in to comment.