| 14 | import retrofit2.converter.moshi.MoshiConverterFactory; |
| 15 | |
| 16 | @Module( |
| 17 | complete = false, |
| 18 | library = true, |
| 19 | injects = { |
| 20 | OauthService.class |
| 21 | } |
| 22 | ) |
| 23 | public final class ApiModule { |
| 24 | public static final HttpUrl PRODUCTION_API_URL = HttpUrl.parse("https://api.github.com/"); |
| 25 | |
| 26 | @Provides @Singleton HttpUrl provideBaseUrl() { |
| 27 | return PRODUCTION_API_URL; |
| 28 | } |
| 29 | |
| 30 | @Provides @Singleton @Named("Api") OkHttpClient provideApiClient(OkHttpClient client, |
| 31 | OauthInterceptor oauthInterceptor) { |
| 32 | return createApiClient(client, oauthInterceptor).build(); |
| 33 | } |
| 34 | |
| 35 | @Provides @Singleton Retrofit provideRetrofit(HttpUrl baseUrl, @Named("Api") OkHttpClient client, |
| 36 | Moshi moshi) { |
| 37 | return new Retrofit.Builder() // |
| 38 | .client(client) // |
| 39 | .baseUrl(baseUrl) // |
| 40 | .addConverterFactory(MoshiConverterFactory.create(moshi)) // |
| 41 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // |
| 42 | .build(); |
| 43 | } |
| 44 | |
| 45 | @Provides @Singleton GithubService provideGithubService(Retrofit retrofit) { |
| 46 | return retrofit.create(GithubService.class); |
| 47 | } |
| 48 | |
| 49 | static OkHttpClient.Builder createApiClient(OkHttpClient client, OauthInterceptor oauthInterceptor) { |
| 50 | return client.newBuilder() |
| 51 | .addInterceptor(oauthInterceptor); |
| 52 | } |
| 53 | } |