Skip to content

Commit

Permalink
chore: refactor interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
alvr committed May 24, 2024
1 parent c6898af commit fbb9e3d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import org.koin.dsl.module
internal expect fun dataStoreModule(): Module

private val repositoriesModule = module {
singleOf(::SessionRepositoryImpl).bind<SessionRepository>()
singleOf(::SessionRepositoryImpl) bind SessionRepository::class
}

private val sourcesModule = module {
singleOf(::SessionLocalSourceImpl).bind<SessionLocalSource>()
singleOf(::SessionLocalSourceImpl) bind SessionLocalSource::class
}

val commonSessionDataModule = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import org.koin.dsl.bind
import org.koin.dsl.module

private val managersModule = module {
singleOf(::UserIdManagerImpl).bind<UserIdManager>()
singleOf(::UserIdManagerImpl) bind UserIdManager::class
}

private val repositoriesModule = module {
singleOf(::UserRepositoryImpl).bind<UserRepository>()
singleOf(::UserRepositoryImpl) bind UserRepository::class
}

private val sourcesModule = module {
singleOf(::UserIdRemoteSourceImpl).bind<UserIdRemoteSource>()
singleOf(::UserInfoRemoteSourceImpl).bind<UserInfoRemoteSource>()
singleOf(::UserIdRemoteSourceImpl) bind UserIdRemoteSource::class
singleOf(::UserInfoRemoteSourceImpl) bind UserInfoRemoteSource::class
}

val commonUserDataModule = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private val aeadModule = module {
}

private val securerModule = module {
factoryOf(::AndroidPreferencesEncrypt).bind<PreferencesEncrypt>()
factoryOf(::AndroidPreferencesEncrypt) bind PreferencesEncrypt::class
}

internal actual fun encryptionModule() = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.koin.dsl.bind
import org.koin.dsl.module

private val securerModule = module {
factoryOf(::IosPreferencesEncrypt).bind<PreferencesEncrypt>()
factoryOf(::IosPreferencesEncrypt) bind PreferencesEncrypt::class
}

internal actual fun encryptionModule() = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.apollographql.apollo3.cache.normalized.FetchPolicy
import com.apollographql.apollo3.cache.normalized.api.CacheKey
import com.apollographql.apollo3.cache.normalized.api.CacheKeyGenerator
import com.apollographql.apollo3.cache.normalized.api.CacheKeyGeneratorContext
import com.apollographql.apollo3.cache.normalized.api.MemoryCacheFactory
import com.apollographql.apollo3.cache.normalized.api.NormalizedCacheFactory
import com.apollographql.apollo3.cache.normalized.fetchPolicy
import com.apollographql.apollo3.cache.normalized.normalizedCache
Expand All @@ -28,9 +29,11 @@ import org.koin.dsl.module

internal expect fun ApolloClient.Builder.sentryInterceptor(): ApolloClient.Builder

private val getAnilistTokenInterceptor = named("getAnilistTokenInterceptor")
private val deleteAnilistTokenInterceptor = named("deleteAnilistTokenInterceptor")
private val loggingInterceptor = named("loggingInterceptor")
private enum class Interceptor {
GET_TOKEN,
DELETE_TOKEN,
LOGGING,
}

private val apolloClientModule = module {
single {
Expand All @@ -50,9 +53,9 @@ private val apolloClientModule = module {

ApolloClient.Builder()
.serverUrl(ANILIST_BASE_URL)
.addHttpInterceptor(get(getAnilistTokenInterceptor))
.addHttpInterceptor(get(deleteAnilistTokenInterceptor))
.addHttpInterceptor(get(loggingInterceptor))
.addHttpInterceptor(get(named(Interceptor.GET_TOKEN)))
.addHttpInterceptor(get(named(Interceptor.DELETE_TOKEN)))
.addHttpInterceptor(get(named(Interceptor.LOGGING)))
.sentryInterceptor()
.fetchPolicy(FetchPolicy.CacheAndNetwork)
.normalizedCache(
Expand All @@ -69,10 +72,10 @@ private val apolloDatabaseModule: Module = module {
}

private val apolloInterceptorsModule = module {
single<HttpInterceptor>(getAnilistTokenInterceptor) {
object : HttpInterceptor {
val useCase = get<GetAnilistTokenUseCase>()
single<HttpInterceptor>(named(Interceptor.GET_TOKEN)) {
val useCase = get<GetAnilistTokenUseCase>()

object : HttpInterceptor {
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain) =
request.newBuilder()
.addHeader("Authorization", "Bearer ${useCase().getOrNull()?.token}")
Expand All @@ -82,10 +85,10 @@ private val apolloInterceptorsModule = module {
}
}

single<HttpInterceptor>(deleteAnilistTokenInterceptor) {
object : HttpInterceptor {
val useCase = get<DeleteAnilistTokenUseCase>()
single<HttpInterceptor>(named(Interceptor.DELETE_TOKEN)) {
val useCase = get<DeleteAnilistTokenUseCase>()

object : HttpInterceptor {
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain) =
chain.proceed(request).also { response ->
if (
Expand All @@ -98,7 +101,7 @@ private val apolloInterceptorsModule = module {
}
}

single<HttpInterceptor>(loggingInterceptor) {
single<HttpInterceptor>(named(Interceptor.LOGGING)) {
LoggingInterceptor(
log = { Logger.i("ApolloLoggingInterceptor") { it } },
level = if (KatanaBuildConfig.DEBUG) {
Expand All @@ -109,7 +112,7 @@ private val apolloInterceptorsModule = module {
)
}

factoryOf(::ReloadInterceptor).bind<ApolloInterceptor>()
factoryOf(::ReloadInterceptor) bind ApolloInterceptor::class
}

val coreRemoteModule = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import arrow.core.Either
import com.apollographql.apollo3.api.Optional
import com.apollographql.apollo3.exception.ApolloHttpException
import com.apollographql.apollo3.exception.ApolloNetworkException
import com.apollographql.apollo3.exception.ApolloParseException
import com.apollographql.apollo3.exception.CacheMissException
import com.apollographql.apollo3.exception.DefaultApolloException
import com.apollographql.apollo3.exception.HttpCacheMissException
Expand All @@ -26,7 +25,6 @@ fun Throwable.toFailure(
is DefaultApolloException -> network
is CacheMissException,
is HttpCacheMissException -> cache
is ApolloParseException,
is JsonDataException,
is JsonEncodingException,
is NoDataException -> response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import org.koin.dsl.bind
import org.koin.dsl.module

private val repositoriesModule = module {
singleOf(::ListsRepositoryImpl).bind<ListsRepository>()
singleOf(::ListsRepositoryImpl) bind ListsRepository::class
}

private val sourcesModule = module {
singleOf(::CommonListsRemoteSourceImpl).bind<CommonListsRemoteSource>()
singleOf(::AnimeListsRemoteSourceImpl).bind<AnimeListsRemoteSource>()
singleOf(::MangaListsRemoteSourceImpl).bind<MangaListsRemoteSource>()
singleOf(::CommonListsRemoteSourceImpl) bind CommonListsRemoteSource::class
singleOf(::AnimeListsRemoteSourceImpl) bind AnimeListsRemoteSource::class
singleOf(::MangaListsRemoteSourceImpl) bind MangaListsRemoteSource::class
}

val featuresListsDataModule = module {
Expand Down

0 comments on commit fbb9e3d

Please sign in to comment.