Initialize v1 support

This commit is contained in:
Kirill Kamakin
2022-08-07 17:19:35 +02:00
parent 684b492fb2
commit 95205f8ffe
24 changed files with 478 additions and 26 deletions

View File

@@ -44,6 +44,19 @@ class MealieDataSourceImpl @Inject constructor(
block = { getVersion("$baseUrl/api/debug/version") },
logMethod = { "getVersionInfo" },
logParameters = { "baseUrl = $baseUrl" },
).getOrElse {
when (it) {
is HttpException, is SerializationException -> getVersionInfoV1(baseUrl)
is SocketTimeoutException,
is ConnectException -> throw NetworkError.NoServerConnection(it)
else -> throw NetworkError.MalformedUrl(it)
}
}
private suspend fun getVersionInfoV1(baseUrl: String): VersionResponse = makeCall(
block = { getVersion("$baseUrl/api/app/about") },
logMethod = { "getVersionInfoV1" },
logParameters = { "baseUrl = $baseUrl" },
).getOrElse {
throw when (it) {
is HttpException, is SerializationException -> NetworkError.NotMealie(it)
@@ -58,7 +71,23 @@ class MealieDataSourceImpl @Inject constructor(
block = { getRecipeSummary("$baseUrl/api/recipes/summary", token, start, limit) },
logMethod = { "requestRecipes" },
logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" }
).getOrThrowUnauthorized()
).getOrElse {
val code = (it as? HttpException)?.code() ?: throw it
if (code == 404) requestRecipesV1(baseUrl, token, start, limit) else throw it
}
private suspend fun requestRecipesV1(
baseUrl: String, token: String?, start: Int, limit: Int
): List<GetRecipeSummaryResponse> {
// Imagine start is 30 and limit is 15. It means that we already have page 1 and 2, now we need page 3
val perPage = limit
val page = start / perPage + 1
return makeCall(
block = { getRecipeSummaryV1("$baseUrl/api/recipes", token, page, perPage) },
logMethod = { "requestRecipesV1" },
logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" }
).map { it.items }.getOrThrowUnauthorized()
}
override suspend fun requestRecipeInfo(
baseUrl: String, token: String?, slug: String

View File

@@ -34,6 +34,14 @@ interface MealieService {
@Query("limit") limit: Int,
): List<GetRecipeSummaryResponse>
@GET
suspend fun getRecipeSummaryV1(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Query("page") page: Int,
@Query("perPage") perPage: Int,
): GetRecipesResponseV1
@GET
suspend fun getRecipe(
@Url url: String,

View File

@@ -10,5 +10,5 @@ data class GetRecipeIngredientResponse(
@SerialName("unit") val unit: String = "",
@SerialName("food") val food: String = "",
@SerialName("disableAmount") val disableAmount: Boolean,
@SerialName("quantity") val quantity: Int,
@SerialName("quantity") val quantity: Double,
)

View File

@@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class GetRecipeResponse(
@SerialName("id") val remoteId: Long,
@SerialName("id") val remoteId: String,
@SerialName("name") val name: String,
@SerialName("slug") val slug: String,
@SerialName("image") val image: String,

View File

@@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class GetRecipeSummaryResponse(
@SerialName("id") val remoteId: Long,
@SerialName("id") val remoteId: String,
@SerialName("name") val name: String,
@SerialName("slug") val slug: String,
@SerialName("image") val image: String?,

View File

@@ -0,0 +1,9 @@
package gq.kirmanak.mealient.datasource.models
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class GetRecipesResponseV1(
@SerialName("items") val items: List<GetRecipeSummaryResponse>,
)