Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch request using Retrofit

I'd like to perform batch request using Retrofit. It there any nice way, how to achieve it? Basically what I'm trying to do is to replace some characters in query part of URL (replace block is allowed only in path part of URL - using @Path annotation).

Here is a pseudocode for my problem.

@GET("/v2/multi?requests=/users/self,/venues/search?client_id={client_id}&client_secret={client_secret}&v={v}&ll={ll}&intent={intent}&limit={limit}")
    ProfileSearchVenuesResponse searchVenuesAndProfiles(@ReplaceBy("client_id") String clientId,
                          @ReplaceBy("client_secret") String clientSecret,
                          @ReplaceBy("v") int version,
                          @ReplaceBy("ll") String location,
                          @ReplaceBy("intent") String intent,
                          @ReplaceBy("limit") int limit);
like image 418
sealskej Avatar asked Oct 09 '14 22:10

sealskej


People also ask

Can you use multiple Baseurl in retrofit?

Yes, you can create two different Service instances.

Can we call API in batch?

Batch calls allow API applications to make multiple API calls within a single API call. In addition, each call can be tied to a different access_token meaning batch API calls work with multiple users. Batch calls allow your platform to accomplish more API interaction with less traffic, thus avoiding throttle limits.

How do I send parameters in retrofit?

You can pass parameter by @QueryMap Retrofit uses annotations to translate defined keys and values into appropriate format. Using the @Query("key") String value annotation will add a query parameter with name key and the respective string value to the request url .


1 Answers

@Query is what you are looking for:

@GET("/v2/multi?requests=/users/self,/venues/search")
ProfileSearchVenuesResponse searchVenuesAndProfiles(
    @Query("client_id") String clientId,
    @Query("client_secret") String clientSecret,
    @Query("v") int version,
    @Query("ll") String location,
    @Query("intent") String intent,
    @Query("limit") int limit);

In version 1.7.0 of Retrofit (released yesterday) the exception message for attempting to use @Path in the original question instructs you as to the right solution:

URL query string "client_id={client_id}&client_secret={client_secret}&v={v}&ll={ll}&intent={intent}&limit={limit}" must not have replace block. For dynamic query parameters use @Query.

like image 52
Jake Wharton Avatar answered Oct 05 '22 19:10

Jake Wharton