From bb8d5dd55a40c3e80e350af1fbcbc3aa9e058944 Mon Sep 17 00:00:00 2001 From: wjchoi96 Date: Fri, 12 Jan 2024 23:15:55 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:TOP-90=20ThtHeaderInterceptor=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - code 500일때, error 필드 값에 따라서 토큰 만료인지, internal serveer error인지 구분 --- .../di/retrofit/OkHttpInterceptorModule.kt | 23 ++----- .../remote/retrofit/ThtHeaderInterceptor.kt | 66 +++++++++++++++++++ 2 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt diff --git a/data/src/main/java/com/tht/tht/data/di/retrofit/OkHttpInterceptorModule.kt b/data/src/main/java/com/tht/tht/data/di/retrofit/OkHttpInterceptorModule.kt index 4549b1aa..3f10a6ed 100644 --- a/data/src/main/java/com/tht/tht/data/di/retrofit/OkHttpInterceptorModule.kt +++ b/data/src/main/java/com/tht/tht/data/di/retrofit/OkHttpInterceptorModule.kt @@ -1,9 +1,9 @@ package com.tht.tht.data.di.retrofit +import com.google.gson.Gson import com.tht.tht.data.BuildConfig +import com.tht.tht.data.remote.retrofit.ThtHeaderInterceptor import com.tht.tht.data.remote.retrofit.TokenRefreshAuthenticator -import com.tht.tht.data.remote.retrofit.header.HttpHeaderKey -import com.tht.tht.domain.token.model.TokenException import com.tht.tht.domain.token.token.FetchThtAccessTokenUseCase import com.tht.tht.domain.token.token.RefreshThtAccessTokenUseCase import dagger.Lazy @@ -11,7 +11,6 @@ import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent -import kotlinx.coroutines.runBlocking import okhttp3.Interceptor import okhttp3.logging.HttpLoggingInterceptor @@ -21,23 +20,9 @@ object OkHttpInterceptorModule { @Provides fun provideHeaderInterceptor( + gson: Gson, fetchThtAccessTokenUseCase: Lazy - ): Interceptor = Interceptor { chain -> - val requestBuilder = chain.request().newBuilder() - .header(HttpHeaderKey.CONTENT_TYPE_HEADER_KEY, HttpHeaderKey.CONTENT_TYPE_HEADER_VALUE) - val accessToken = runBlocking { fetchThtAccessTokenUseCase.get().invoke().getOrNull() } - if (accessToken != null) { - requestBuilder.header( - HttpHeaderKey.AUTHORIZATION_HEADER_KEY, - "${HttpHeaderKey.BEARER_PREFIX} $accessToken" - ) - } - chain.proceed(requestBuilder.build()).also { response -> - when (response.code) { - 500 -> throw TokenException.RefreshTokenExpiredException() - } - } - } + ): Interceptor = ThtHeaderInterceptor(gson, fetchThtAccessTokenUseCase) @Provides fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = diff --git a/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt b/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt new file mode 100644 index 00000000..eb447138 --- /dev/null +++ b/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt @@ -0,0 +1,66 @@ +package com.tht.tht.data.remote.retrofit + +import android.util.Log +import com.google.gson.Gson +import com.tht.tht.data.remote.response.base.ErrorResponse +import com.tht.tht.data.remote.retrofit.header.HttpHeaderKey +import com.tht.tht.domain.token.model.TokenException +import com.tht.tht.domain.token.token.FetchThtAccessTokenUseCase +import dagger.Lazy +import kotlinx.coroutines.runBlocking +import okhttp3.Interceptor +import okhttp3.Response +import okhttp3.ResponseBody +import javax.inject.Inject + +class ThtHeaderInterceptor @Inject constructor( + private val gson: Gson, + private val fetchThtAccessTokenUseCase: Lazy +) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val requestBuilder = chain.request().newBuilder() + .header(HttpHeaderKey.CONTENT_TYPE_HEADER_KEY, HttpHeaderKey.CONTENT_TYPE_HEADER_VALUE) + val accessToken = runBlocking { fetchThtAccessTokenUseCase.get().invoke().getOrNull() } + if (accessToken != null) { + requestBuilder.header( + HttpHeaderKey.AUTHORIZATION_HEADER_KEY, + "${HttpHeaderKey.BEARER_PREFIX} $accessToken" + ) + } + + val response = chain.proceed(requestBuilder.build()) + return response.body?.string()?.let { + checkTokenExpiredException(response, parseErrorResponse(it)) + //response 는 한 번 밖에 읽지 못함 + response.newBuilder() + .body(ResponseBody.create(response.body!!.contentType(), it)) + .build() + } ?: kotlin.run { + checkTokenExpiredException(response, null) + response + } + } + + private fun parseErrorResponse(errorJson: String): ErrorResponse? { + return try { + gson.fromJson(errorJson, ErrorResponse::class.java) + } catch (e : Exception) { + e.printStackTrace() + null + } + } + + private fun checkTokenExpiredException( + response: Response, + errorResponse: ErrorResponse? + ) { + when { + errorResponse?.error == "refresh_token_expired" && + response.code == 500 -> { + throw TokenException.RefreshTokenExpiredException() + } + + } + } +} From cde965ff2893ebdca8e6628b6843ec5901b48497 Mon Sep 17 00:00:00 2001 From: wjchoi96 Date: Sat, 13 Jan 2024 17:31:19 +0900 Subject: [PATCH 2/2] chore:TOP-90 lint check --- .../com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt b/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt index eb447138..a19d3f39 100644 --- a/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt +++ b/data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.kt @@ -1,6 +1,5 @@ package com.tht.tht.data.remote.retrofit -import android.util.Log import com.google.gson.Gson import com.tht.tht.data.remote.response.base.ErrorResponse import com.tht.tht.data.remote.retrofit.header.HttpHeaderKey @@ -45,7 +44,7 @@ class ThtHeaderInterceptor @Inject constructor( private fun parseErrorResponse(errorJson: String): ErrorResponse? { return try { gson.fromJson(errorJson, ErrorResponse::class.java) - } catch (e : Exception) { + } catch (e: Exception) { e.printStackTrace() null } @@ -60,7 +59,6 @@ class ThtHeaderInterceptor @Inject constructor( response.code == 500 -> { throw TokenException.RefreshTokenExpiredException() } - } } }