-
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 #35 from YAPP-Github/feature/TNT-164
[TNT-164] 네트워크 기본 세팅
- Loading branch information
Showing
22 changed files
with
283 additions
and
3 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
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
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
18 changes: 18 additions & 0 deletions
18
data/network/src/main/java/co/kr/data/network/authenticator/Authenticator.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,18 @@ | ||
package co.kr.data.network.authenticator | ||
|
||
import co.kr.data.network.monitor.NetworkSessionMonitor | ||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import javax.inject.Inject | ||
|
||
// 구현체 주입 여부 재검토 필요. | ||
internal class Authenticator @Inject constructor( | ||
private val networkSessionMonitor: NetworkSessionMonitor, | ||
) : Authenticator { | ||
override fun authenticate(route: Route?, response: Response): Request? { | ||
networkSessionMonitor.sendExpired() | ||
return null | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
data/network/src/main/java/co/kr/data/network/di/MonitorModule.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,17 @@ | ||
package co.kr.data.network.di | ||
|
||
import co.kr.data.network.monitor.NetworkSessionMonitor | ||
import co.kr.tnt.domain.monitor.SessionMonitor | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class MonitorModule { | ||
@Binds | ||
abstract fun bindsSessionMonitor( | ||
monitor: NetworkSessionMonitor, | ||
): SessionMonitor | ||
} |
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
23 changes: 23 additions & 0 deletions
23
data/network/src/main/java/co/kr/data/network/interceptor/SessionInterceptor.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,23 @@ | ||
package co.kr.data.network.interceptor | ||
|
||
import co.kr.data.network.provider.SessionProvider | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import javax.inject.Inject | ||
|
||
internal class SessionInterceptor @Inject constructor( | ||
private val sessionProvider: SessionProvider, | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originRequest = chain.request() | ||
val requestBuilder = originRequest.newBuilder() | ||
|
||
requestBuilder.addHeader( | ||
"AUTHORIZATION", | ||
"SESSION-ID ${runBlocking { sessionProvider.getSessionId() }}", | ||
) | ||
|
||
return chain.proceed(requestBuilder.build()) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
data/network/src/main/java/co/kr/data/network/model/base/BaseErrorResponse.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,8 @@ | ||
package co.kr.data.network.model.base | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class BaseErrorResponse( | ||
val message: String, | ||
) |
31 changes: 31 additions & 0 deletions
31
data/network/src/main/java/co/kr/data/network/model/exception/NetworkException.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,31 @@ | ||
package co.kr.data.network.model.exception | ||
|
||
sealed class NetworkException(override val message: String) : Exception(message) { | ||
data class BadRequestException( | ||
override val message: String = "Bad request", | ||
) : NetworkException(message) | ||
|
||
data class UnauthorizedException( | ||
override val message: String = "Unauthorized", | ||
) : NetworkException(message) | ||
|
||
data class ForbiddenException( | ||
override val message: String = "Forbidden", | ||
) : NetworkException(message) | ||
|
||
data class NotFoundException( | ||
override val message: String = "Not Found", | ||
) : NetworkException(message) | ||
|
||
data class ConflictException( | ||
override val message: String = "Conflict", | ||
) : NetworkException(message) | ||
|
||
data class ServerException( | ||
override val message: String = "Server Error", | ||
) : NetworkException(message) | ||
|
||
data class UnknownException( | ||
override val message: String = "Unknown Error", | ||
) : NetworkException(message) | ||
} |
21 changes: 21 additions & 0 deletions
21
data/network/src/main/java/co/kr/data/network/monitor/NetworkSessionMonitor.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,21 @@ | ||
package co.kr.data.network.monitor | ||
|
||
import co.kr.tnt.domain.monitor.SessionMonitor | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class NetworkSessionMonitor @Inject constructor() : SessionMonitor { | ||
private val _onExpired = Channel<Unit>( | ||
capacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST, | ||
) | ||
override val onExpired: Flow<Unit> | ||
get() = _onExpired.receiveAsFlow() | ||
|
||
fun sendExpired() = _onExpired.trySend(Unit) | ||
} |
6 changes: 6 additions & 0 deletions
6
data/network/src/main/java/co/kr/data/network/provider/SessionProvider.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,6 @@ | ||
package co.kr.data.network.provider | ||
|
||
interface SessionProvider { | ||
suspend fun getSessionId(): String | ||
suspend fun setSessionId(sessionId: String) | ||
} |
38 changes: 38 additions & 0 deletions
38
data/network/src/main/java/co/kr/data/network/util/NetworkHandler.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,38 @@ | ||
package co.kr.data.network.util | ||
|
||
import co.kr.data.network.model.base.BaseErrorResponse | ||
import co.kr.data.network.model.exception.NetworkException | ||
import kotlinx.serialization.json.Json | ||
import retrofit2.HttpException | ||
import retrofit2.Response | ||
|
||
internal suspend inline fun <T> networkHandler( | ||
crossinline call: suspend () -> T, | ||
): T = try { | ||
call() | ||
} catch (httpException: HttpException) { | ||
val errorResponse = parseErrorResponse(httpException.response()) | ||
val message = errorResponse.message | ||
|
||
throw when (httpException.code()) { | ||
400 -> NetworkException.BadRequestException(message) | ||
401 -> NetworkException.UnauthorizedException(message) | ||
403 -> NetworkException.ForbiddenException(message) | ||
404 -> NetworkException.NotFoundException(message) | ||
409 -> NetworkException.ConflictException(message) | ||
in 500 until 600 -> NetworkException.ServerException(message) | ||
else -> NetworkException.UnknownException(message) | ||
} | ||
} | ||
|
||
private fun parseErrorResponse(response: Response<*>?): BaseErrorResponse { | ||
return try { | ||
requireNotNull( | ||
response?.errorBody()?.string()?.let { | ||
Json.decodeFromString<BaseErrorResponse>(it) | ||
}, | ||
) | ||
} catch (e: Exception) { | ||
throw NetworkException.UnknownException("Failed to parse error message,\n${e.message}") | ||
} | ||
} |
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 @@ | ||
/build |
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,15 @@ | ||
import co.kr.tnt.setNamespace | ||
|
||
plugins { | ||
id("tnt.android.library") | ||
id("tnt.android.hilt") | ||
} | ||
|
||
android { | ||
setNamespace("data.session") | ||
} | ||
|
||
dependencies { | ||
implementation(projects.data.network) | ||
implementation(projects.data.storage) | ||
} |
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,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest /> |
18 changes: 18 additions & 0 deletions
18
data/session/src/main/java/co/kr/data/session/SessionProviderImpl.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,18 @@ | ||
package co.kr.data.session | ||
|
||
import co.kr.data.network.provider.SessionProvider | ||
import co.kr.data.storage.source.SessionDataSource | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
internal class SessionProviderImpl @Inject constructor( | ||
private val sessionDataSource: SessionDataSource, | ||
) : SessionProvider { | ||
override suspend fun getSessionId(): String = | ||
sessionDataSource.sessionId.firstOrNull() ?: "" | ||
|
||
override suspend fun setSessionId(sessionId: String) = | ||
sessionDataSource.updateSessionId(sessionId) | ||
} |
17 changes: 17 additions & 0 deletions
17
data/session/src/main/java/co/kr/data/session/di/SessionModule.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,17 @@ | ||
package co.kr.data.session.di | ||
|
||
import co.kr.data.network.provider.SessionProvider | ||
import co.kr.data.session.SessionProviderImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
internal abstract class SessionModule { | ||
@Binds | ||
abstract fun bindsSessionProvider( | ||
provider: SessionProviderImpl, | ||
): SessionProvider | ||
} |
7 changes: 7 additions & 0 deletions
7
domain/src/main/java/co/kr/tnt/domain/monitor/SessionMonitor.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,7 @@ | ||
package co.kr.tnt.domain.monitor | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SessionMonitor { | ||
val onExpired: Flow<Unit> | ||
} |
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
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
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
Oops, something went wrong.