Handle server info inside of AuthDataSource

This commit is contained in:
Kirill Kamakin
2022-10-30 13:40:43 +01:00
parent 039ba20c83
commit 94c030c04d
7 changed files with 55 additions and 57 deletions

View File

@@ -1,23 +1,23 @@
package gq.kirmanak.mealient.data.add
data class AddRecipeInfo(
val name: String = "",
val description: String = "",
val recipeYield: String = "",
val recipeIngredient: List<AddRecipeIngredientInfo> = emptyList(),
val recipeInstructions: List<AddRecipeInstructionInfo> = emptyList(),
val settings: AddRecipeSettingsInfo = AddRecipeSettingsInfo(),
val name: String,
val description: String,
val recipeYield: String,
val recipeIngredient: List<AddRecipeIngredientInfo>,
val recipeInstructions: List<AddRecipeInstructionInfo>,
val settings: AddRecipeSettingsInfo,
)
data class AddRecipeSettingsInfo(
val disableComments: Boolean = false,
val public: Boolean = true,
val disableComments: Boolean,
val public: Boolean,
)
data class AddRecipeIngredientInfo(
val note: String = "",
val note: String,
)
data class AddRecipeInstructionInfo(
val text: String = "",
val text: String,
)

View File

@@ -1,15 +1,8 @@
package gq.kirmanak.mealient.data.auth
import gq.kirmanak.mealient.data.baseurl.ServerVersion
interface AuthDataSource {
/**
* Tries to acquire authentication token using the provided credentials
*/
suspend fun authenticate(
username: String,
password: String,
baseUrl: String,
serverVersion: ServerVersion,
): String
suspend fun authenticate(username: String, password: String): String
}

View File

@@ -1,6 +1,7 @@
package gq.kirmanak.mealient.data.auth.impl
import gq.kirmanak.mealient.data.auth.AuthDataSource
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
import gq.kirmanak.mealient.data.baseurl.ServerVersion
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
@@ -9,6 +10,7 @@ import javax.inject.Singleton
@Singleton
class AuthDataSourceImpl @Inject constructor(
private val serverInfoRepo: ServerInfoRepo,
private val v0Source: MealieDataSourceV0,
private val v1Source: MealieDataSourceV1,
) : AuthDataSource {
@@ -16,10 +18,11 @@ class AuthDataSourceImpl @Inject constructor(
override suspend fun authenticate(
username: String,
password: String,
baseUrl: String,
serverVersion: ServerVersion,
): String = when (serverVersion) {
ServerVersion.V0 -> v0Source.authenticate(baseUrl, username, password)
ServerVersion.V1 -> v1Source.authenticate(baseUrl, username, password)
): String {
val baseUrl = serverInfoRepo.requireUrl()
return when (serverInfoRepo.getVersion()) {
ServerVersion.V0 -> v0Source.authenticate(baseUrl, username, password)
ServerVersion.V1 -> v1Source.authenticate(baseUrl, username, password)
}
}
}

View File

@@ -3,7 +3,6 @@ package gq.kirmanak.mealient.data.auth.impl
import gq.kirmanak.mealient.data.auth.AuthDataSource
import gq.kirmanak.mealient.data.auth.AuthRepo
import gq.kirmanak.mealient.data.auth.AuthStorage
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
import gq.kirmanak.mealient.logging.Logger
import kotlinx.coroutines.flow.Flow
@@ -15,7 +14,6 @@ import javax.inject.Singleton
class AuthRepoImpl @Inject constructor(
private val authStorage: AuthStorage,
private val authDataSource: AuthDataSource,
private val serverInfoRepo: ServerInfoRepo,
private val logger: Logger,
) : AuthRepo {
@@ -24,11 +22,9 @@ class AuthRepoImpl @Inject constructor(
override suspend fun authenticate(email: String, password: String) {
logger.v { "authenticate() called with: email = $email, password = $password" }
val version = serverInfoRepo.getVersion()
val url = serverInfoRepo.requireUrl()
authDataSource.authenticate(email, password, url, version)
.let { AUTH_HEADER_FORMAT.format(it) }
.let { authStorage.setAuthHeader(it) }
val token = authDataSource.authenticate(email, password)
val header = AUTH_HEADER_FORMAT.format(token)
authStorage.setAuthHeader(header)
authStorage.setEmail(email)
authStorage.setPassword(password)
}