Move V0 responses to v0 package
This commit is contained in:
@@ -6,6 +6,9 @@ import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0Impl
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieServiceV0
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1Impl
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieServiceV1
|
||||
@@ -52,7 +55,7 @@ interface DataSourceModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideMealieService(retrofit: Retrofit): MealieService =
|
||||
fun provideMealieService(retrofit: Retrofit): MealieServiceV0 =
|
||||
retrofit.create()
|
||||
|
||||
@Provides
|
||||
@@ -71,7 +74,7 @@ interface DataSourceModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindMealieDataSource(mealientDataSourceImpl: MealieDataSourceImpl): MealieDataSource
|
||||
fun bindMealieDataSource(mealientDataSourceImpl: MealieDataSourceV0Impl): MealieDataSourceV0
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource
|
||||
|
||||
sealed class NetworkError(cause: Throwable) : RuntimeException(cause) {
|
||||
class Unauthorized(cause: Throwable) : NetworkError(cause)
|
||||
@@ -1,54 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeRequest(
|
||||
@SerialName("name") val name: String = "",
|
||||
@SerialName("description") val description: String = "",
|
||||
@SerialName("image") val image: String = "",
|
||||
@SerialName("recipeYield") val recipeYield: String = "",
|
||||
@SerialName("recipeIngredient") val recipeIngredient: List<AddRecipeIngredient> = emptyList(),
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<AddRecipeInstruction> = emptyList(),
|
||||
@SerialName("slug") val slug: String = "",
|
||||
@SerialName("filePath") val filePath: String = "",
|
||||
@SerialName("tags") val tags: List<String> = emptyList(),
|
||||
@SerialName("categories") val categories: List<String> = emptyList(),
|
||||
@SerialName("notes") val notes: List<AddRecipeNote> = emptyList(),
|
||||
@SerialName("extras") val extras: Map<String, String> = emptyMap(),
|
||||
@SerialName("assets") val assets: List<String> = emptyList(),
|
||||
@SerialName("settings") val settings: AddRecipeSettings = AddRecipeSettings(),
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeSettings(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("disableComments") val disableComments: Boolean = false,
|
||||
@SerialName("landscapeView") val landscapeView: Boolean = true,
|
||||
@SerialName("public") val public: Boolean = true,
|
||||
@SerialName("showAssets") val showAssets: Boolean = true,
|
||||
@SerialName("showNutrition") val showNutrition: Boolean = true,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeNote(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeInstruction(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeIngredient(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("food") val food: String? = null,
|
||||
@SerialName("note") val note: String = "",
|
||||
@SerialName("quantity") val quantity: Int = 1,
|
||||
@SerialName("title") val title: String? = null,
|
||||
@SerialName("unit") val unit: String? = null,
|
||||
)
|
||||
@@ -1,7 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ErrorDetail(@SerialName("detail") val detail: String? = null)
|
||||
@@ -1,7 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetTokenResponse(@SerialName("access_token") val accessToken: String)
|
||||
@@ -1,14 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class VersionResponse(
|
||||
@SerialName("production")
|
||||
val production: Boolean,
|
||||
@SerialName("version")
|
||||
val version: String,
|
||||
@SerialName("demoStatus")
|
||||
val demoStatus: Boolean,
|
||||
)
|
||||
@@ -1,16 +1,16 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
package gq.kirmanak.mealient.datasource.v0
|
||||
|
||||
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.VersionResponse
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeSummaryResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.VersionResponseV0
|
||||
|
||||
interface MealieDataSource {
|
||||
interface MealieDataSourceV0 {
|
||||
|
||||
suspend fun addRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequest,
|
||||
recipe: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
/**
|
||||
@@ -24,18 +24,18 @@ interface MealieDataSource {
|
||||
|
||||
suspend fun getVersionInfo(
|
||||
baseUrl: String,
|
||||
): VersionResponse
|
||||
): VersionResponseV0
|
||||
|
||||
suspend fun requestRecipes(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
start: Int,
|
||||
limit: Int,
|
||||
): List<GetRecipeSummaryResponse>
|
||||
): List<GetRecipeSummaryResponseV0>
|
||||
|
||||
suspend fun requestRecipeInfo(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
slug: String,
|
||||
): GetRecipeResponse
|
||||
): GetRecipeResponseV0
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
package gq.kirmanak.mealient.datasource.v0
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v0.models.*
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.SerializationException
|
||||
@@ -14,14 +15,14 @@ import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class MealieDataSourceImpl @Inject constructor(
|
||||
class MealieDataSourceV0Impl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
private val mealieService: MealieService,
|
||||
private val mealieServiceV0: MealieServiceV0,
|
||||
private val json: Json,
|
||||
) : MealieDataSource {
|
||||
) : MealieDataSourceV0 {
|
||||
|
||||
override suspend fun addRecipe(
|
||||
baseUrl: String, token: String?, recipe: AddRecipeRequest
|
||||
baseUrl: String, token: String?, recipe: AddRecipeRequestV0
|
||||
): String = makeCall(
|
||||
block = { addRecipe("$baseUrl/api/recipes/create", token, recipe) },
|
||||
logMethod = { "addRecipe" },
|
||||
@@ -36,11 +37,11 @@ class MealieDataSourceImpl @Inject constructor(
|
||||
logParameters = { "baseUrl = $baseUrl, username = $username, password = $password" }
|
||||
).map { it.accessToken }.getOrElse {
|
||||
val errorBody = (it as? HttpException)?.response()?.errorBody() ?: throw it
|
||||
val errorDetail = errorBody.decode<ErrorDetail>()
|
||||
throw if (errorDetail.detail == "Unauthorized") NetworkError.Unauthorized(it) else it
|
||||
val errorDetailV0 = errorBody.decode<ErrorDetailV0>()
|
||||
throw if (errorDetailV0.detail == "Unauthorized") NetworkError.Unauthorized(it) else it
|
||||
}
|
||||
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionResponse = makeCall(
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionResponseV0 = makeCall(
|
||||
block = { getVersion("$baseUrl/api/debug/version") },
|
||||
logMethod = { "getVersionInfo" },
|
||||
logParameters = { "baseUrl = $baseUrl" },
|
||||
@@ -56,7 +57,7 @@ class MealieDataSourceImpl @Inject constructor(
|
||||
|
||||
override suspend fun requestRecipes(
|
||||
baseUrl: String, token: String?, start: Int, limit: Int
|
||||
): List<GetRecipeSummaryResponse> = makeCall(
|
||||
): List<GetRecipeSummaryResponseV0> = makeCall(
|
||||
block = { getRecipeSummary("$baseUrl/api/recipes/summary", token, start, limit) },
|
||||
logMethod = { "requestRecipes" },
|
||||
logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" }
|
||||
@@ -67,19 +68,19 @@ class MealieDataSourceImpl @Inject constructor(
|
||||
|
||||
override suspend fun requestRecipeInfo(
|
||||
baseUrl: String, token: String?, slug: String
|
||||
): GetRecipeResponse = makeCall(
|
||||
): GetRecipeResponseV0 = makeCall(
|
||||
block = { getRecipe("$baseUrl/api/recipes/$slug", token) },
|
||||
logMethod = { "requestRecipeInfo" },
|
||||
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug" }
|
||||
).getOrThrowUnauthorized()
|
||||
|
||||
private suspend inline fun <T> makeCall(
|
||||
crossinline block: suspend MealieService.() -> T,
|
||||
crossinline block: suspend MealieServiceV0.() -> T,
|
||||
crossinline logMethod: () -> String,
|
||||
crossinline logParameters: () -> String,
|
||||
): Result<T> {
|
||||
logger.v { "${logMethod()} called with: ${logParameters()}" }
|
||||
return mealieService.runCatching { block() }
|
||||
return mealieServiceV0.runCatching { block() }
|
||||
.onFailure { logger.e(it) { "${logMethod()} request failed with: ${logParameters()}" } }
|
||||
.onSuccess { logger.d { "${logMethod()} request succeeded with ${logParameters()}" } }
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
package gq.kirmanak.mealient.datasource.v0
|
||||
|
||||
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.v0.models.*
|
||||
import retrofit2.http.*
|
||||
|
||||
interface MealieService {
|
||||
interface MealieServiceV0 {
|
||||
|
||||
@FormUrlEncoded
|
||||
@POST
|
||||
@@ -12,19 +12,19 @@ interface MealieService {
|
||||
@Url url: String,
|
||||
@Field("username") username: String,
|
||||
@Field("password") password: String,
|
||||
): GetTokenResponse
|
||||
): GetTokenResponseV0
|
||||
|
||||
@POST
|
||||
suspend fun addRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Body addRecipeRequest: AddRecipeRequest,
|
||||
@Body addRecipeRequestV0: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
@GET
|
||||
suspend fun getVersion(
|
||||
@Url url: String,
|
||||
): VersionResponse
|
||||
): VersionResponseV0
|
||||
|
||||
@GET
|
||||
suspend fun getRecipeSummary(
|
||||
@@ -32,11 +32,11 @@ interface MealieService {
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Query("start") start: Int,
|
||||
@Query("limit") limit: Int,
|
||||
): List<GetRecipeSummaryResponse>
|
||||
): List<GetRecipeSummaryResponseV0>
|
||||
|
||||
@GET
|
||||
suspend fun getRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
): GetRecipeResponse
|
||||
): GetRecipeResponseV0
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeIngredientV0(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("food") val food: String? = null,
|
||||
@SerialName("note") val note: String = "",
|
||||
@SerialName("quantity") val quantity: Int = 1,
|
||||
@SerialName("title") val title: String? = null,
|
||||
@SerialName("unit") val unit: String? = null,
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeInstructionV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeNoteV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeRequestV0(
|
||||
@SerialName("name") val name: String = "",
|
||||
@SerialName("description") val description: String = "",
|
||||
@SerialName("image") val image: String = "",
|
||||
@SerialName("recipeYield") val recipeYield: String = "",
|
||||
@SerialName("recipeIngredient") val recipeIngredient: List<AddRecipeIngredientV0> = emptyList(),
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<AddRecipeInstructionV0> = emptyList(),
|
||||
@SerialName("slug") val slug: String = "",
|
||||
@SerialName("filePath") val filePath: String = "",
|
||||
@SerialName("tags") val tags: List<String> = emptyList(),
|
||||
@SerialName("categories") val categories: List<String> = emptyList(),
|
||||
@SerialName("notes") val notes: List<AddRecipeNoteV0> = emptyList(),
|
||||
@SerialName("extras") val extras: Map<String, String> = emptyMap(),
|
||||
@SerialName("assets") val assets: List<String> = emptyList(),
|
||||
@SerialName("settings") val settings: AddRecipeSettingsV0 = AddRecipeSettingsV0(),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeSettingsV0(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("disableComments") val disableComments: Boolean = false,
|
||||
@SerialName("landscapeView") val landscapeView: Boolean = true,
|
||||
@SerialName("public") val public: Boolean = true,
|
||||
@SerialName("showAssets") val showAssets: Boolean = true,
|
||||
@SerialName("showNutrition") val showNutrition: Boolean = true,
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ErrorDetailV0(@SerialName("detail") val detail: String? = null)
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeIngredientResponse(
|
||||
data class GetRecipeIngredientResponseV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("note") val note: String = "",
|
||||
@SerialName("unit") val unit: String = "",
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeInstructionResponse(
|
||||
data class GetRecipeInstructionResponseV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String,
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
@@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeResponse(
|
||||
data class GetRecipeResponseV0(
|
||||
@SerialName("id") val remoteId: Int,
|
||||
@SerialName("name") val name: String,
|
||||
@SerialName("slug") val slug: String,
|
||||
@@ -18,6 +18,6 @@ data class GetRecipeResponse(
|
||||
@SerialName("dateAdded") val dateAdded: LocalDate,
|
||||
@SerialName("dateUpdated") val dateUpdated: LocalDateTime,
|
||||
@SerialName("recipeYield") val recipeYield: String = "",
|
||||
@SerialName("recipeIngredient") val recipeIngredients: List<GetRecipeIngredientResponse>,
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<GetRecipeInstructionResponse>,
|
||||
@SerialName("recipeIngredient") val recipeIngredients: List<GetRecipeIngredientResponseV0>,
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<GetRecipeInstructionResponseV0>,
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
@@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeSummaryResponse(
|
||||
data class GetRecipeSummaryResponseV0(
|
||||
@SerialName("id") val remoteId: Int,
|
||||
@SerialName("name") val name: String,
|
||||
@SerialName("slug") val slug: String,
|
||||
@@ -0,0 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetTokenResponseV0(@SerialName("access_token") val accessToken: String)
|
||||
@@ -0,0 +1,11 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class VersionResponseV0(
|
||||
@SerialName("production") val production: Boolean,
|
||||
@SerialName("version") val version: String,
|
||||
@SerialName("demoStatus") val demoStatus: Boolean,
|
||||
)
|
||||
@@ -1,6 +1,6 @@
|
||||
package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
@@ -10,7 +10,7 @@ interface MealieDataSourceV1 {
|
||||
suspend fun addRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequest,
|
||||
recipe: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
@@ -24,7 +24,7 @@ class MealieDataSourceV1Impl @Inject constructor(
|
||||
override suspend fun addRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequest
|
||||
recipe: AddRecipeRequestV0
|
||||
): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetTokenResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipesResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
@@ -15,13 +17,13 @@ interface MealieServiceV1 {
|
||||
@Url url: String,
|
||||
@Field("username") username: String,
|
||||
@Field("password") password: String,
|
||||
): GetTokenResponse
|
||||
): GetTokenResponseV0
|
||||
|
||||
@POST
|
||||
suspend fun addRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Body addRecipeRequest: AddRecipeRequest,
|
||||
@Body addRecipeRequestV0: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
@GET
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.datasource.models.GetTokenResponse
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.models.VersionResponse
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0Impl
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieServiceV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetTokenResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.VersionResponseV0
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.test.toJsonResponseBody
|
||||
import io.mockk.MockKAnnotations
|
||||
@@ -21,25 +22,25 @@ import java.io.IOException
|
||||
import java.net.ConnectException
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class MealieDataSourceImplTest {
|
||||
class MealieDataSourceV0ImplTest {
|
||||
|
||||
@MockK
|
||||
lateinit var service: MealieService
|
||||
lateinit var service: MealieServiceV0
|
||||
|
||||
@MockK(relaxUnitFun = true)
|
||||
lateinit var logger: Logger
|
||||
|
||||
lateinit var subject: MealieDataSourceImpl
|
||||
lateinit var subject: MealieDataSourceV0Impl
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
subject = MealieDataSourceImpl(logger, service, Json.Default)
|
||||
subject = MealieDataSourceV0Impl(logger, service, Json)
|
||||
}
|
||||
|
||||
@Test(expected = NetworkError.NotMealie::class)
|
||||
fun `when getVersionInfo and getVersion throws HttpException then NotMealie`() = runTest {
|
||||
val error = HttpException(Response.error<VersionResponse>(404, "".toJsonResponseBody()))
|
||||
val error = HttpException(Response.error<VersionResponseV0>(404, "".toJsonResponseBody()))
|
||||
coEvery { service.getVersion(any()) } throws error
|
||||
subject.getVersionInfo(TEST_BASE_URL)
|
||||
}
|
||||
@@ -60,14 +61,14 @@ class MealieDataSourceImplTest {
|
||||
|
||||
@Test
|
||||
fun `when getVersionInfo and getVersion returns result then result`() = runTest {
|
||||
val versionResponse = VersionResponse(true, "v0.5.6", true)
|
||||
val versionResponse = VersionResponseV0(true, "v0.5.6", true)
|
||||
coEvery { service.getVersion(any()) } returns versionResponse
|
||||
assertThat(subject.getVersionInfo(TEST_BASE_URL)).isSameInstanceAs(versionResponse)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authentication is successful then token is correct`() = runTest {
|
||||
coEvery { service.getToken(any(), any(), any()) } returns GetTokenResponse(TEST_TOKEN)
|
||||
coEvery { service.getToken(any(), any(), any()) } returns GetTokenResponseV0(TEST_TOKEN)
|
||||
assertThat(callAuthenticate()).isEqualTo(TEST_TOKEN)
|
||||
}
|
||||
|
||||
@@ -76,7 +77,7 @@ class MealieDataSourceImplTest {
|
||||
val body = "{\"detail\":\"Unauthorized\"}".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
} throws HttpException(Response.error<GetTokenResponseV0>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ class MealieDataSourceImplTest {
|
||||
val body = "{\"detail\":\"Something\"}".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
} throws HttpException(Response.error<GetTokenResponseV0>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@@ -94,7 +95,7 @@ class MealieDataSourceImplTest {
|
||||
val body = "".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
} throws HttpException(Response.error<GetTokenResponseV0>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user