Create network module
This commit is contained in:
@@ -1,25 +1,7 @@
|
||||
package gq.kirmanak.mealient.extensions
|
||||
|
||||
import gq.kirmanak.mealient.data.network.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.mapToNetworkError
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.decodeFromStream
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
inline fun <T, reified R> Json.decodeErrorBody(response: Response<T>): R =
|
||||
checkNotNull(response.errorBody()) { "Can't decode absent error body" }
|
||||
.byteStream()
|
||||
.let(::decodeFromStream)
|
||||
|
||||
|
||||
fun Throwable.mapToNetworkError(): NetworkError = when (this) {
|
||||
is HttpException, is SerializationException -> NetworkError.NotMealie(this)
|
||||
else -> NetworkError.NoServerConnection(this)
|
||||
}
|
||||
|
||||
inline fun <T> Logger.logAndMapErrors(
|
||||
block: () -> T,
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package gq.kirmanak.mealient.extensions
|
||||
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.baseurl.impl.VersionResponse
|
||||
import gq.kirmanak.mealient.data.recipes.network.response.GetRecipeIngredientResponse
|
||||
import gq.kirmanak.mealient.data.recipes.network.response.GetRecipeInstructionResponse
|
||||
import gq.kirmanak.mealient.data.recipes.network.response.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.data.recipes.network.response.GetRecipeSummaryResponse
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
|
||||
|
||||
fun GetRecipeResponse.toRecipeEntity() = RecipeEntity(
|
||||
remoteId = remoteId,
|
||||
@@ -45,4 +42,26 @@ fun GetRecipeSummaryResponse.recipeEntity() = RecipeSummaryEntity(
|
||||
dateUpdated = dateUpdated,
|
||||
)
|
||||
|
||||
fun VersionResponse.versionInfo() = VersionInfo(production, version, demoStatus)
|
||||
fun VersionResponse.versionInfo() = VersionInfo(production, version, demoStatus)
|
||||
|
||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequest(
|
||||
name = recipeName,
|
||||
description = recipeDescription,
|
||||
recipeYield = recipeYield,
|
||||
recipeIngredient = recipeIngredients.map { AddRecipeIngredient(note = it) },
|
||||
recipeInstructions = recipeInstructions.map { AddRecipeInstruction(text = it) },
|
||||
settings = AddRecipeSettings(
|
||||
public = isRecipePublic,
|
||||
disableComments = areCommentsDisabled,
|
||||
)
|
||||
)
|
||||
|
||||
fun AddRecipeRequest.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||
recipeName = name,
|
||||
recipeDescription = description,
|
||||
recipeYield = recipeYield,
|
||||
recipeInstructions = recipeInstructions.map { it.text },
|
||||
recipeIngredients = recipeIngredient.map { it.note },
|
||||
isRecipePublic = settings.public,
|
||||
areCommentsDisabled = settings.disableComments,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user