Initialize v1 support
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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?,
|
||||
|
||||
@@ -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>,
|
||||
)
|
||||
Reference in New Issue
Block a user