Move Json provider from Network module to AppModule to reuse it.
This commit is contained in:
parent
14c7a63f45
commit
276c707e42
10 changed files with 34 additions and 25 deletions
|
|
@ -15,7 +15,6 @@ import dev.zacsweers.metro.SingleIn
|
|||
import io.element.android.libraries.core.meta.BuildMeta
|
||||
import io.element.android.libraries.network.interceptors.FormattedJsonHttpLogger
|
||||
import io.element.android.libraries.network.interceptors.UserAgentInterceptor
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
|
@ -35,12 +34,6 @@ object NetworkModule {
|
|||
addInterceptor(userAgentInterceptor)
|
||||
if (buildMeta.isDebuggable) addInterceptor(providesHttpLoggingInterceptor())
|
||||
}.build()
|
||||
|
||||
@Provides
|
||||
@SingleIn(AppScope::class)
|
||||
fun providesJson(): Json = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@ import io.element.android.libraries.pushproviders.api.PushData
|
|||
import kotlinx.serialization.json.Json
|
||||
|
||||
@Inject
|
||||
class UnifiedPushParser {
|
||||
private val json by lazy { Json { ignoreUnknownKeys = true } }
|
||||
|
||||
class UnifiedPushParser(
|
||||
private val json: Json,
|
||||
) {
|
||||
fun parse(message: ByteArray, clientSecret: String): PushData? {
|
||||
return tryOrNull { json.decodeFromString<PushDataUnifiedPush>(String(message)) }?.toPushData(clientSecret)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import timber.log.Timber
|
|||
@Inject
|
||||
class DefaultSessionWellknownRetriever(
|
||||
private val matrixClient: MatrixClient,
|
||||
private val parser: Json,
|
||||
private val json: Json,
|
||||
) : SessionWellknownRetriever {
|
||||
private val domain by lazy { matrixClient.userIdServerName() }
|
||||
|
||||
|
|
@ -32,7 +32,7 @@ class DefaultSessionWellknownRetriever(
|
|||
.getUrl(url)
|
||||
.mapCatchingExceptions {
|
||||
val data = String(it)
|
||||
parser.decodeFromString(InternalWellKnown.serializer(), data)
|
||||
json.decodeFromString(InternalWellKnown.serializer(), data)
|
||||
}
|
||||
.onFailure { Timber.e(it, "Failed to retrieve .well-known from $domain") }
|
||||
.map { it.map() }
|
||||
|
|
@ -45,7 +45,7 @@ class DefaultSessionWellknownRetriever(
|
|||
.getUrl(url)
|
||||
.mapCatchingExceptions {
|
||||
val data = String(it)
|
||||
parser.decodeFromString(InternalElementWellKnown.serializer(), data)
|
||||
json.decodeFromString(InternalElementWellKnown.serializer(), data)
|
||||
}
|
||||
.onFailure { Timber.e(it, "Failed to retrieve Element .well-known from $domain") }
|
||||
.map { it.map() }
|
||||
|
|
|
|||
|
|
@ -244,6 +244,6 @@ class DefaultSessionWellknownRetrieverTest {
|
|||
userIdServerNameLambda = { "user.domain.org" },
|
||||
getUrlLambda = getUrlLambda,
|
||||
),
|
||||
parser = Json { ignoreUnknownKeys = true }
|
||||
json = Json { ignoreUnknownKeys = true },
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue