1

I have several URL that accepts the same GET parameters (mainly for pagination purposes) as follow :

public interface AsynchronousApi {

    @GET("/api/users")
    public void listUsers(@Query("limit") Integer limit,
                          @Query("offset") Integer offset,
                         Callback<userList> callback);

    @GET("/api/posts")
    public void listPosts(@Query("limit") Integer limit,
                          @Query("offset") Integer offset,
                          Callback<postList> callback);

    ...

}

Since I have lots of URL, this is getting a bit repetitive. So I would like to have way to refactor this so I don't have to repeat @Query("limit") and @Query("offset") everytime. Maybe another annotation would help ?

mantal
  • 1,093
  • 1
  • 20
  • 38
Nezo
  • 312
  • 3
  • 14

2 Answers2

3

No. Retrofit values the semantic weight of separate methods much more than de-duplication of interface method declarations.

While the similar behavior of the API endpoints is good API design, it would be poor Java design. If this service was local (i.e., inside your app) you wouldn't consolidate the two methods because they fetch two very different things.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • 1
    I disagree about "bad design". Decoupling the client and the server is a GOOD thing. And that could also mean having variable URLs returned by the server. Go read about HATEOAS. Without getting to that extreme you can need having your server specify an URL to get the data. You know the format of the data, you don't know the actual URL until you ask the server. – Daniele Segato Mar 15 '16 at 10:01
  • 1
    I think you aknoledge the need since @URL has been added as an annotation: http://stackoverflow.com/questions/32559333/retrofit-2-dynamic-url – Daniele Segato Mar 15 '16 at 13:30
1

See this other question

Apparently retrofit added an @Url annotation for that purpose.

public interface APIService {
    @GET
    Call<Users> getUsers(@Url String url);
}

This feature has been added in version 2.0.0.

Community
  • 1
  • 1
Daniele Segato
  • 12,314
  • 6
  • 62
  • 88
  • 1
    Thank you Daniele! Not being able to use endpoints returned by an API was a big issue that seems to be a thing from the past, now :) – Nezo Mar 15 '16 at 13:40