Hide MealieDataSourceWrapper behind interfaces
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
package gq.kirmanak.mealient.data.add.impl
|
||||
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.extensions.logAndMapErrors
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class AddRecipeDataSourceImpl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
private val mealieDataSourceWrapper: MealieDataSourceWrapper,
|
||||
) : AddRecipeDataSource {
|
||||
|
||||
override suspend fun addRecipe(recipe: AddRecipeRequest): String {
|
||||
logger.v { "addRecipe() called with: recipe = $recipe" }
|
||||
val response = logger.logAndMapErrors(
|
||||
block = { mealieDataSourceWrapper.addRecipe(recipe) },
|
||||
logProvider = { "addRecipe: can't add recipe" }
|
||||
)
|
||||
logger.v { "addRecipe() response = $response" }
|
||||
return response
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package gq.kirmanak.mealient.data.baseurl.impl
|
||||
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||
import gq.kirmanak.mealient.extensions.logAndMapErrors
|
||||
import gq.kirmanak.mealient.extensions.versionInfo
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class VersionDataSourceImpl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
private val mealieDataSourceWrapper: MealieDataSourceWrapper,
|
||||
) : VersionDataSource {
|
||||
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo {
|
||||
logger.v { "getVersionInfo() called with: baseUrl = $baseUrl" }
|
||||
|
||||
val response = logger.logAndMapErrors(
|
||||
block = { mealieDataSourceWrapper.getVersionInfo(baseUrl) },
|
||||
logProvider = { "getVersionInfo: can't request version" }
|
||||
)
|
||||
|
||||
return response.versionInfo()
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,17 @@
|
||||
package gq.kirmanak.mealient.data.network
|
||||
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||
import gq.kirmanak.mealient.datasource.MealieDataSource
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.extensions.toVersionInfo
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -13,34 +20,27 @@ class MealieDataSourceWrapper @Inject constructor(
|
||||
private val baseURLStorage: BaseURLStorage,
|
||||
private val authRepo: AuthRepo,
|
||||
private val mealieDataSource: MealieDataSource,
|
||||
) {
|
||||
) : AddRecipeDataSource, RecipeDataSource, VersionDataSource {
|
||||
|
||||
suspend fun addRecipe(recipe: AddRecipeRequest): String {
|
||||
val baseUrl = baseURLStorage.requireBaseURL()
|
||||
return withAuthHeader { token -> addRecipe(baseUrl, token, recipe) }
|
||||
}
|
||||
override suspend fun addRecipe(recipe: AddRecipeRequest): String =
|
||||
withAuthHeader { token -> addRecipe(getUrl(), token, recipe) }
|
||||
|
||||
suspend fun getVersionInfo(baseUrl: String): VersionResponse {
|
||||
return mealieDataSource.getVersionInfo(baseUrl)
|
||||
}
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo =
|
||||
mealieDataSource.getVersionInfo(baseUrl).toVersionInfo()
|
||||
|
||||
suspend fun requestRecipes(
|
||||
start: Int = 0,
|
||||
limit: Int = 9999,
|
||||
): List<GetRecipeSummaryResponse> {
|
||||
val baseUrl = baseURLStorage.requireBaseURL()
|
||||
return withAuthHeader { token -> requestRecipes(baseUrl, token, start, limit) }
|
||||
}
|
||||
override suspend fun requestRecipes(start: Int, limit: Int): List<GetRecipeSummaryResponse> =
|
||||
withAuthHeader { token -> requestRecipes(getUrl(), token, start, limit) }
|
||||
|
||||
suspend fun requestRecipeInfo(slug: String): GetRecipeResponse {
|
||||
val baseUrl = baseURLStorage.requireBaseURL()
|
||||
return withAuthHeader { token -> requestRecipeInfo(baseUrl, token, slug) }
|
||||
}
|
||||
override suspend fun requestRecipeInfo(slug: String): GetRecipeResponse =
|
||||
withAuthHeader { token -> requestRecipeInfo(getUrl(), token, slug) }
|
||||
|
||||
private suspend fun getUrl() = baseURLStorage.requireBaseURL()
|
||||
|
||||
private suspend inline fun <T> withAuthHeader(block: MealieDataSource.(String?) -> T): T =
|
||||
mealieDataSource.runCatching { block(authRepo.getAuthHeader()) }.getOrElse {
|
||||
if (it is NetworkError.Unauthorized) {
|
||||
authRepo.invalidateAuthHeader()
|
||||
// Trying again with new authentication header
|
||||
mealieDataSource.block(authRepo.getAuthHeader())
|
||||
} else {
|
||||
throw it
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package gq.kirmanak.mealient.data.recipes.network
|
||||
|
||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class RecipeDataSourceImpl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
private val mealieDataSourceWrapper: MealieDataSourceWrapper,
|
||||
) : RecipeDataSource {
|
||||
|
||||
override suspend fun requestRecipes(start: Int, limit: Int): List<GetRecipeSummaryResponse> {
|
||||
logger.v { "requestRecipes() called with: start = $start, limit = $limit" }
|
||||
val recipeSummary = mealieDataSourceWrapper.requestRecipes(start, limit)
|
||||
logger.v { "requestRecipes() returned: $recipeSummary" }
|
||||
return recipeSummary
|
||||
}
|
||||
|
||||
override suspend fun requestRecipeInfo(slug: String): GetRecipeResponse {
|
||||
logger.v { "requestRecipeInfo() called with: slug = $slug" }
|
||||
val recipeInfo = mealieDataSourceWrapper.requestRecipeInfo(slug)
|
||||
logger.v { "requestRecipeInfo() returned: $recipeInfo" }
|
||||
return recipeInfo
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,8 +6,8 @@ import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||
import gq.kirmanak.mealient.data.add.impl.AddRecipeDataSourceImpl
|
||||
import gq.kirmanak.mealient.data.add.impl.AddRecipeRepoImpl
|
||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorageImpl
|
||||
import javax.inject.Singleton
|
||||
@@ -23,7 +23,7 @@ interface AddRecipeModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindAddRecipeDataSource(addRecipeDataSourceImpl: AddRecipeDataSourceImpl): AddRecipeDataSource
|
||||
fun bindAddRecipeDataSource(mealieDataSourceWrapper: MealieDataSourceWrapper): AddRecipeDataSource
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
|
||||
@@ -7,7 +7,7 @@ import dagger.hilt.components.SingletonComponent
|
||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||
import gq.kirmanak.mealient.data.baseurl.impl.BaseURLStorageImpl
|
||||
import gq.kirmanak.mealient.data.baseurl.impl.VersionDataSourceImpl
|
||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@@ -16,7 +16,7 @@ interface BaseURLModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindVersionDataSource(versionDataSourceImpl: VersionDataSourceImpl): VersionDataSource
|
||||
fun bindVersionDataSource(mealieDataSourceWrapper: MealieDataSourceWrapper): VersionDataSource
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
|
||||
@@ -9,6 +9,7 @@ import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealient.data.recipes.db.RecipeStorage
|
||||
import gq.kirmanak.mealient.data.recipes.db.RecipeStorageImpl
|
||||
@@ -16,7 +17,6 @@ import gq.kirmanak.mealient.data.recipes.impl.RecipeImageUrlProvider
|
||||
import gq.kirmanak.mealient.data.recipes.impl.RecipeImageUrlProviderImpl
|
||||
import gq.kirmanak.mealient.data.recipes.impl.RecipeRepoImpl
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSourceImpl
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.ui.recipes.images.RecipeModelLoaderFactory
|
||||
import java.io.InputStream
|
||||
@@ -28,7 +28,7 @@ interface RecipeModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun provideRecipeDataSource(recipeDataSourceImpl: RecipeDataSourceImpl): RecipeDataSource
|
||||
fun provideRecipeDataSource(mealieDataSourceWrapper: MealieDataSourceWrapper): RecipeDataSource
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
|
||||
@@ -42,7 +42,7 @@ fun GetRecipeSummaryResponse.recipeEntity() = RecipeSummaryEntity(
|
||||
dateUpdated = dateUpdated,
|
||||
)
|
||||
|
||||
fun VersionResponse.versionInfo() = VersionInfo(production, version, demoStatus)
|
||||
fun VersionResponse.toVersionInfo() = VersionInfo(production, version, demoStatus)
|
||||
|
||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequest(
|
||||
name = recipeName,
|
||||
|
||||
Reference in New Issue
Block a user