В моем приложении для Android я использую Retrofit 2 lib.
Здесь API
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
interface TraderMonitorRestClient {
@GET("traders/json")
suspend fun getTraidersList(): Response<List<Trader>>
@GET("trader/{trader_operation}")
suspend fun executeTraderOperation(@Path("trader_operation") traderOperation: String,
@Query("base") base: String,
@Query("quote") quote: String,
@Query("sender") sender: String,
@Query("key") key: String): Response<Void>
}
здесь использование:
suspend fun getTraidersList(isCustomtHandle: Boolean = false): Response<*> {
suspend fun execOperation(): Response<*> = traderMonitorRestClient.getTraidersList()
return runOperation(isCustomtHandle, ::execOperation)
}
suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String, isCustomtHandle: Boolean = false): Response<*> {
val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
suspend fun execOperation(): Response<*> = traderMonitorRestClient.executeTraderOperation(
traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
return runOperation(isCustomtHandle, ::execOperation)
}
private suspend fun runOperation(isCustomtHandle: Boolean, func: suspend () -> Response<*>): Response<*> {
if (isCustomtHandle) {
return runOperationWithoutHandle(func)
} else {
return runOperationWithDefaultHandle(func)
}
}
suspend private fun runOperationWithoutHandle(func: suspend () -> Response<*>): Response<*> = withContext(Dispatchers.IO) {
val response: Response<*> = func() // execute sequential http request and in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
response
}
и здесь использование:
class TradersViewModel(application: Application) : AndroidViewModel(application) {
private fun loadData() {
viewModelScope.launch(Dispatchers.Main) {
val response: Response<*> = TransportService.getTraidersList()
if (response.isSuccessful) { // code >= 200 && code < 300;
finishLoadData()
val traders: List<Trader> = response.body() as List<Trader>
traderListLiveData.postValue(traders)
}
как видите, мне нужно сыграть
val traders: List<Trader> = response.body() as List<Trader>
Ницца.Работает нормально.
Но не хочу кастовать.Поэтому я пытаюсь это:
@GET("traders/json")
suspend fun getTraidersList(): List<Trader>
suspend fun getTraidersList(isCustomtHandle: Boolean = false): Response<*> {
suspend fun execOperation(): List<Trader> = traderMonitorRestClient.getTraidersList()
return runOperation(isCustomtHandle, ::execOperation)
}
Но я получаю ошибку компиляции здесь:
return runOperation(isCustomtHandle, ::execOperation)
type mismatch.
Required:
suspend () → Response<*>
Found:
KSuspendFunction0<List<Trader>>
Результат должен быть таким:
interface TraderMonitorRestClient {
@GET("traders/json")
suspend fun getTraidersList(): List<Trader>
@GET("trader/{trader_operation}")
suspend fun executeTraderOperation(@Path("trader_operation") traderOperation: String,
@Query("base") base: String,
@Query("quote") quote: String,
@Query("sender") sender: String,
@Query("key") key: String): Void