Skip to content

Commit

Permalink
Network library changes (#542)
Browse files Browse the repository at this point in the history
- Allows for http2
- Caches a global instance of kotlinx-serialization `Json` and configures it with a bit more leniency.
- Moves okhttp's dependency strings to the .toml file
- Switches off logging for release builds
  • Loading branch information
Marco Romano authored Jun 6, 2023
1 parent 7308428 commit 1d6f4e3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 39 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ dependencies {
implementation(libs.coil)

implementation(platform(libs.network.okhttp.bom))
implementation("com.squareup.okhttp3:logging-interceptor")
implementation(libs.network.okhttp.logging)
implementation(libs.serialization.json)

implementation(libs.dagger)
kapt(libs.dagger.compiler)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ squareup_seismic = "com.squareup:seismic:1.0.3"

# network
network_okhttp_bom = "com.squareup.okhttp3:okhttp-bom:4.11.0"
network_okhttp_logging = { module = "com.squareup.okhttp3:logging-interceptor" }
network_okhttp = { module = "com.squareup.okhttp3:okhttp" }
network_retrofit = "com.squareup.retrofit2:retrofit:2.9.0"
network_retrofit_converter_serialization = "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0"

Expand Down
5 changes: 2 additions & 3 deletions libraries/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ dependencies {
implementation(projects.libraries.core)
implementation(projects.libraries.di)
implementation(platform(libs.network.okhttp.bom))
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")

implementation(libs.network.okhttp)
implementation(libs.network.okhttp.logging)
implementation(libs.network.retrofit)
implementation(libs.network.retrofit.converter.serialization)
implementation(libs.serialization.json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,36 @@ import io.element.android.libraries.core.meta.BuildMeta
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.network.interceptors.FormattedJsonHttpLogger
import kotlinx.serialization.json.Json
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit

@Module
@ContributesTo(AppScope::class)
object NetworkModule {

@Provides
@JvmStatic
fun providesHttpLoggingInterceptor(buildMeta: BuildMeta): HttpLoggingInterceptor {
val loggingLevel = if (buildMeta.isDebuggable) {
HttpLoggingInterceptor.Level.BODY
} else {
HttpLoggingInterceptor.Level.BASIC
}
val logger = FormattedJsonHttpLogger(loggingLevel)
val interceptor = HttpLoggingInterceptor(logger)
interceptor.level = loggingLevel
return interceptor
}
@SingleIn(AppScope::class)
fun providesOkHttpClient(
buildMeta: BuildMeta,
): OkHttpClient = OkHttpClient.Builder().apply {
connectTimeout(30, TimeUnit.SECONDS)
readTimeout(60, TimeUnit.SECONDS)
writeTimeout(60, TimeUnit.SECONDS)
if (buildMeta.isDebuggable) addInterceptor(providesHttpLoggingInterceptor())
}.build()

@Provides
@SingleIn(AppScope::class)
fun providesOkHttpClient(
httpLoggingInterceptor: HttpLoggingInterceptor,
): OkHttpClient {
return OkHttpClient.Builder()
// workaround for #4669
.protocols(listOf(Protocol.HTTP_1_1))
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor)
.build()
fun providesJson(): Json = Json {
ignoreUnknownKeys = true
}
}

private fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
val loggingLevel = HttpLoggingInterceptor.Level.BODY
val logger = FormattedJsonHttpLogger(loggingLevel)
val interceptor = HttpLoggingInterceptor(logger)
interceptor.level = loggingLevel
return interceptor
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@
package io.element.android.libraries.network

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import dagger.Lazy
import io.element.android.libraries.core.uri.ensureTrailingSlash
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import javax.inject.Inject
import javax.inject.Provider

class RetrofitFactory @Inject constructor(
private val okHttpClient: Lazy<OkHttpClient>,
private val okHttpClient: Provider<OkHttpClient>,
private val json: Provider<Json>,
) {
fun create(baseUrl: String): Retrofit {
val contentType = "application/json".toMediaType()
return Retrofit.Builder()
.baseUrl(baseUrl.ensureTrailingSlash())
.addConverterFactory(Json.asConverterFactory(contentType))
.callFactory { request -> okHttpClient.get().newCall(request) }
.build()
}
fun create(baseUrl: String): Retrofit = Retrofit.Builder()
.baseUrl(baseUrl.ensureTrailingSlash())
.addConverterFactory(json.get().asConverterFactory("application/json".toMediaType()))
.callFactory(okHttpClient.get())
.build()
}

0 comments on commit 1d6f4e3

Please sign in to comment.