Untitled

 avatar
unknown
kotlin
2 months ago
710 B
4
Indexable
fun <DomainType, ApiDataModel> fetchListAndMap(call: Call<List<ApiDataModel>>, mapper: Mapper<ApiDataModel, DomainType>): Result<List<DomainType>> {
    return try {
        val apiCallResult = call.execute()
        if(!apiCallResult.isSuccessful) {
            return Result.Error(exception = Throwable(apiCallResult.message()))
        }
        if(apiCallResult.body() == null) {
            return Result.Error(exception = NoBodyResponseException())
        }
        val responseData = apiCallResult.body()!!
        val mappedResponse = mapper.mapToDomainModelList(responseData)
        return Result.Success(mappedResponse)
    }catch (ex: Exception) {
        Result.Error(ex)
    }
}
Leave a Comment