-
Notifications
You must be signed in to change notification settings - Fork 1
[Feature/#4] network module 세팅 #16
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 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a61681e
Feat: BaseResponse 정의
wjdrjs00 81478a0
Feat: AuthInterceptor 구현
wjdrjs00 f4e3ffd
Feat: TokenAuthenticator 구현
wjdrjs00 6ac11f1
Feat: BuildConfig에 BASE_URL 추가
wjdrjs00 ef1ff6e
Feat: network DI 모듈 추가
wjdrjs00 89d5e4c
Fix: HttpLoggingInterceptor 반환 타입 수정
wjdrjs00 06c9b41
Refactor: url 값이 없을 경우 GradleException 예외처리 추가
wjdrjs00 0e1141c
Chore: 환경 변수에서 URL을 읽도록 설정
wjdrjs00 35fd177
Chore: workflow에 서버 url 추가
wjdrjs00 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
110 changes: 110 additions & 0 deletions
110
app/src/main/java/com/threegap/bitnagil/di/NetworkModule.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,110 @@ | ||
| package com.threegap.bitnagil.di | ||
|
|
||
| import com.threegap.bitnagil.BuildConfig | ||
| import com.threegap.bitnagil.network.auth.AuthInterceptor | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import kotlinx.serialization.json.Json | ||
| import okhttp3.Interceptor | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.logging.HttpLoggingInterceptor | ||
| import retrofit2.Converter | ||
| import retrofit2.Retrofit | ||
| import retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
| import java.util.concurrent.TimeUnit | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object NetworkModule { | ||
| private const val APPLICATION_JSON = "application/json" | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideBaseUrl(): String = BuildConfig.BASE_URL | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideJson(): Json = | ||
| Json { | ||
| ignoreUnknownKeys = true | ||
| prettyPrint = true | ||
| explicitNulls = false | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideJsonConverter(json: Json): Converter.Factory = | ||
| json.asConverterFactory(APPLICATION_JSON.toMediaType()) | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = | ||
| HttpLoggingInterceptor().apply { | ||
| level = if (BuildConfig.DEBUG) { | ||
| HttpLoggingInterceptor.Level.BODY | ||
| } else { | ||
| HttpLoggingInterceptor.Level.NONE | ||
| } | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Auth | ||
| fun provideAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor = authInterceptor | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Auth | ||
| fun provideAuthOkHttpClient( | ||
| httpLoggingInterceptor: HttpLoggingInterceptor, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| authInterceptor: Interceptor, | ||
| ): OkHttpClient = OkHttpClient.Builder() | ||
| .addInterceptor(authInterceptor) | ||
| .addInterceptor(httpLoggingInterceptor) | ||
| .connectTimeout(10L, TimeUnit.SECONDS) | ||
| .writeTimeout(30L, TimeUnit.SECONDS) | ||
| .readTimeout(30L, TimeUnit.SECONDS) | ||
| .build() | ||
|
wjdrjs00 marked this conversation as resolved.
|
||
|
|
||
| @Provides | ||
| @Singleton | ||
| @NoneAuth | ||
| fun provideNoneAuthOkHttpClient( | ||
| httpLoggingInterceptor: HttpLoggingInterceptor, | ||
| ): OkHttpClient = OkHttpClient.Builder() | ||
| .addInterceptor(httpLoggingInterceptor) | ||
| .connectTimeout(10L, TimeUnit.SECONDS) | ||
| .writeTimeout(30L, TimeUnit.SECONDS) | ||
| .readTimeout(30L, TimeUnit.SECONDS) | ||
| .build() | ||
|
wjdrjs00 marked this conversation as resolved.
|
||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Auth | ||
| fun provideAuthRetrofit( | ||
| baseUrl: String, | ||
| converterFactory: Converter.Factory, | ||
| @Auth okHttpClient: OkHttpClient, | ||
| ): Retrofit = Retrofit.Builder() | ||
| .baseUrl(baseUrl) | ||
| .addConverterFactory(converterFactory) | ||
| .client(okHttpClient) | ||
| .build() | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @NoneAuth | ||
| fun provideNoneAuthRetrofit( | ||
| baseUrl: String, | ||
| converterFactory: Converter.Factory, | ||
| @NoneAuth okHttpClient: OkHttpClient, | ||
| ): Retrofit = Retrofit.Builder() | ||
| .baseUrl(baseUrl) | ||
| .addConverterFactory(converterFactory) | ||
| .client(okHttpClient) | ||
| .build() | ||
| } | ||
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,11 @@ | ||
| package com.threegap.bitnagil.di | ||
|
|
||
| import javax.inject.Qualifier | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.BINARY) | ||
| annotation class NoneAuth | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.BINARY) | ||
| annotation class Auth |
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
Empty file.
31 changes: 31 additions & 0 deletions
31
core/network/src/main/java/com/threegap/bitnagil/network/auth/AuthInterceptor.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,31 @@ | ||
| package com.threegap.bitnagil.network.auth | ||
|
|
||
| import com.threegap.bitnagil.datastore.storage.AuthTokenDataStore | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.runBlocking | ||
| import okhttp3.Interceptor | ||
| import okhttp3.Response | ||
| import javax.inject.Inject | ||
|
|
||
| class AuthInterceptor @Inject constructor( | ||
| private val dataStore: AuthTokenDataStore, | ||
| ) : Interceptor { | ||
| override fun intercept(chain: Interceptor.Chain): Response { | ||
| val originalRequest = chain.request() | ||
| val token = runBlocking { dataStore.tokenFlow.first().accessToken } | ||
|
l5x5l marked this conversation as resolved.
|
||
| if (token.isNullOrBlank()) { | ||
| return chain.proceed(originalRequest) | ||
| } | ||
|
|
||
| val newRequest = originalRequest.newBuilder() | ||
| .addHeader(HEADER_AUTHORIZATION, "$TOKEN_PREFIX $token") | ||
| .build() | ||
|
|
||
| return chain.proceed(newRequest) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val HEADER_AUTHORIZATION = "Authorization" | ||
| private const val TOKEN_PREFIX = "Bearer " | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
core/network/src/main/java/com/threegap/bitnagil/network/auth/TokenAuthenticator.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,46 @@ | ||
| package com.threegap.bitnagil.network.auth | ||
|
|
||
| import com.threegap.bitnagil.datastore.storage.AuthTokenDataStore | ||
| import kotlinx.coroutines.flow.first | ||
| 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 dataStore: AuthTokenDataStore, | ||
| ) : Authenticator { | ||
|
|
||
| override fun authenticate(route: Route?, response: Response): Request? { | ||
| if (response.code != UNAUTHORIZED) return null | ||
| if (responseCount(response) >= MAX_RETRY) return null | ||
|
|
||
| // 재발급 api 연결 시 수정 예정입니다.(현재 코드는 임시) | ||
| val newAccessToken = runBlocking { | ||
| runCatching { dataStore.tokenFlow.first().refreshToken }.getOrNull() | ||
| } ?: return null | ||
|
wjdrjs00 marked this conversation as resolved.
|
||
|
|
||
| return response.request.newBuilder() | ||
| .header(AUTHORIZATION, "$BEARER $newAccessToken") | ||
| .build() | ||
| } | ||
|
|
||
| private fun responseCount(response: Response): Int { | ||
| var count = 1 | ||
| var prior = response.priorResponse | ||
| while (prior != null) { | ||
| count++ | ||
| prior = prior.priorResponse | ||
| } | ||
| return count | ||
| } | ||
|
|
||
| companion object { | ||
| private const val UNAUTHORIZED = 401 | ||
| private const val MAX_RETRY = 2 | ||
| private const val AUTHORIZATION = "Authorization" | ||
| private const val BEARER = "Bearer" | ||
| } | ||
| } | ||
|
wjdrjs00 marked this conversation as resolved.
|
||
14 changes: 14 additions & 0 deletions
14
core/network/src/main/java/com/threegap/bitnagil/network/model/BaseResponse.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,14 @@ | ||
| package com.threegap.bitnagil.network.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class BaseResponse<T>( | ||
| @SerialName("code") | ||
| val code: String, | ||
| @SerialName("message") | ||
| val message: String, | ||
| @SerialName("data") | ||
| val data: T? = null, | ||
| ) |
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.