mirror of
https://github.com/bbara04/Purefin.git
synced 2026-07-24 03:36:51 +00:00
refactor: move JellyfinAuthInterceptor and JellyfinNetworkModule to the domain module
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package hu.bbara.purefin.jellyfin
|
||||
|
||||
import hu.bbara.purefin.data.session.UserSessionRepository
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class JellyfinAuthInterceptor @Inject constructor(
|
||||
userSessionRepository: UserSessionRepository
|
||||
) : Interceptor {
|
||||
|
||||
@Volatile
|
||||
private var cachedToken: String = ""
|
||||
|
||||
init {
|
||||
userSessionRepository.accessToken
|
||||
.onEach { cachedToken = it }
|
||||
.launchIn(CoroutineScope(SupervisorJob() + Dispatchers.IO))
|
||||
}
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val token = cachedToken
|
||||
val request = chain.request().newBuilder()
|
||||
.addHeader("X-Emby-Token", token)
|
||||
.build()
|
||||
return chain.proceed(request)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package hu.bbara.purefin.jellyfin
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object JellyfinNetworkModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideOkHttpClient(authInterceptor: JellyfinAuthInterceptor): OkHttpClient {
|
||||
return OkHttpClient.Builder()
|
||||
.addInterceptor(authInterceptor)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user