Add OkHttp logging interceptor
This commit is contained in:
@@ -61,6 +61,10 @@ dependencies {
|
|||||||
implementation "com.squareup.retrofit2:retrofit:2.9.0"
|
implementation "com.squareup.retrofit2:retrofit:2.9.0"
|
||||||
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
|
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
|
||||||
|
|
||||||
|
def okhttp_version = "4.9.2"
|
||||||
|
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
|
||||||
|
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
|
||||||
|
|
||||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
|
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
|
||||||
|
|
||||||
implementation "androidx.preference:preference-ktx:1.1.1"
|
implementation "androidx.preference:preference-ktx:1.1.1"
|
||||||
|
|||||||
21
app/src/main/java/gq/kirmanak/mealie/data/OkHttpBuilder.kt
Normal file
21
app/src/main/java/gq/kirmanak/mealie/data/OkHttpBuilder.kt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package gq.kirmanak.mealie.data
|
||||||
|
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class OkHttpBuilder @Inject constructor() {
|
||||||
|
fun buildOkHttp(): OkHttpClient {
|
||||||
|
return OkHttpClient.Builder()
|
||||||
|
.addNetworkInterceptor(buildLoggingInterceptor())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildLoggingInterceptor() : Interceptor {
|
||||||
|
val interceptor = HttpLoggingInterceptor { message -> Timber.v(message) }
|
||||||
|
interceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||||
|
return interceptor
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,19 +3,20 @@ package gq.kirmanak.mealie.data
|
|||||||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import okhttp3.MediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@ExperimentalSerializationApi
|
@ExperimentalSerializationApi
|
||||||
class RetrofitBuilder @Inject constructor() {
|
class RetrofitBuilder @Inject constructor(private val okHttpBuilder: OkHttpBuilder) {
|
||||||
fun buildRetrofit(baseUrl: String): Retrofit {
|
fun buildRetrofit(baseUrl: String): Retrofit {
|
||||||
Timber.v("buildRetrofit() called with: baseUrl = $baseUrl")
|
Timber.v("buildRetrofit() called with: baseUrl = $baseUrl")
|
||||||
val url = if (baseUrl.startsWith("http")) baseUrl else "https://$baseUrl"
|
val url = if (baseUrl.startsWith("http")) baseUrl else "https://$baseUrl"
|
||||||
val contentType = "application/json".toMediaType()
|
val contentType = "application/json".toMediaType()
|
||||||
return Retrofit.Builder()
|
return Retrofit.Builder()
|
||||||
.baseUrl(url)
|
.baseUrl(url)
|
||||||
|
.client(okHttpBuilder.buildOkHttp())
|
||||||
.addConverterFactory(Json.asConverterFactory(contentType))
|
.addConverterFactory(Json.asConverterFactory(contentType))
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user