Simplifying the usage of similar API calls

78 views Asked by At

I have 2 different requests that have the same parameters and response handling, but it depends on one flag if the app should use on or the other API.

Everything works just fine but I have a question about simplifying the Main.java.

I tried to use java.lang.reflect.Method but to no avail. What are the other options to simplify this?

MyRestApi.java

public interface MyRestApi {
   @POST("first")
   Call<Void> firstRequest(@Header(AUTH) String authorization, @Body String body);     

   @POST("second")
   Call<Void> secondRequest(@Header(AUTH) String authorization, @Body String body); 
}

Main.java

JobService.ApiRequestor apirequestor;
MyRestApi restApi;

if(true) {
    apiRequestor.request(restApi.firstRequest(auth, body), res -> {
        doStuff(res);
    });
} else {
    apiRequestor.request(restApi.secondRequest(auth, body), res -> {
        doStuff(res);
    });
}
0

There are 0 answers