com.deque.networking.models.HealthCheck.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of axe-devtools-android-data Show documentation
Show all versions of axe-devtools-android-data Show documentation
The Axe Devtools Android Data Library
package com.deque.networking.models
import com.deque.networking.interfaces.HealthCheckInterface
import kotlinx.coroutines.runBlocking
import java.lang.IllegalArgumentException
class HealthCheck(@JvmField val status: HealthStatus) {
companion object{
private fun getClient(dbUrl: String): HealthCheckInterface {
val retrofit = DequeRetrofitBuilder(dbUrl)
return retrofit.client(HealthCheckInterface::class.java)
}
fun checkDbUrl(dbUrl: String): Pair {
val path = extractedPath(dbUrl)
val client = getClient(dbUrl)
var log = ""
val healthCheckResponse = runBlocking {
if (path.isEmpty()) {
client.getHealthCheck().onFailure {
System.err.println("ADT localized message: " + it.localizedMessage)
System.err.println("ADT message: " + it.message)
log = it.message + it.localizedMessage
}
} else {
client.getHealthCheck(path).onFailure {
System.err.println("ADT localized message: " + it.localizedMessage)
System.err.println("ADT message: " + it.message)
log = it.message + it.localizedMessage
}
}
}
val healthCheckResult: HealthCheck? = healthCheckResponse.getOrNull()
val passingCode = "UP"
return Pair(healthCheckResult?.status?.code == passingCode, log)
}
private fun extractedPath(dbUrl: String): String {
val url1 = dbUrl.replace("https://", "")
val remTrailing = url1.removeSuffix("/")
if (remTrailing.contains("/")) {
return remTrailing.substring(remTrailing.indexOf("/") + 1)
}
return ""
}
}
}
data class HealthStatus(@JvmField val code: String, @JvmField val description: String)