Best approach of defining endpoints for pagination retrofit2 - android

I was thinking which should be the preferred way of defining endpoints in Retrofit2 for pagination.
Should we separate the endpoints or should we define only one endpoint method and pass page=null?
#GET("/search/users")
Observable<FetchUserResponse> fetchUsers(#Query("q") String uname,
#Query("sort") String followerspage);
#GET("/search/users")
Observable<FetchUserResponse> fetchUsersPaginationCall(#Query("q") String uname,
#Query("sort") String followerspage,
#Query("page") int page);
or
#GET("/search/users")
Observable<FetchUserResponse> fetchUsers(#Query("q") String uname,
#Query("sort") String followers,
#Query("page") String page);

Adjustments must happen on the API itself, it must set a default on a page if it is null or missing on the query param. You should define one end point only for this.

Related

How to make retrofit query parameter request without key name

I am using the google auto complete web service. I want to call the
https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=Amoeba&types=establishment&location=37.76999,-122.44696&radius=500&strictbounds&key=YOUR_API_KEY
But strictbounds parameter don't have the parameter name. I have created the retrofit request but not able to add strictbounds parameter
#GET("maps/api/place/autocomplete/json")
Call<PlaceSearchResult> searchPlaceByName(#Query("input") String input, #Query("location") String location
, #Query("radius") String radius, #Query("key") String key);
How to add the parameter without key?
Change it like this:
#GET("maps/api/place/autocomplete/json?strictbounds")
Call<PlaceSearchResult> searchPlaceByName(#Query("input") String input, #Query("location") String location
, #Query("radius") String radius, #Query("key") String key);

problems with operators in url in Retrofit 2

I'm trying to learn how to use Retrofit2, and this is the URL I have to generate:
(baseUrl)/repositories?q=language:Python&sort=stars&page=1
This is the method I'm using:
Call<List<Repo>> javaRepos(
#Query("language") String language,
#Query("sort") String sort,
#Query("page") int page
);
and this is how I'm calling it:
Call<List<Repo>> call = client.javaRepos("Python", "stars", 1);
However, this is the url my code generates:
(baseUrl)/repositories?language=Python&sort=stars&page=1
The differences are:
the q= is missing;
language is followed by a = instead of a :
How can I generate the correct url using #Query parameters (or any other way, actually)?
Looks like you're misinterpreting the query string of your desired url.
q=language:Python&sort=stars&page=1 should be broken down into three key-value pairs:
q - language:Python
sort - stars
page - 1
Note that the first key is q and not language.
With that in mind, your method should look like this (and you'll have to pass "language:Python" instead of just "Python" as the first argument).
Call<List<Repo>> javaRepos(
#Query("q") String language,
#Query("sort") String sort,
#Query("page") int page
);
You need to use Path annotation according to this link : https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Path.html
#GET("repositories?q=language:{lang}&sort={sort}&page={page}")
Call<List<Repo>> javaRepos(#Path(value = "lang") String lang, #Path(value = "sort") String sort, #Path(value = "page") int page);

Change user-type in API Header or change header using variable

I declare a final variable in global
public static final String USERTYPE="customer"
Then I use this variable as
#POST
#Headers({
"Content-Type: application/json;charset=utf-8",
"Accept: application/json;charset=utf-8",
"Cache-Control: max-age=640000",
"user-type: " + APIService.STRING
})
Call<ReturnPojo> addpost(#Url String s, #Body Add body);
Later in my program, I need to change the
USERTYPE="guest"
I try
1.USERTYPE.replace("customer","guest");
2.String user="customer"
USERTYPE=user;
How can I achieve this? or How to change a final variable in java?
You can't. You've declared USERTYPE as final so the only choice you have is to use a different String.
Change Header on Run-time(Retrofit API)
We cannot change the final variable, So we cannot change the user type programmatically. By duplicating the code is not good practice. But we can be done through this,
#POST
Call<ReturnPojo> addpost(#Url String s,
#Body Add body,
#Header("user-type") String user_type,
#Header("Content-Type") String content_type,
#Header("Accept") String accept,
#Header("Cache-Control") String cache_controller);
And finally, while we call API just pass the data as the parameter.

Android Retrofit 2 make POST request multiple params (without FormUrlEncoded)

I would like to make a POST request call with multiple params instead of one #Body param. I'm not using #FormUrlEncoded annotation on this one and I don't want to. I'm using Retrofit 2.0.
Currently, the call is made this way :
#POST("user/register")
Call<APIResponse> register(#Body RequestRegisterParams params);
with RequestRegisterParams being :
public class RequestRegisterParams {
public String username;
public String email;
public String password;
}
I would like to be able to do this (with proper annotations of course) :
#POST("user/register")
Call<APIResponse> register(String username, String email, String password);
My goal is to get rid of the data model class. Is there a way to do this or a POST request without #FormUrlEncoded must have only one #Body param ? I know it can only be one #Body param but maybe with other annotations ?
Thanks in advance !
#FormUrlEncoded
#POST("user/register")
Call<APIResponse> updateUser(#Field("username") String username, #Field("email") String email, #Field("password") String password);
#Field is Named pair for a form-encoded request.

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

Categories

Resources