All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deque.networking.models.HealthCheck.kt Maven / Gradle / Ivy

There is a newer version: 5.5.2
Show newest version
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)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy