Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASK] How to create sequential or parallel request? #11

Closed
fjr619 opened this issue Feb 20, 2021 · 7 comments
Closed

[ASK] How to create sequential or parallel request? #11

fjr619 opened this issue Feb 20, 2021 · 7 comments

Comments

@fjr619
Copy link

fjr619 commented Feb 20, 2021

right now i'm using CoroutinesResponseCallAdapterFactory, how to create sequential request and parallel request with sandwich?
Example for sequential = i have API for getConfig and getBanner, frist getConfig, when succeed it will call getBanner
Example for parallel = API getProductDetail, getUserLimit. both API will parralel then result of 2 api will combine into 1 result

thank you

@skydoves
Copy link
Owner

Hi, you can check merge.

@fjr619
Copy link
Author

fjr619 commented Feb 21, 2021

If i look at documentation merge can only be use with ApiResponse<List>, but my requirement is not list. So i cant use that

@skydoves
Copy link
Owner

skydoves commented Feb 21, 2021

I got it.
Here is a simple example using the toFlow by ApiResponse, and combine or zip by Flow.

    val response1 = withContext(Dispatchers.IO) { pokedexClient.fetchPokemonInfo(name = name1) }
    val flow1: Flow<PokemonInfo> = response1.toFlow()

    val response2 = withContext(Dispatchers.IO) { pokedexClient.fetchPokemonInfo(name = name2) }
    val flow2: Flow<PokemonInfo> = response2.toFlow()

    val mergedFlow: Flow<String> = flow1.combine(flow2) { info1, info2 ->
      "${info1.name}, ${info2.name}"
    }

    mergedFlow.collect {
      Log.e("Test", "merged information: $it")
    }

@skydoves
Copy link
Owner

And you can handle the error case like this:

    val response1 = withContext(Dispatchers.IO) { pokedexClient.fetchPokemonInfo(name = "bulbasaur") }
    val flow1: Flow<PokemonInfo> = response1.toFlow()

    val response2 = withContext(Dispatchers.IO) {
      pokedexClient.fetchPokemonInfo(name = "Wrong name").onError {
        Log.e("Test", "You've got an error by fetching response2..") }
    }
    val flow2: Flow<PokemonInfo> = response2.toFlow()

    val mergedFlow: Flow<String> = flow1.combine(flow2) { info1, info2 ->
      "${info1.name}, ${info2.name}"
    }

    mergedFlow.collect {
      Log.e("Test", "merged information: $it")
    }

@skydoves
Copy link
Owner

And here is an example that you can just request sequentially and emit data as a single Flow.

flow {
      pokedexClient.fetchPokemonInfo(name = "bulbasaur")
        .suspendOnSuccess { emit(data?.name!! + " response1") }
        .onError {
          Log.e("Test", "You've got an error by fetching response1..")
        }

      pokedexClient.fetchPokemonInfo(name = "bulbasaur")
        .suspendOnSuccess { emit(data?.name!! + " response2") }
        .onError {
          Log.e("Test", "You've got an error by fetching response2..")
        }
    }.flowOn(Dispatchers.IO).collect {
      Log.e("Test", "collected response: $it")
    }

@fjr619
Copy link
Author

fjr619 commented Feb 21, 2021

thankyou i will explore it 👍

@chanjungkim
Copy link

chanjungkim commented Jan 16, 2023

@skydoves
I am trying to call APIs sequentially,

    suspend fun reCheckout(
        phoneNumber: String,
        orderId: String,
        items: ArrayList< CartItemAddBody>,
        checkoutBody: CheckoutRequestBody
    ) = flow {
        loginAndRegister(phoneNumber)
        cancelOrder(orderId, OrderCancelRequestBody("store", "re-checkout"))
        addMultiCartItem(items)
        checkOut(checkoutBody)
    }

How can I achieve it? it must be called one by one in order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants