generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
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
96a38b0
commit 68ce214
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
core/network/src/main/java/org/sopt/network/di/ApiModule.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,24 @@ | ||
package org.sopt.network.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.network.api.AuthApi | ||
import org.sopt.network.api.ReqresApi | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ApiModule { | ||
@Provides | ||
@Singleton | ||
fun provideAuthService(@Auth retrofit: Retrofit): AuthApi = | ||
retrofit.create(AuthApi::class.java) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideReqresService(@Reqres retrofit: Retrofit): ReqresApi = | ||
retrofit.create(ReqresApi::class.java) | ||
} |
26 changes: 26 additions & 0 deletions
26
core/network/src/main/java/org/sopt/network/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.network.interceptor | ||
|
||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import org.sopt.datastore.source.UserPreferencesDataSource | ||
import javax.inject.Inject | ||
|
||
class AuthInterceptor @Inject constructor( | ||
private val userPreferencesDataSource: UserPreferencesDataSource, | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val authResponse = chain.proceed(handleRequest(chain.request())) | ||
val memberId = authResponse.header("Location")?.toInt() | ||
runBlocking { if (memberId != null) userPreferencesDataSource.setMemberId(memberId) } | ||
|
||
return authResponse | ||
} | ||
|
||
private fun handleRequest(originalRequest: Request) = | ||
originalRequest.newBuilder().addHeader("memberId", | ||
runBlocking { userPreferencesDataSource.userData.first().memberId.toString() } | ||
).addHeader("Accept-Encoding", "identity").build() | ||
} |