I would like to know the content of my request when I get a WebAPI response.

Asked 1 years ago, Updated 1 years ago, 103 views

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.)

environment

Kotlin, Retrofit2, RxJava2

WebAPI Specifications

If you specify a product name, return the price of the product in JSON. request://price/apples
response: {price:180}

Code

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)

android kotlin retrofit

2022-09-30 17:27

2 Answers

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.


2022-09-30 17:27

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")
    })

Add

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}
    })
    ...


2022-09-30 17:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.