All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reown.foundation.di.FoundationNetworkModule.kt Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.reown.foundation.di

import com.tinder.scarlet.Scarlet
import com.tinder.scarlet.messageadapter.moshi.MoshiMessageAdapter
import com.tinder.scarlet.retry.LinearBackoffStrategy
import com.tinder.scarlet.websocket.okhttp.newWebSocketFactory
import com.reown.foundation.network.BaseRelayClient
import com.reown.foundation.network.data.adapter.FlowStreamAdapter
import com.reown.foundation.network.data.service.RelayService
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import org.koin.dsl.module
import java.util.concurrent.TimeUnit

fun networkModule(serverUrl: String, sdkVersion: String, jwt: String): Module = module {
    val DEFAULT_BACKOFF_SECONDS = 5L
    val TIMEOUT_TIME = 40000L

    // TODO: Setup env variable for tag instead of relayTest. Use env variable here instead of hard coded version
    single(named(FoundationDITags.INTERCEPTOR)) {
        Interceptor {
            val updatedRequest = it.request().newBuilder()
                .addHeader("User-Agent", "wc-2/kotlin-$sdkVersion")
                .build()

            it.proceed(updatedRequest)
        }
    }

    single(named(FoundationDITags.OK_HTTP)) {
        OkHttpClient.Builder()
            .addInterceptor(get(named(FoundationDITags.INTERCEPTOR)))
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .writeTimeout(TIMEOUT_TIME, TimeUnit.MILLISECONDS)
            .readTimeout(TIMEOUT_TIME, TimeUnit.MILLISECONDS)
            .callTimeout(TIMEOUT_TIME, TimeUnit.MILLISECONDS)
            .connectTimeout(TIMEOUT_TIME, TimeUnit.MILLISECONDS)
            .build()
    }

    single(named(FoundationDITags.MSG_ADAPTER)) { MoshiMessageAdapter.Factory(get(named(FoundationDITags.MOSHI))) }

    single { FlowStreamAdapter.Factory() }

    single { LinearBackoffStrategy(TimeUnit.SECONDS.toMillis(DEFAULT_BACKOFF_SECONDS)) }

    single(named(FoundationDITags.SCARLET)) {
        Scarlet.Builder()
            .backoffStrategy(get())
            .webSocketFactory(get(named(FoundationDITags.OK_HTTP)).newWebSocketFactory("$serverUrl&auth=$jwt"))
            .addMessageAdapterFactory(get(named(FoundationDITags.MSG_ADAPTER)))
            .addStreamAdapterFactory(get())
            .build()
    }

    single(named(FoundationDITags.RELAY_SERVICE)) { get(named(FoundationDITags.SCARLET)).create(RelayService::class.java) }

    single { object : BaseRelayClient() {} }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy