Add interceptors to hebe too

This commit is contained in:
Mikołaj Pich 2023-04-16 21:27:31 +02:00
parent d228df6c6d
commit 57660cc7c9
3 changed files with 20 additions and 6 deletions

View file

@ -47,7 +47,11 @@ class Hebe {
resettableManager.reset()
}
private val interceptors: MutableList<Pair<Interceptor, Boolean>> = mutableListOf()
private val appInterceptors: MutableList<Pair<Interceptor, Boolean>> = mutableListOf()
fun addInterceptor(interceptor: Interceptor, network: Boolean = false) {
appInterceptors.add(interceptor to network)
}
private val serviceManager by resettableLazy(resettableManager) {
RepositoryManager(
@ -55,8 +59,11 @@ class Hebe {
keyId = keyId,
privatePem = privatePem,
deviceModel = deviceModel,
interceptors = interceptors,
)
).apply {
appInterceptors.forEach { (interceptor, isNetwork) ->
setInterceptor(interceptor, isNetwork)
}
}
}
private val routes by resettableLazy(resettableManager) { serviceManager.getRoutesRepository() }

View file

@ -18,9 +18,17 @@ internal class RepositoryManager(
private val keyId: String,
private val privatePem: String,
private val deviceModel: String,
private val interceptors: MutableList<Pair<Interceptor, Boolean>>,
) {
private val interceptors: MutableList<Pair<Interceptor, Boolean>> = mutableListOf(
HttpLoggingInterceptor().setLevel(logLevel) to true,
ErrorInterceptor() to false,
)
fun setInterceptor(interceptor: Interceptor, network: Boolean = false) {
interceptors.add(0, interceptor to network)
}
@OptIn(ExperimentalSerializationApi::class)
private val json by lazy {
Json {
@ -62,7 +70,6 @@ internal class RepositoryManager(
}
.client(
OkHttpClient().newBuilder()
.addInterceptor(ErrorInterceptor())
.apply {
if (signInterceptor) {
addInterceptor(SignInterceptor(keyId, privatePem, deviceModel))

View file

@ -232,7 +232,7 @@ class Sdk {
fun addInterceptor(interceptor: Interceptor, network: Boolean = false) {
scrapper.addInterceptor(interceptor, network)
// hebe.setInterceptor(interceptor, network)
hebe.addInterceptor(interceptor, network)
interceptors.add(interceptor to network)
}