Skip to content

Instantly share code, notes, and snippets.

@dfpalomar
Last active October 18, 2015 02:41
Show Gist options
  • Save dfpalomar/edb182cd7e33ec624a41 to your computer and use it in GitHub Desktop.
Save dfpalomar/edb182cd7e33ec624a41 to your computer and use it in GitHub Desktop.
ApiClient for multiple service
import com.jakewharton.u2020.data.api.GithubService;
import com.jakewharton.u2020.data.api.transforms.WeatherService;
import com.squareup.okhttp.OkHttpClient;
import retrofit.Retrofit;
public class ApiClient {
private static GithubService githubService;
private static WeatherService weatherService;
private ApiClient() {
// No instances
}
public static GithubService getGithubService(){
if (githubService == null) {
githubService = RestAdapter.GITHUB.getApiClient().create(GithubService.class);
}
return githubService;
}
public static WeatherService getWeatherService(){
if (weatherService == null) {
weatherService = RestAdapter.WEATHER.getApiClient().create(WeatherService.class);
}
return weatherService;
}
private enum RestAdapter {
GITHUB {
@Override
public Retrofit getApiClient() {
return new Retrofit.Builder()
.client(new OkHttpClient())
.baseUrl(BuildConfig.GithubEndpoint)
// .addConverterFactory(MoshiConverterFactory.create(null))
.build();
}
},
WEATHER {
@Override
public Retrofit getApiClient() {
return new Retrofit.Builder()
.client(new OkHttpClient())
.baseUrl(BuildConfig.WeatherEndpoint)
// .addConverterFactory(MoshiConverterFactory.create(null))
.build();
}
};
abstract Retrofit getApiClient();
}
}
@Sottti
Copy link

Sottti commented Oct 18, 2015

Very useful! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment