Stefan de Kraker
04/18/2023, 5:19 PMHttpResponse
to a Result<T>
But I can't get it to work?!
class ResponseMapper {
fun <T> map (httpResponse: HttpResponse) : Result<T> {
return if (httpResponse.status.isSuccess()){
Result.success(httpResponse.body()) // This is not allowed?
} else if (httpResponse.status.value == 401) {
Result.failure(exception = AuthException())
} else {
Result.failure(BackendException(errorCode = httpResponse.status.value))
}
}
}
Cannot use 'T' as reified type parameter. Use a class instead.
Landry Norris
04/18/2023, 5:21 PMStefan de Kraker
04/18/2023, 5:21 PMsuspend inline fun <reified T> map
Landry Norris
04/18/2023, 5:23 PMStefan de Kraker
04/18/2023, 5:24 PMHttpResponse
that is returned from my ktor.request
method:
val response = ktor.request(PokemonApi.getAllPokemon(BATCH_SIZE, batch * BATCH_SIZE))
val entity = responseMapper.map(response)
responseMapper.map(response)
is not allowed:
Not enough information to infer type variable T
Landry Norris
04/18/2023, 5:25 PMval entity: Result<PokemonList> = ...
Or
val entity = mapper.map<PokemonList>(response)
Stefan de Kraker
04/18/2023, 5:31 PMResponseMapper.map(response)
?Landry Norris
04/18/2023, 5:32 PMStefan de Kraker
04/18/2023, 5:32 PMRetrofit
and the ResponseMapper
looked like this:
fun <T> map(response: Response<T>): Result<T> = when {
response.isSuccessful -> response.body()?.let(Result.Companion::success) ?: Result.failure(
exception = Throwable("Http body is empty. HttpMessage: ${response.message()} HttpCode: ${response.code()}")
)
response.code() == 401 -> Result.failure(
exception = AuthException()
)
else -> Result.failure(
BackendException(
errorCode = response.code()
)
)
}
Landry Norris
04/18/2023, 5:33 PMStefan de Kraker
04/18/2023, 5:34 PMLandry Norris
04/18/2023, 9:40 PM