-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1654e8e
commit 08a340a
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
app/src/main/java/co/orange/ddanzi/di/RetrofitModule.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import co.orange.core.extension.isJsonArray | ||
import co.orange.core.extension.isJsonObject | ||
import co.orange.ddanzi.BuildConfig.BASE_URL | ||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import timber.log.Timber | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RetrofitModule { | ||
private const val APPLICATION_JSON = "application/json" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJson(): Json = | ||
Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJsonConverter(json: Json): Converter.Factory = | ||
json.asConverterFactory( | ||
APPLICATION_JSON.toMediaType(), | ||
) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideHttpLoggingInterceptor(): Interceptor = | ||
HttpLoggingInterceptor { message -> | ||
when { | ||
message.isJsonObject() -> | ||
Timber.tag("okhttp").d(JSONObject(message).toString(4)) | ||
|
||
message.isJsonArray() -> | ||
Timber.tag("okhttp").d(JSONArray(message).toString(4)) | ||
|
||
else -> { | ||
Timber.tag("okhttp").d("CONNECTION INFO -> $message") | ||
} | ||
} | ||
}.apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.JWT | ||
fun provideAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor = authInterceptor | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.JWT | ||
fun provideJWTOkHttpClient( | ||
loggingInterceptor: Interceptor, | ||
@RetrofitQualifier.JWT authInterceptor: Interceptor, | ||
): OkHttpClient = | ||
OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.addInterceptor(authInterceptor) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.NOTOKEN | ||
fun provideReissueOkHttpClient(loggingInterceptor: Interceptor): OkHttpClient = | ||
OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.JWT | ||
fun provideJWTRetrofit( | ||
@RetrofitQualifier.JWT client: OkHttpClient, | ||
factory: Converter.Factory, | ||
): Retrofit = | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(client) | ||
.addConverterFactory(factory) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.NOTOKEN | ||
fun provideReissueRetrofit( | ||
@RetrofitQualifier.NOTOKEN client: OkHttpClient, | ||
factory: Converter.Factory, | ||
): Retrofit = | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(client) | ||
.addConverterFactory(factory) | ||
.build() | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/co/orange/ddanzi/di/RetrofitQualifier.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import javax.inject.Qualifier | ||
|
||
object RetrofitQualifier { | ||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class JWT | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class NOTOKEN | ||
} |