Last active
June 29, 2017 13:23
-
-
Save chris-carneiro/b667f0556d555f8189872ae4a7945e2c to your computer and use it in GitHub Desktop.
Example of a Rest Client using retrofit
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
| /** | |
| * Created by ChrisC on 22/05/17. | |
| */ | |
| public enum RestClient { | |
| INSTANCE; | |
| transient Retrofit apiClient = new Retrofit.Builder() | |
| .baseUrl(BuildConfig.WS_SERVER) | |
| .client(getAPIKeyClient()) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| WS service = apiClient.create(WS.class); | |
| public OkHttpClient getTokenClient() { | |
| HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
| logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
| return new OkHttpClient.Builder() | |
| .addInterceptor(new TokenInterceptor()) | |
| .addInterceptor(logging) | |
| .build(); | |
| } | |
| public OkHttpClient getAPIKeyClient() { | |
| HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
| logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
| return new OkHttpClient.Builder() | |
| .addInterceptor(new APIKeyInterceptor()) | |
| .addInterceptor(logging) | |
| .build(); | |
| } | |
| public OkHttpClient getLogClient() { | |
| HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
| logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
| return new OkHttpClient.Builder() | |
| .addInterceptor(logging) | |
| .build(); | |
| } | |
| public WS getApiService() { | |
| return service; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment