Skip to content

Commit

Permalink
[ADD/#37] 토큰 헤더별 Retrofit 객체 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Jul 9, 2024
1 parent 1654e8e commit 08a340a
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
112 changes: 112 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/RetrofitModule.kt
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 app/src/main/java/co/orange/ddanzi/di/RetrofitQualifier.kt
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
}

0 comments on commit 08a340a

Please sign in to comment.