Skip to content Skip to sidebar Skip to footer

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,

Solution 1:

I guess you could work with Named annotations:

@Module(
        complete = false,
        library = true
)publicfinalclassApiModule {

    publicstaticfinalStringPRODUCTS_BASE_URL="https://products.com";
    publicstaticfinalStringSUBSIDIARIES_BASE_URL="https://subsidiaries.com";

    publicstaticfinalStringPRODUCTS="products";
    publicstaticfinalStringSUBSIDIARIES="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() {
        ObjectMapperobjectMapper=newObjectMapper();
        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) {
        returnnewRestAdapter.Builder()
                       .setLogLevel(RestAdapter.LogLevel.NONE)
                       .setConverter(newJacksonConverter(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.

Post a Comment for "How To Address Multiple Api End Points Using Retrofit?"