Skip to content

Commit

Permalink
[feat] #7 api, interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook123 committed May 3, 2024
1 parent 96a38b0 commit 68ce214
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/network/src/main/java/org/sopt/network/di/ApiModule.kt
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)
}
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()
}

0 comments on commit 68ce214

Please sign in to comment.