Last active
October 18, 2015 02:41
-
-
Save dfpalomar/edb182cd7e33ec624a41 to your computer and use it in GitHub Desktop.
ApiClient for multiple service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful! Thanks.