Merge pull request #233 from nielsvanvelzen/recommended-is-actually-minimum

Rename recommendedVersion to minimumVersion
This commit is contained in:
Max Rumpf 2021-04-29 19:19:52 +02:00 committed by GitHub
commit f711ab9eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 13 deletions

View File

@ -11,7 +11,7 @@ public final class org/jellyfin/sdk/Jellyfin {
public final class org/jellyfin/sdk/Jellyfin$Companion {
public final fun getApiVersion ()Lorg/jellyfin/sdk/model/ServerVersion;
public final fun getRecommendedVersion ()Lorg/jellyfin/sdk/model/ServerVersion;
public final fun getMinimumVersion ()Lorg/jellyfin/sdk/model/ServerVersion;
}
public final class org/jellyfin/sdk/JellyfinOptions {

View File

@ -58,7 +58,14 @@ public class Jellyfin(
}
public companion object {
public val recommendedVersion: ServerVersion = ServerVersion(10, 7, 0, 0)
/**
* The minimum server version expected to work. Lower versions may work but are not supported.
*/
public val minimumVersion: ServerVersion = ServerVersion(10, 7, 0, 0)
/**
* The exact server version used to generate the API. Should be equal or higher than [minimumVersion].
*/
public val apiVersion: ServerVersion = ServerVersion.fromString(ApiConstants.apiVersion)!!
}
}

View File

@ -11,14 +11,20 @@ import org.slf4j.LoggerFactory
import java.net.ConnectException
public class RecommendedServerDiscovery(
private val jellyfin: Jellyfin
private val jellyfin: Jellyfin,
) {
private val logger = LoggerFactory.getLogger("RecommendedServerDiscovery")
private companion object {
private const val HTTP_OK = 200
private const val HTTPS_PREFIX = "https://"
private const val PRODUCT_NAME = "Jellyfin Server"
}
private data class SystemInfoResult(
val address: String,
val systemInfo: PublicSystemInfo?,
val responseTime: Long
val responseTime: Long,
)
private suspend fun getSystemInfoResult(address: String): SystemInfoResult? {
@ -28,6 +34,7 @@ public class RecommendedServerDiscovery(
val api = SystemApi(client)
val startTime = System.currentTimeMillis()
@Suppress("TooGenericExceptionCaught")
val info = try {
api.getPublicSystemInfo()
@ -40,10 +47,9 @@ public class RecommendedServerDiscovery(
}
val endTime = System.currentTimeMillis()
@Suppress("MagicNumber")
return SystemInfoResult(
address = address,
systemInfo = if (info != null && info.status == 200) info.content else null,
systemInfo = if (info != null && info.status == HTTP_OK) info.content else null,
responseTime = endTime - startTime,
)
}
@ -53,7 +59,7 @@ public class RecommendedServerDiscovery(
var points = 0
// Security
if (result.address.startsWith("https://")) points += 3
if (result.address.startsWith(HTTPS_PREFIX)) points += 3
// Speed
when {
@ -66,13 +72,14 @@ public class RecommendedServerDiscovery(
val version = result.systemInfo?.version?.let(ServerVersion::fromString)
if (version != null) {
if (version >= Jellyfin.apiVersion) points += 1
if (version == Jellyfin.recommendedVersion) points += 2
else if (version > Jellyfin.recommendedVersion) points += 1
if (version >= Jellyfin.minimumVersion) points += 1
}
val productName = result.systemInfo?.productName
if (productName != null && !productName.equals(PRODUCT_NAME, ignoreCase = true)) points = 0
// Minimum amount of points: 0
// Maximum amount of points: 9
// Maximum amount of points: 8
val score = when {
points < 3 -> RecommendedServerInfoScore.BAD
points < 6 -> RecommendedServerInfoScore.OK
@ -85,7 +92,7 @@ public class RecommendedServerDiscovery(
public suspend fun discover(
servers: List<String>,
includeAppendedServers: Boolean,
minimumScore: RecommendedServerInfoScore
minimumScore: RecommendedServerInfoScore,
): Flow<RecommendedServerInfo> = discover(
servers = servers.asFlow(),
includeAppendedServers = includeAppendedServers,
@ -95,7 +102,7 @@ public class RecommendedServerDiscovery(
public suspend fun discover(
servers: Flow<String>,
includeAppendedServers: Boolean,
minimumScore: RecommendedServerInfoScore
minimumScore: RecommendedServerInfoScore,
): Flow<RecommendedServerInfo> = withContext(Dispatchers.IO) {
flow {
servers.onEach parentEach@{ server ->