|
| 1 | +package com.threegap.bitnagil.di |
| 2 | + |
| 3 | +import com.threegap.bitnagil.BuildConfig |
| 4 | +import com.threegap.bitnagil.network.auth.AuthInterceptor |
| 5 | +import dagger.Module |
| 6 | +import dagger.Provides |
| 7 | +import dagger.hilt.InstallIn |
| 8 | +import dagger.hilt.components.SingletonComponent |
| 9 | +import kotlinx.serialization.json.Json |
| 10 | +import okhttp3.Interceptor |
| 11 | +import okhttp3.MediaType.Companion.toMediaType |
| 12 | +import okhttp3.OkHttpClient |
| 13 | +import okhttp3.logging.HttpLoggingInterceptor |
| 14 | +import retrofit2.Converter |
| 15 | +import retrofit2.Retrofit |
| 16 | +import retrofit2.converter.kotlinx.serialization.asConverterFactory |
| 17 | +import java.util.concurrent.TimeUnit |
| 18 | +import javax.inject.Singleton |
| 19 | + |
| 20 | +@Module |
| 21 | +@InstallIn(SingletonComponent::class) |
| 22 | +object NetworkModule { |
| 23 | + private const val APPLICATION_JSON = "application/json" |
| 24 | + |
| 25 | + @Provides |
| 26 | + @Singleton |
| 27 | + fun provideBaseUrl(): String = BuildConfig.BASE_URL |
| 28 | + |
| 29 | + @Provides |
| 30 | + @Singleton |
| 31 | + fun provideJson(): Json = |
| 32 | + Json { |
| 33 | + ignoreUnknownKeys = true |
| 34 | + prettyPrint = true |
| 35 | + explicitNulls = false |
| 36 | + } |
| 37 | + |
| 38 | + @Provides |
| 39 | + @Singleton |
| 40 | + fun provideJsonConverter(json: Json): Converter.Factory = |
| 41 | + json.asConverterFactory(APPLICATION_JSON.toMediaType()) |
| 42 | + |
| 43 | + @Provides |
| 44 | + @Singleton |
| 45 | + fun provideHttpLoggingInterceptor(): Interceptor = |
| 46 | + HttpLoggingInterceptor().apply { |
| 47 | + level = if (BuildConfig.DEBUG) { |
| 48 | + HttpLoggingInterceptor.Level.BODY |
| 49 | + } else { |
| 50 | + HttpLoggingInterceptor.Level.NONE |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Provides |
| 55 | + @Singleton |
| 56 | + @Auth |
| 57 | + fun provideAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor = authInterceptor |
| 58 | + |
| 59 | + @Provides |
| 60 | + @Singleton |
| 61 | + @Auth |
| 62 | + fun provideAuthOkHttpClient( |
| 63 | + httpLoggingInterceptor: HttpLoggingInterceptor, |
| 64 | + authInterceptor: Interceptor, |
| 65 | + ): OkHttpClient = OkHttpClient.Builder() |
| 66 | + .addInterceptor(authInterceptor) |
| 67 | + .addInterceptor(httpLoggingInterceptor) |
| 68 | + .connectTimeout(10L, TimeUnit.SECONDS) |
| 69 | + .writeTimeout(30L, TimeUnit.SECONDS) |
| 70 | + .readTimeout(30L, TimeUnit.SECONDS) |
| 71 | + .build() |
| 72 | + |
| 73 | + @Provides |
| 74 | + @Singleton |
| 75 | + @NoneAuth |
| 76 | + fun provideNoneAuthOkHttpClient( |
| 77 | + httpLoggingInterceptor: HttpLoggingInterceptor, |
| 78 | + ): OkHttpClient = OkHttpClient.Builder() |
| 79 | + .addInterceptor(httpLoggingInterceptor) |
| 80 | + .connectTimeout(10L, TimeUnit.SECONDS) |
| 81 | + .writeTimeout(30L, TimeUnit.SECONDS) |
| 82 | + .readTimeout(30L, TimeUnit.SECONDS) |
| 83 | + .build() |
| 84 | + |
| 85 | + @Provides |
| 86 | + @Singleton |
| 87 | + @Auth |
| 88 | + fun provideAuthRetrofit( |
| 89 | + baseUrl: String, |
| 90 | + converterFactory: Converter.Factory, |
| 91 | + @Auth okHttpClient: OkHttpClient, |
| 92 | + ): Retrofit = Retrofit.Builder() |
| 93 | + .baseUrl(baseUrl) |
| 94 | + .addConverterFactory(converterFactory) |
| 95 | + .client(okHttpClient) |
| 96 | + .build() |
| 97 | + |
| 98 | + @Provides |
| 99 | + @Singleton |
| 100 | + @NoneAuth |
| 101 | + fun provideNoneAuthRetrofit( |
| 102 | + baseUrl: String, |
| 103 | + converterFactory: Converter.Factory, |
| 104 | + @NoneAuth okHttpClient: OkHttpClient, |
| 105 | + ): Retrofit = Retrofit.Builder() |
| 106 | + .baseUrl(baseUrl) |
| 107 | + .addConverterFactory(converterFactory) |
| 108 | + .client(okHttpClient) |
| 109 | + .build() |
| 110 | +} |
0 commit comments