Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to address multiple API end points using Retrofit?

In my Android project I am using the following Retrofit ApiModule for one API end point. Please note, I use Dagger for injecting dependencies.

@Module(
        complete = false,
        library = true
)
public final class ApiModule {

    public static final String PRODUCTS_BASE_URL = "https://products.com";

    @Provides
    @Singleton
    Endpoint provideEndpoint() {
        return Endpoints.newFixedEndpoint(PRODUCTS_BASE_URL);
    }

    @Provides
    @Singleton
    ObjectMapper provideObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(
            PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        return objectMapper;
    }

    @Provides
    @Singleton
    RestAdapter provideRestAdapter(
        Endpoint endpoint, ObjectMapper objectMapper) {
        return new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.NONE)
                .setEndpoint(endpoint)
                .setConverter(new JacksonConverter(objectMapper))
                .build();
    }

    @Provides
    @Singleton
    ProductsService provideProductsService(RestAdapter restAdapter) {
        return restAdapter.create(ProductsService.class);
    }

}

Now, there is another API (e.g. "http://subsidiaries.com") which I want to communicate with. Is it possible to extend the given ApiModule while reusing the ObjectMapper and the RestAdapter? Or should I not extend it? I already tried to duplicate the module. But this involves that I have to duplicate the Endpoint, ObjectMapper and ... the RestAdapter has a private contructor - so I can't.

like image 300
JJD Avatar asked Dec 10 '14 11:12

JJD


People also ask

How do I retrofit an API call?

If we want to consume the API asynchronously, we call the service as follows: String username = "sarahjean"; Call<User> call = apiService. getUser(username); call. enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { int statusCode = response.

Is retrofit REST API?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.

What is retrofit in Android with example?

In Android, Retrofit is a REST Client for Java and Android by Square inc under Apache 2.0 license. Its a simple network library that used for network transactions. By using this library we can seamlessly capture JSON response from web service/web API.


1 Answers

I guess you could work with Named annotations:

@Module(
        complete = false,
        library = true
)
public final class ApiModule {

    public static final String PRODUCTS_BASE_URL = "https://products.com";
    public static final String SUBSIDIARIES_BASE_URL = "https://subsidiaries.com";

    public static final String PRODUCTS = "products";
    public static final String SUBSIDIARIES = "subsidiaries";

    @Provides
    @Singleton
    @Named(PRODUCTS)
    Endpoint provideProductsEndpoint() {
        return Endpoints.newFixedEndpoint(PRODUCTS_BASE_URL);
    }

    @Provides
    @Singleton
    @Named(SUBSIDIARIES)
    Endpoint provideSubsidiariesEndpoint() {
        return Endpoints.newFixedEndpoint(SUBSIDIARIES_BASE_URL);
    }

    @Provides
    @Singleton
    ObjectMapper provideObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(
            PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        return objectMapper;
    }

    @Provides
    @Singleton
    @Named(PRODUCTS)
    RestAdapter provideProductsRestAdapter(@Named(PRODUCTS) Endpoint endpoint, ObjectMapper objectMapper) {
        return newRestAdapterBuilder(objectMapper)
                .setEndpoint(endpoint)
                .build();
    }

    @Provides
    @Singleton
    @Named(SUBSIDIARIES)
    RestAdapter provideSubsidiariesRestAdapter(@Named(SUBSIDIARIES) Endpoint endpoint, ObjectMapper objectMapper) {
        return newRestAdapterBuilder(objectMapper)
                .setEndpoint(endpoint)
                .build();
    }

    @Provides
    @Singleton
    @Named(PRODUCTS)
    ProductsService provideProductsService(@Named(PRODUCTS) RestAdapter restAdapter) {
        return restAdapter.create(ProductsService.class);
    }

    @Provides
    @Singleton
    @Named(SUBSIDIARIES)
    ProductsService provideSubsidiariesService(@Named(SUBSIDIARIES) RestAdapter restAdapter) {
        return restAdapter.create(ProductsService.class);
    }

    private RestAdapter.Builder newRestAdapterBuilder(ObjectMapper objectMapper) {
        return new RestAdapter.Builder()
                       .setLogLevel(RestAdapter.LogLevel.NONE)
                       .setConverter(new JacksonConverter(objectMapper));
    }
}

Now everywhere where you inject ProductsService you need to either annotate the dependency with @Named(PRODUCTS) or @Named(SUBSIDIARIES), depending on which variant you need. Of course instead of the @Named annotations you could also create your own, custom annotations and use them. See here under "Qualifiers".

To flatten your module a bit you could move the creation of the RestAdapters into the provide*Service() methods and get rid of the provide*RestAdapter() methods. Unless you need the RestAdapters as a dependency outside of the module, of course.

like image 124
Sven Jacobs Avatar answered Sep 21 '22 14:09

Sven Jacobs