-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from THT-Team/feature/TOP-90_refresh_token
TOP-90 feature Http Code가 500일 때 error 필드값에 따라서 토큰 만료, Internal Server Errro 구분
- Loading branch information
Showing
2 changed files
with
68 additions
and
19 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
data/src/main/java/com/tht/tht/data/remote/retrofit/ThtHeaderInterceptor.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,64 @@ | ||
package com.tht.tht.data.remote.retrofit | ||
|
||
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<FetchThtAccessTokenUseCase> | ||
) : 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() | ||
} | ||
} | ||
} | ||
} |