-
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.
- Loading branch information
1 parent
beb488e
commit 4e05bc6
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.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,26 @@ | ||
package org.sopt.dateroad.data.dataremote.interceptor | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import javax.inject.Inject | ||
|
||
class AuthInterceptor @Inject constructor() : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
val authRequest = originalRequest.newBuilder().addHeader(AUTHORIZATION, "").build() | ||
val response = chain.proceed(authRequest) | ||
|
||
when (response.code) { | ||
CODE_TOKEN_EXPIRE -> { | ||
// TODO 토큰 재발급 api 연동 | ||
} | ||
} | ||
return response | ||
} | ||
|
||
companion object { | ||
const val CODE_TOKEN_EXPIRE = 401 | ||
const val AUTHORIZATION = "Authorization" | ||
const val BEARER = "Bearer " | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/org/sopt/dateroad/di/DataSourceModule.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,10 @@ | ||
package org.sopt.dateroad.di | ||
|
||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class DataSourceModule { | ||
} |
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,78 @@ | ||
package org.sopt.dateroad.di | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.dateroad.BuildConfig | ||
import org.sopt.dateroad.BuildConfig.DEBUG | ||
import org.sopt.dateroad.data.dataremote.interceptor.AuthInterceptor | ||
import org.sopt.dateroad.di.qualifier.Auth | ||
import org.sopt.dateroad.di.qualifier.DateRoad | ||
import retrofit2.Retrofit | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
@OptIn(ExperimentalSerializationApi::class) | ||
@Provides | ||
@Singleton | ||
fun providesJson(): Json = | ||
Json { | ||
isLenient = true | ||
prettyPrint = true | ||
explicitNulls = false | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun providesOkHttpClient( | ||
loggingInterceptor: HttpLoggingInterceptor, | ||
@Auth authInterceptor: Interceptor, | ||
): OkHttpClient = | ||
OkHttpClient.Builder().apply { | ||
connectTimeout(10, TimeUnit.SECONDS) | ||
writeTimeout(10, TimeUnit.SECONDS) | ||
readTimeout(10, TimeUnit.SECONDS) | ||
addInterceptor(authInterceptor) | ||
if (DEBUG) addInterceptor(loggingInterceptor) | ||
}.build() | ||
|
||
@Provides | ||
@Singleton | ||
fun providesLoggingInterceptor(): HttpLoggingInterceptor = | ||
HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
@Auth | ||
fun provideAuthInterceptor(interceptor: AuthInterceptor): Interceptor = interceptor | ||
|
||
@ExperimentalSerializationApi | ||
@Provides | ||
@DateRoad | ||
@Singleton | ||
fun providesPingleRetrofit( | ||
okHttpClient: OkHttpClient, | ||
json: Json, | ||
): Retrofit = | ||
Retrofit.Builder() | ||
.baseUrl(BuildConfig.BASE_URL) | ||
.client(okHttpClient) | ||
.addConverterFactory( | ||
json.asConverterFactory(requireNotNull("application/json".toMediaTypeOrNull())), | ||
) | ||
.build() | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/org/sopt/dateroad/di/RepositoryModule.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,10 @@ | ||
package org.sopt.dateroad.di | ||
|
||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class RepositoryModule { | ||
} |
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,11 @@ | ||
package org.sopt.dateroad.di | ||
|
||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ServiceModule { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/org/sopt/dateroad/di/qualifier/Qualifier.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,9 @@ | ||
package org.sopt.dateroad.di.qualifier | ||
|
||
import javax.inject.Qualifier | ||
|
||
@Qualifier | ||
annotation class DateRoad | ||
|
||
@Qualifier | ||
annotation class Auth |