-
Notifications
You must be signed in to change notification settings - Fork 1
[NDGL-17] 네트워크 구조 설정 #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e9ff104
[NDGL-17] chore: 네트워크 레이어를 위한 빌드 설정 및 의존성 추가
mj010504 a3e36df
[NDGL-17] feat: 공통 모델 및 유틸리티 추가
mj010504 8948a5a
[NDGL-17] feat: 네트워크 모듈 및 네트워크 미들웨어 추가
mj010504 856aba5
[NDGL-17] feat: DataStore 기반 로컬 인증 데이터 저장소 구현
mj010504 9535b62
[NDGL-17] chore: auth 관련 모델 추가
mj010504 7f4cce0
[NDGL-17] chore: dataPlugin에 timber 의존성 추가 및 adapter에 로깅 추가
mj010504 d530bbd
[NDGL-17] chore: Set up google-services.json 추가
mj010504 e40753d
[NDGL-17] refactor: BaseResponse getData() null 처리 개선
mj010504 dbe9bb2
[NDGL-17] refactor: Authenticator 토큰 갱신 로직 개선
mj010504 e0edb7c
[NDGL-17] chore: buildConfigField 방식 수정
mj010504 daaecf1
[NDGL-17] refactor: 네트워크 구조 수정
mj010504 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,17 +1,33 @@ | ||
| import convention.configureCoroutineAndroid | ||
| import convention.configureHiltAndroid | ||
| import convention.configureKotlinAndroid | ||
| import convention.configureTimber | ||
| import org.gradle.api.Plugin | ||
| import org.gradle.api.Project | ||
| import org.gradle.kotlin.dsl.dependencies | ||
| import util.libs | ||
|
|
||
| class NDGLDataPlugin : Plugin<Project> { | ||
| override fun apply(target: Project): Unit = with(target) { | ||
| with(pluginManager) { | ||
| apply("com.android.library") | ||
| apply("org.jetbrains.kotlin.plugin.serialization") | ||
| } | ||
|
|
||
| configureKotlinAndroid() | ||
| configureHiltAndroid() | ||
| configureCoroutineAndroid() | ||
| configureTimber() | ||
|
|
||
| if (path != ":data:core") { | ||
| dependencies.add("implementation", project(":data:core")) | ||
| } | ||
|
|
||
| dependencies { | ||
| "implementation"(libs.findLibrary("retrofit").get()) | ||
| "implementation"(libs.findLibrary("retrofit-kotlinx-serialization-json").get()) | ||
| "implementation"(libs.findLibrary("kotlinx-serialization-json").get()) | ||
| "implementation"(libs.findLibrary("okhttp").get()) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
core/util/src/main/java/com/yapp/ndgl/core/util/ResultUtil.kt
This file contains hidden or 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,13 @@ | ||
| package com.yapp.ndgl.core.util | ||
|
|
||
| import kotlin.coroutines.cancellation.CancellationException | ||
|
|
||
| suspend inline fun <T, R> T.suspendRunCatching(crossinline block: suspend T.() -> R): Result<R> { | ||
| return try { | ||
| Result.success(block()) | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (t: Throwable) { | ||
| Result.failure(t) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,5 +7,5 @@ android { | |
| } | ||
|
|
||
| dependencies { | ||
| implementation(project(":data:core")) | ||
| implementation(libs.androidx.datastore) | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
data/auth/src/main/java/com/yapp/ndgl/data/auth/api/AuthApi.kt
This file contains hidden or 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,20 @@ | ||
| package com.yapp.ndgl.data.auth.api | ||
|
|
||
| import com.yapp.ndgl.data.auth.model.AuthResponse | ||
| import com.yapp.ndgl.data.auth.model.CreateUserRequest | ||
| import com.yapp.ndgl.data.auth.model.LoginRequest | ||
| import com.yapp.ndgl.data.core.model.BaseResponse | ||
| import retrofit2.http.Body | ||
| import retrofit2.http.POST | ||
|
|
||
| interface AuthApi { | ||
| @POST("/api/v1/auth/users") | ||
| suspend fun createUser( | ||
| @Body request: CreateUserRequest, | ||
| ): BaseResponse<AuthResponse> | ||
|
|
||
| @POST("/api/v1/auth/login") | ||
| suspend fun login( | ||
| @Body request: LoginRequest, | ||
| ): BaseResponse<AuthResponse> | ||
| } |
19 changes: 19 additions & 0 deletions
19
data/auth/src/main/java/com/yapp/ndgl/data/auth/di/AuthModule.kt
This file contains hidden or 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,19 @@ | ||
| package com.yapp.ndgl.data.auth.di | ||
|
|
||
| import com.yapp.ndgl.data.auth.token.TokenManagerImpl | ||
| import com.yapp.ndgl.data.core.token.TokenManager | ||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class AuthModule { | ||
| @Binds | ||
| @Singleton | ||
| abstract fun bindTokenManager( | ||
| impl: TokenManagerImpl, | ||
| ): TokenManager | ||
| } |
34 changes: 34 additions & 0 deletions
34
data/auth/src/main/java/com/yapp/ndgl/data/auth/di/AuthNetworkModule.kt
This file contains hidden or 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,34 @@ | ||
| package com.yapp.ndgl.data.auth.di | ||
|
|
||
| import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
| import com.yapp.ndgl.data.auth.api.AuthApi | ||
| import com.yapp.ndgl.data.core.adapter.NDGLCallAdapterFactory | ||
| import com.yapp.ndgl.data.core.di.AuthClient | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import kotlinx.serialization.json.Json | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import retrofit2.Retrofit | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object AuthNetworkModule { | ||
| @Provides | ||
| @Singleton | ||
| fun provideAuthApi( | ||
| json: Json, | ||
| baseUrl: String, | ||
| @AuthClient okHttpClient: OkHttpClient, | ||
| callAdapterFactory: NDGLCallAdapterFactory, | ||
| ): AuthApi = Retrofit.Builder() | ||
| .baseUrl(baseUrl) | ||
| .client(okHttpClient) | ||
| .addConverterFactory(json.asConverterFactory("application/json".toMediaType())) | ||
| .addCallAdapterFactory(callAdapterFactory) | ||
| .build() | ||
| .create(AuthApi::class.java) | ||
| } |
57 changes: 57 additions & 0 deletions
57
data/auth/src/main/java/com/yapp/ndgl/data/auth/local/LocalAuthDataSource.kt
This file contains hidden or 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,57 @@ | ||
| package com.yapp.ndgl.data.auth.local | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.edit | ||
| import androidx.datastore.preferences.core.stringPreferencesKey | ||
| import com.yapp.ndgl.data.auth.local.util.handleException | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.map | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class LocalAuthDataSource @Inject constructor( | ||
| private val dataStore: DataStore<Preferences>, | ||
| ) { | ||
| private val accessToken: Flow<String> = dataStore.data | ||
| .handleException() | ||
| .map { preferences -> | ||
| preferences[ACCESS_TOKEN_KEY] ?: "" | ||
| } | ||
|
|
||
| private val uuid: Flow<String> = dataStore.data | ||
| .handleException() | ||
| .map { preferences -> | ||
| preferences[UUID_KEY] ?: "" | ||
| } | ||
|
|
||
| suspend fun getAccessToken(): String = accessToken.first() | ||
|
|
||
| suspend fun getUuid(): String = uuid.first() | ||
|
|
||
| suspend fun setAccessToken(token: String) { | ||
| dataStore.edit { preferences -> | ||
| preferences[ACCESS_TOKEN_KEY] = token | ||
| } | ||
| } | ||
|
|
||
| suspend fun setUuid(uuid: String) { | ||
| dataStore.edit { preferences -> | ||
| preferences[UUID_KEY] = uuid | ||
| } | ||
|
mj010504 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| suspend fun clearSession() { | ||
| dataStore.edit { preferences -> | ||
| preferences.remove(ACCESS_TOKEN_KEY) | ||
| preferences.remove(UUID_KEY) | ||
| } | ||
| } | ||
|
|
||
| private companion object { | ||
| private val ACCESS_TOKEN_KEY = stringPreferencesKey("access_token") | ||
| private val UUID_KEY = stringPreferencesKey("uuid") | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
data/auth/src/main/java/com/yapp/ndgl/data/auth/local/di/DataStoreModule.kt
This file contains hidden or 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,28 @@ | ||
| package com.yapp.ndgl.data.auth.local.di | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.preferencesDataStore | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object DataStoreModule { | ||
|
|
||
| private const val TOKEN_PREFERENCES = "token_preferences" | ||
| private val Context.tokenDataStore: DataStore<Preferences> by preferencesDataStore(name = TOKEN_PREFERENCES) | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideTokenDataStore( | ||
| @ApplicationContext context: Context, | ||
| ): DataStore<Preferences> { | ||
| return context.tokenDataStore | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
data/auth/src/main/java/com/yapp/ndgl/data/auth/local/util/DataStoreUtil.kt
This file contains hidden or 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,16 @@ | ||
| package com.yapp.ndgl.data.auth.local.util | ||
|
|
||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.emptyPreferences | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.catch | ||
| import java.io.IOException | ||
|
|
||
| internal fun Flow<Preferences>.handleException(): Flow<Preferences> = | ||
| this.catch { exception -> | ||
| if (exception is IOException) { | ||
| emit(emptyPreferences()) | ||
| } else { | ||
| throw exception | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
data/auth/src/main/java/com/yapp/ndgl/data/auth/model/AuthResponse.kt
|
jihee-dev marked this conversation as resolved.
|
This file contains hidden or 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,10 @@ | ||
| package com.yapp.ndgl.data.auth.model | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class AuthResponse( | ||
| val uuid: String, | ||
| val accessToken: String, | ||
| val nickname: String, | ||
| ) |
12 changes: 12 additions & 0 deletions
12
data/auth/src/main/java/com/yapp/ndgl/data/auth/model/CreateUserRequest.kt
This file contains hidden or 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,12 @@ | ||
| package com.yapp.ndgl.data.auth.model | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class CreateUserRequest( | ||
| val fcmToken: String, | ||
| val deviceModel: String, | ||
| val deviceOs: String, | ||
| val deviceOsVersion: String, | ||
| val appVersion: String, | ||
| ) |
8 changes: 8 additions & 0 deletions
8
data/auth/src/main/java/com/yapp/ndgl/data/auth/model/LoginRequest.kt
This file contains hidden or 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 com.yapp.ndgl.data.auth.model | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class LoginRequest( | ||
| val uuid: String, | ||
| ) |
43 changes: 43 additions & 0 deletions
43
data/auth/src/main/java/com/yapp/ndgl/data/auth/token/TokenManagerImpl.kt
This file contains hidden or 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,43 @@ | ||
| package com.yapp.ndgl.data.auth.token | ||
|
|
||
| import com.yapp.ndgl.data.auth.api.AuthApi | ||
| import com.yapp.ndgl.data.auth.local.LocalAuthDataSource | ||
| import com.yapp.ndgl.data.auth.model.LoginRequest | ||
| import com.yapp.ndgl.data.core.model.getData | ||
| import com.yapp.ndgl.data.core.token.TokenManager | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class TokenManagerImpl @Inject constructor( | ||
| private val localAuthDataSource: LocalAuthDataSource, | ||
| private val authApi: AuthApi, | ||
| ) : TokenManager { | ||
|
|
||
| override suspend fun getAccessToken(): String { | ||
| return localAuthDataSource.getAccessToken() | ||
| } | ||
|
|
||
| override suspend fun getUuid(): String { | ||
| return localAuthDataSource.getUuid() | ||
| } | ||
|
|
||
| override suspend fun setAccessToken(accessToken: String) { | ||
| localAuthDataSource.setAccessToken(accessToken) | ||
| } | ||
|
|
||
| override suspend fun setUuid(uuid: String) { | ||
| localAuthDataSource.setUuid(uuid) | ||
| } | ||
|
|
||
| override suspend fun refreshToken(): String { | ||
| val uuid = getUuid() | ||
| check(uuid.isNotEmpty()) { "UUID is empty" } | ||
|
|
||
| val response = authApi.login(LoginRequest(uuid)).getData() | ||
| setAccessToken(response.accessToken) | ||
| setUuid(response.uuid) | ||
|
|
||
| return response.accessToken | ||
| } | ||
|
mj010504 marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.