Calling the same WebAPI multiple times while changing the request parameters.I am using Merge from RxJava.
WebAPI will return the price if you specify the product name, but the product name you specified in the request is not included in the response.
What are some ways to learn about your request when you get a response?
(Unfortunately, the WebAPI specification cannot be changed.)
Kotlin, Retrofit2, RxJava2
If you specify a product name, return the price of the product in JSON.
request://price/apples
response: {price:180}
valapiArray=arrayOf(
apiClient.getPrice("Apple"),
apiClient.getPrice("Momo"),
apiClient.getPrice("Grape")
)
Observable.merge (*apiArray)
.subscribeOn(Schedulers.io())
.observeOn (AndroidScheduler.mainThread())
.doAfterTerminate{
println ("It's all over")
}
.subscribe({price->
// ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
// ※※ ※I want to know the name of the product you requested here※※※
// ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
println("${xxx} is priced at ${price.price} yen")
},{
println("Error")
})
interface ApiClient {
@Headers("accept:application/json")
@ GET("/price/{name}")
fungetPrice(@Path("name")name:String):Observable<Price>
}
data class price (val price: Int)
WebAPI specifications cannot be changed, so I think it would be better to prepare another API and relay it.
Transit API Overview
·[How to call] Same as WebAPI, specifying a product name.
·[Operation]
If the relay API is called, the Web API is called with the designated commodity name to obtain a price.
Then, return the product name and price in json format.
Something like that.
You want to use parameters such as "Apple"
with the results.
Then, Observable.merge
causes events to be out of order and difficult to handle, so why don't you combine the results with flatMap
based on the array of product names?(The behavior remains the same)
val names=arrayOf("Apple", "Momo", "Grape")
Observable
.fromArray(names)
.flatMap {name->
// Play name and result pair on map
apiClient.getPrice(name).map {name to it}
}
.subscribeOn(Schedulers.io())
.observeOn (AndroidScheduler.mainThread())
.doAfterTerminate{
println ("It's all over")
}
.subscribe({(name,price)->
println("${name} is priced at ${price.price} yen")
}, {
println("Error")
})
If you do merge
after the Pair of parameters and results is streamed, it will be the same behavior, so I will list that as well.
val names=arrayOf("Apple", "Momo", "Grape")
Observable
.merge(names.map {name->
// Play name and result pair on map
apiClient.getPrice(name).map {name to it}
})
...
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
578 Understanding How to Configure Google API Key
914 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
611 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.