Move network extensions to separate file
This commit is contained in:
@@ -5,16 +5,14 @@ import gq.kirmanak.mealient.data.auth.AuthService
|
|||||||
import gq.kirmanak.mealient.data.auth.impl.AuthenticationError.*
|
import gq.kirmanak.mealient.data.auth.impl.AuthenticationError.*
|
||||||
import gq.kirmanak.mealient.data.impl.ErrorDetail
|
import gq.kirmanak.mealient.data.impl.ErrorDetail
|
||||||
import gq.kirmanak.mealient.data.impl.RetrofitBuilder
|
import gq.kirmanak.mealient.data.impl.RetrofitBuilder
|
||||||
|
import gq.kirmanak.mealient.data.impl.util.decodeErrorBodyOrNull
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.SerializationException
|
import kotlinx.serialization.SerializationException
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.decodeFromStream
|
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
import retrofit2.Response
|
|
||||||
import retrofit2.create
|
import retrofit2.create
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.InputStream
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@ExperimentalSerializationApi
|
@ExperimentalSerializationApi
|
||||||
@@ -22,6 +20,7 @@ class AuthDataSourceImpl @Inject constructor(
|
|||||||
private val retrofitBuilder: RetrofitBuilder,
|
private val retrofitBuilder: RetrofitBuilder,
|
||||||
private val json: Json,
|
private val json: Json,
|
||||||
) : AuthDataSource {
|
) : AuthDataSource {
|
||||||
|
|
||||||
override suspend fun authenticate(
|
override suspend fun authenticate(
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
@@ -54,12 +53,4 @@ class AuthDataSourceImpl @Inject constructor(
|
|||||||
Timber.v("authenticate() returned: $accessToken")
|
Timber.v("authenticate() returned: $accessToken")
|
||||||
return accessToken
|
return accessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified T, reified R> Response<T>.decodeErrorBodyOrNull(): R? =
|
|
||||||
errorBody()?.byteStream()?.let { json.decodeFromStreamOrNull<R>(it) }
|
|
||||||
|
|
||||||
private inline fun <reified T> Json.decodeFromStreamOrNull(stream: InputStream): T? =
|
|
||||||
runCatching { decodeFromStream<T>(stream) }
|
|
||||||
.onFailure { Timber.e(it, "decodeFromStreamOrNull: can't decode") }
|
|
||||||
.getOrNull()
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package gq.kirmanak.mealient.data.impl.util
|
||||||
|
|
||||||
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
|
import retrofit2.Response
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
@ExperimentalSerializationApi
|
||||||
|
inline fun <T, reified R> Response<T>.decodeErrorBodyOrNull(json: Json): R? =
|
||||||
|
errorBody()?.byteStream()?.let { json.decodeFromStreamOrNull<R>(it) }
|
||||||
|
|
||||||
|
@ExperimentalSerializationApi
|
||||||
|
inline fun <reified T> Json.decodeFromStreamOrNull(stream: InputStream): T? =
|
||||||
|
runCatching { decodeFromStream<T>(stream) }
|
||||||
|
.onFailure { Timber.e(it, "decodeFromStreamOrNull: can't decode") }
|
||||||
|
.getOrNull()
|
||||||
|
|
||||||
Reference in New Issue
Block a user