Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: network module 을 사용한 401 refresh 방식 수정 #697

Open
wants to merge 3 commits into
base: chongdae
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import com.zzang.chongdae.auth.dto.response.MemberResponse
import com.zzang.chongdae.common.handler.DataError
import com.zzang.chongdae.common.handler.Result
import com.zzang.chongdae.data.remote.util.safeApiCall
import com.zzang.chongdae.di.annotations.AuthApiServiceQualifier
import javax.inject.Inject

class AuthRemoteDataSourceImpl
@Inject
constructor(
@AuthApiServiceQualifier private val service: AuthApiService,
private val service: AuthApiService,
) : AuthRemoteDataSource {
override suspend fun saveLogin(tokensRequest: TokensRequest): Result<MemberResponse, DataError.Network> {
return safeApiCall { service.postLogin(tokensRequest) }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.zzang.chongdae.data.remote.interceptor

import com.zzang.chongdae.common.handler.Result
import com.zzang.chongdae.domain.usecase.auth.RefreshTokenUseCase
import dagger.Lazy
import kotlinx.coroutines.runBlocking
import okhttp3.Authenticator
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import javax.inject.Inject

class TokenAuthenticator
@Inject
constructor(
private val refreshTokenUseCase: Lazy<RefreshTokenUseCase>,
) : Authenticator {
override fun authenticate(
route: Route?,
response: Response,
): Request? {
if (response.request.header(AUTHORIZATION_HEADER) != null) {
return null
}

Comment on lines +13 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 Authorization 헤더가 있는 경우에는 refreshTokenUseCase가 필요없으니 Lazy를 사용하신건가용

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 Lazy 에 대한 코드 내용 설명이 빠졌네요. 지금 NetworkModule은 refreshTokenUseCase가 필요한데,
NetworkModule에서 auth 관련 api service를 provide해주고 있습니다. 이 때문에 순환 참조 문제가 일어나기 때문에 사용할때 refreshTokenUseCase를 불러오도록 Lazy를 사용하였습니다.

val newToken =
runBlocking {
when (val result = refreshTokenUseCase.get()()) {
is Result.Success -> result.data
else -> null
}
} ?: return null

return response.request.newBuilder()
.header(AUTHORIZATION_HEADER, "$TOKEN_PREFIX$newToken")
.build()
}
Comment on lines +26 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분이 무한 refresh api에 문제가 있어서 무한 refresh에 빠지는 것을 막는 부분인가여??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니당 AUTHORIZATION_HEADER을 생성해주어서 refresh 를 진행한 애들에게는 AUTHORIZATION_HEADER 값이 null 이 아닌 것으로 감지하여 무한 루프에 빠지는 것을 방지했습니다!


companion object {
private const val AUTHORIZATION_HEADER = "Authorization"
private const val TOKEN_PREFIX = "Bearer "
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import com.zzang.chongdae.data.remote.dto.response.comment.CommentsResponse
import com.zzang.chongdae.data.remote.dto.response.comment.UpdatedStatusResponse
import com.zzang.chongdae.data.remote.util.safeApiCall
import com.zzang.chongdae.data.source.comment.CommentRemoteDataSource
import com.zzang.chongdae.di.annotations.CommentDetailApiServiceQualifier
import javax.inject.Inject

class CommentRemoteDataSourceImpl
@Inject
constructor(
@CommentDetailApiServiceQualifier private val service: CommentApiService,
private val service: CommentApiService,
) : CommentRemoteDataSource {
override suspend fun saveComment(commentRequest: CommentRequest): Result<Unit, DataError.Network> =
safeApiCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import com.zzang.chongdae.data.remote.api.CommentApiService
import com.zzang.chongdae.data.remote.dto.response.commentroom.CommentRoomsResponse
import com.zzang.chongdae.data.remote.util.safeApiCall
import com.zzang.chongdae.data.source.CommentRoomsDataSource
import com.zzang.chongdae.di.annotations.CommentRoomsApiServiceQualifier
import javax.inject.Inject

class CommentRoomsDataSourceImpl
@Inject
constructor(
@CommentRoomsApiServiceQualifier private val commentApiService: CommentApiService,
private val commentApiService: CommentApiService,
) : CommentRoomsDataSource {
override suspend fun fetchCommentRooms(): Result<CommentRoomsResponse, DataError.Network> {
return safeApiCall { commentApiService.getCommentRooms() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import com.zzang.chongdae.data.remote.dto.request.ParticipationRequest
import com.zzang.chongdae.data.remote.dto.response.offering.OfferingDetailResponse
import com.zzang.chongdae.data.remote.util.safeApiCall
import com.zzang.chongdae.data.source.OfferingDetailDataSource
import com.zzang.chongdae.di.annotations.OfferingApiServiceQualifier
import com.zzang.chongdae.di.annotations.ParticipantApiServiceQualifier
import javax.inject.Inject

class OfferingDetailDataSourceImpl
@Inject
constructor(
@OfferingApiServiceQualifier private val offeringApiService: OfferingApiService,
@ParticipantApiServiceQualifier private val participationApiService: ParticipationApiService,
private val offeringApiService: OfferingApiService,
private val participationApiService: ParticipationApiService,
) : OfferingDetailDataSource {
override suspend fun fetchOfferingDetail(offeringId: Long): Result<OfferingDetailResponse, DataError.Network> =
safeApiCall { offeringApiService.getOfferingDetail(offeringId) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import com.zzang.chongdae.data.remote.dto.response.offering.RemoteOffering
import com.zzang.chongdae.data.remote.mapper.toProductUrlRequest
import com.zzang.chongdae.data.remote.util.safeApiCall
import com.zzang.chongdae.data.source.offering.OfferingRemoteDataSource
import com.zzang.chongdae.di.annotations.OfferingApiServiceQualifier
import okhttp3.MultipartBody
import javax.inject.Inject

class OfferingRemoteDataSourceImpl
@Inject
constructor(
@OfferingApiServiceQualifier private val service: OfferingApiService,
private val service: OfferingApiService,
) : OfferingRemoteDataSource {
override suspend fun fetchOffering(offeringId: Long): Result<RemoteOffering, DataError.Network> =
safeApiCall { service.getOffering(offeringId) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import com.zzang.chongdae.data.remote.api.ParticipationApiService
import com.zzang.chongdae.data.remote.dto.response.participants.ParticipantsResponse
import com.zzang.chongdae.data.remote.util.safeApiCall
import com.zzang.chongdae.data.source.ParticipantRemoteDataSource
import com.zzang.chongdae.di.annotations.ParticipantApiServiceQualifier
import javax.inject.Inject

class ParticipantRemoteDataSourceImpl
@Inject
constructor(
@ParticipantApiServiceQualifier private val service: ParticipationApiService,
private val service: ParticipationApiService,
) : ParticipantRemoteDataSource {
override suspend fun fetchParticipants(offeringId: Long): Result<ParticipantsResponse, DataError.Network> =
safeApiCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ annotation class AuthRepositoryQualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AuthDataSourceQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AuthApiServiceQualifier
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ annotation class CommentDetailRepositoryQualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class CommentDetailDataSourceQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class CommentDetailApiServiceQualifier
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ annotation class OfferingDetailRepositoryQualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class OfferingDetailDataSourceQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class OfferingDetailApiServiceQualifier
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ annotation class OfferingRemoteDataSourceQualifier
@Retention(AnnotationRetention.BINARY)
annotation class OfferingLocalDataSourceQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class OfferingApiServiceQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class OfferingDaoQualifier
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ annotation class ParticipantRepositoryQualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class ParticipantDataSourceQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class ParticipantApiServiceQualifier
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.zzang.chongdae.di.module

import com.zzang.chongdae.auth.api.AuthApiService
import com.zzang.chongdae.auth.repository.AuthRepository
import com.zzang.chongdae.auth.repository.AuthRepositoryImpl
import com.zzang.chongdae.auth.source.AuthRemoteDataSource
import com.zzang.chongdae.auth.source.AuthRemoteDataSourceImpl
import com.zzang.chongdae.data.remote.api.NetworkManager
import com.zzang.chongdae.di.annotations.AuthApiServiceQualifier
import com.zzang.chongdae.di.annotations.AuthDataSourceQualifier
import com.zzang.chongdae.di.annotations.AuthRepositoryQualifier
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
Expand All @@ -28,13 +24,4 @@ abstract class AuthDependencyModule {
@Singleton
@AuthDataSourceQualifier
abstract fun provideAuthDataSource(impl: AuthRemoteDataSourceImpl): AuthRemoteDataSource

companion object {
@Provides
@Singleton
@AuthApiServiceQualifier
fun provideAuthApiService(): AuthApiService {
return NetworkManager.authService()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.zzang.chongdae.di.module

import com.zzang.chongdae.data.remote.api.CommentApiService
import com.zzang.chongdae.data.remote.api.NetworkManager
import com.zzang.chongdae.data.remote.source.CommentRemoteDataSourceImpl
import com.zzang.chongdae.data.repository.CommentDetailRepositoryImpl
import com.zzang.chongdae.data.source.comment.CommentRemoteDataSource
import com.zzang.chongdae.di.annotations.CommentDetailApiServiceQualifier
import com.zzang.chongdae.di.annotations.CommentDetailDataSourceQualifier
import com.zzang.chongdae.di.annotations.CommentDetailRepositoryQualifier
import com.zzang.chongdae.domain.repository.CommentDetailRepository
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
Expand All @@ -28,13 +24,4 @@ abstract class CommentDetailDependencyModule {
@Singleton
@CommentDetailDataSourceQualifier
abstract fun provideCommentDetailDataSource(impl: CommentRemoteDataSourceImpl): CommentRemoteDataSource

companion object {
@Provides
@Singleton
@CommentDetailApiServiceQualifier
fun provideCommentDetailApiService(): CommentApiService {
return NetworkManager.commentService()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.zzang.chongdae.di.module

import com.zzang.chongdae.data.remote.api.CommentApiService
import com.zzang.chongdae.data.remote.api.NetworkManager
import com.zzang.chongdae.data.remote.source.CommentRoomsDataSourceImpl
import com.zzang.chongdae.data.repository.CommentRoomsRepositoryImpl
import com.zzang.chongdae.data.source.CommentRoomsDataSource
import com.zzang.chongdae.di.annotations.CommentRoomsApiServiceQualifier
import com.zzang.chongdae.di.annotations.CommentRoomsDataSourceQualifier
import com.zzang.chongdae.di.annotations.CommentRoomsRepositoryQualifier
import com.zzang.chongdae.domain.repository.CommentRoomsRepository
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
Expand All @@ -28,13 +24,4 @@ abstract class CommentRoomsDependencyModule {
@Singleton
@CommentRoomsDataSourceQualifier
abstract fun provideCommentRoomsDataSource(impl: CommentRoomsDataSourceImpl): CommentRoomsDataSource

companion object {
@Provides
@Singleton
@CommentRoomsApiServiceQualifier
fun provideCommentRoomsService(): CommentApiService {
return NetworkManager.commentService()
}
}
}
Loading