Extract Base URL from authentication
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
interface BaseURLStorage {
|
||||
|
||||
suspend fun getBaseURL(): String?
|
||||
|
||||
suspend fun requireBaseURL(): String
|
||||
|
||||
suspend fun storeBaseURL(baseURL: String)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
import gq.kirmanak.mealient.data.storage.PreferencesStorage
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class BaseURLStorageImpl @Inject constructor(
|
||||
private val preferencesStorage: PreferencesStorage,
|
||||
) : BaseURLStorage {
|
||||
|
||||
private val baseUrlKey by preferencesStorage::baseUrlKey
|
||||
|
||||
override suspend fun getBaseURL(): String? = preferencesStorage.getValue(baseUrlKey)
|
||||
|
||||
override suspend fun requireBaseURL(): String = checkNotNull(getBaseURL()) {
|
||||
"Base URL was null when it was required"
|
||||
}
|
||||
|
||||
override suspend fun storeBaseURL(baseURL: String) {
|
||||
preferencesStorage.storeValues(Pair(baseUrlKey, baseURL))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
import gq.kirmanak.mealient.data.network.NetworkError
|
||||
|
||||
interface VersionDataSource {
|
||||
|
||||
@Throws(NetworkError::class)
|
||||
suspend fun getVersionInfo(baseUrl: String): VersionInfo
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
import gq.kirmanak.mealient.data.network.NetworkError
|
||||
import gq.kirmanak.mealient.data.network.ServiceFactory
|
||||
import gq.kirmanak.mealient.extensions.versionInfo
|
||||
import kotlinx.serialization.SerializationException
|
||||
import retrofit2.HttpException
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class VersionDataSourceImpl @Inject constructor(
|
||||
private val serviceFactory: ServiceFactory<VersionService>,
|
||||
) : VersionDataSource {
|
||||
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo {
|
||||
Timber.v("getVersionInfo() called with: baseUrl = $baseUrl")
|
||||
|
||||
val service = try {
|
||||
serviceFactory.provideService(baseUrl)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "getVersionInfo: can't create service")
|
||||
throw NetworkError.MalformedUrl(e)
|
||||
}
|
||||
|
||||
val response = try {
|
||||
service.getVersion()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "getVersionInfo: can't request version")
|
||||
when (e) {
|
||||
is HttpException, is SerializationException -> throw NetworkError.NotMealie(e)
|
||||
else -> throw NetworkError.NoServerConnection(e)
|
||||
}
|
||||
}
|
||||
|
||||
return response.versionInfo()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
data class VersionInfo(
|
||||
val production: Boolean,
|
||||
val version: String,
|
||||
val demoStatus: Boolean,
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
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,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
import retrofit2.http.GET
|
||||
|
||||
interface VersionService {
|
||||
@GET("api/debug/version")
|
||||
suspend fun getVersion(): VersionResponse
|
||||
}
|
||||
Reference in New Issue
Block a user