Да, это полностью выполнимо с сопрограммами:
interface MyApi{
@GET
suspend fun firstRequest(): Response<FirstRequestResonseObject>
@GET
suspend fun secondRequest(): Response<SecondRequestResponseObject>
}
Теперь вызов:
coroutineScope.launch{
//Start first http request by retrofit.
val firstRequest = api.getFirstRequest()
if(firstRequest.isSuccessFul){
//Only after success finish then start second http request by retrofit.
val secondRequest = api.getSecondRequest()
}
//If first request fail then NOT start second request.
}
Но вы можете рассмотреть свои исключения:
val coroutineExceptionHandler = CoroutineExceptionHandler{_, throwable -> throwable.printStackTrace()
}
А затем:
coroutineScope.launch(coroutineExceptionHandler ){
val firstRequest = api.getFirstRequest()
if(firstRequest.isSuccessFul){
val secondRequest = api.getSecondRequest()
}
}
Готово!
Для этого подхода необходимо установить модификацию 2.6 или выше.В противном случае ваши ответы должны быть Deferred<Response<FirstResponseObject>>
, а запросы api.getFirstRequest().await()