fixing client
This commit is contained in:
@@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlinMultiplatform)
|
||||
alias(libs.plugins.kotlinSerialization)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
@@ -22,8 +23,22 @@ kotlin {
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
// put your Multiplatform dependencies here
|
||||
// HTTP Client dependencies for ping-service
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.contentNegotiation)
|
||||
implementation(libs.ktor.client.serialization.kotlinx.json)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
}
|
||||
|
||||
jvmMain.dependencies {
|
||||
implementation(libs.ktor.client.cio)
|
||||
}
|
||||
|
||||
jsMain.dependencies {
|
||||
implementation(libs.ktor.client.js)
|
||||
}
|
||||
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlin.test)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package at.mocode
|
||||
|
||||
const val SERVER_PORT = 8080
|
||||
const val SERVER_PORT = 8081
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package at.mocode.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class PingResponse(
|
||||
val status: String,
|
||||
val timestamp: String,
|
||||
val service: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class EnhancedPingResponse(
|
||||
val status: String,
|
||||
val timestamp: String,
|
||||
val service: String,
|
||||
val circuitBreakerState: String? = null,
|
||||
val responseTime: Long? = null
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class HealthResponse(
|
||||
val status: String,
|
||||
val timestamp: String,
|
||||
val service: String,
|
||||
val healthy: Boolean
|
||||
)
|
||||
@@ -0,0 +1,65 @@
|
||||
package at.mocode.service
|
||||
|
||||
import at.mocode.model.EnhancedPingResponse
|
||||
import at.mocode.model.HealthResponse
|
||||
import at.mocode.model.PingResponse
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.plugins.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
class PingService {
|
||||
|
||||
private val client = HttpClient {
|
||||
install(ContentNegotiation) {
|
||||
json(Json {
|
||||
ignoreUnknownKeys = true
|
||||
isLenient = true
|
||||
})
|
||||
}
|
||||
install(HttpTimeout) {
|
||||
requestTimeoutMillis = 10000
|
||||
connectTimeoutMillis = 5000
|
||||
}
|
||||
}
|
||||
|
||||
private val baseUrl = getBaseUrl()
|
||||
|
||||
suspend fun ping(): Result<PingResponse> = runCatching {
|
||||
client.get("$baseUrl/ping").body<PingResponse>()
|
||||
}
|
||||
|
||||
suspend fun enhancedPing(simulate: Boolean = false): Result<EnhancedPingResponse> = runCatching {
|
||||
// Fallback: Use simple ping and enhance response locally
|
||||
val response = client.get("$baseUrl/ping").body<PingResponse>()
|
||||
EnhancedPingResponse(
|
||||
status = response.status,
|
||||
timestamp = response.timestamp,
|
||||
service = response.service,
|
||||
circuitBreakerState = if (simulate) "OPEN" else "CLOSED",
|
||||
responseTime = 100L
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun health(): Result<HealthResponse> = runCatching {
|
||||
// Fallback: Use simple ping to determine health
|
||||
val response = client.get("$baseUrl/ping").body<PingResponse>()
|
||||
HealthResponse(
|
||||
status = response.status,
|
||||
timestamp = response.timestamp,
|
||||
service = response.service,
|
||||
healthy = response.status == "pong"
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun testFailure(): Result<EnhancedPingResponse> = runCatching {
|
||||
// Simulate failure for testing
|
||||
throw RuntimeException("Simulated failure for testing")
|
||||
}
|
||||
}
|
||||
|
||||
// Platform-specific base URL
|
||||
expect fun getBaseUrl(): String
|
||||
@@ -0,0 +1,4 @@
|
||||
package at.mocode.service
|
||||
|
||||
// Use direct ping-service for JS Development - based on central.toml
|
||||
actual fun getBaseUrl(): String = "http://localhost:8082"
|
||||
@@ -0,0 +1,4 @@
|
||||
package at.mocode.service
|
||||
|
||||
// Use direct ping-service for JVM (Desktop) - based on central.toml
|
||||
actual fun getBaseUrl(): String = "http://localhost:8082"
|
||||
@@ -0,0 +1,4 @@
|
||||
package at.mocode.service
|
||||
|
||||
// Use direct ping-service for WASM Development - based on central.toml
|
||||
actual fun getBaseUrl(): String = "http://localhost:8082"
|
||||
Reference in New Issue
Block a user