Comments (7)
Hi, you can check merge.
from sandwich.
If i look at documentation merge can only be use with ApiResponse<List>, but my requirement is not list. So i cant use that
from sandwich.
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")
}
from sandwich.
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")
}
from sandwich.
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")
}
from sandwich.
thankyou i will explore it 👍
from sandwich.
@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.
from sandwich.
Related Issues (20)
- After enable R8 full mode getting ParameterizedType error HOT 7
- Maven Central Gradle 8 compatibility
- How to handle data structures like BaseResponse HOT 2
- Integrate Benchmark + Baseline Profiles
- Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option HOT 1
- Interface can't be instantiated! Register an InstanceCreator or a TypeAdapter for this type. HOT 2
- Difference Exception Handling between Ktor and Retrofit HOT 1
- ApiResponseCallAdapterFactory is missing in 2.0.4 HOT 2
- runAndRetry not working when type of ApiResponse had type variable HOT 8
- deserializeErrorBody() throws internal exceptions HOT 6
- Any Roadmap to Support Kotlin Multiplatform? HOT 2
- Can I use Sandwich with Mongodb Realm API HOT 1
- Couldn't resolve ApiResponseCallAdapter class HOT 1
- Global ApiResponseFailureMapper not returning desired results HOT 1
- [ASK] How can I get throw exception as onError/onFailure/onException in ViewModel ? HOT 1
- How to get custom error from my server? HOT 1
- [Help] Is it the best approach to validate responses? HOT 2
- Dependency Dashboard
- EmptyBodyInterceptor doesn't work with GsonConverterFactory HOT 6
- java.lang.NullPointerException:::sandwich.ApiResponse$Failure$Error.toString HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sandwich.