Skip to content

Latest commit

 

History

History
80 lines (59 loc) · 2.15 KB

File metadata and controls

80 lines (59 loc) · 2.15 KB

Integrating moon-server-kmp

An opinionated Ktor-based HTTP client wrapped by MoonServerClient, with built-in retry, exponential backoff, and typed error surface via MoonServerException.

1. Prerequisites

  • Kotlin 2.0.20, Compose Multiplatform 1.7.x, AGP 8.6.x, Gradle 8.10, Java 21.
  • A reachable backend (the MoonDeveloper backend, or your own).
  • Valid moonLicenseKey.
  • Koin 4.1.0.

2. Add the repository + dependency

(Same repository block as auth/en.md §2.)

implementation("io.moondeveloper:moon-server-kmp:1.0.0")

3. Platform setup

Android

  • INTERNET permission (standard, present in the sample manifest).

iOS

  • No special capability needed; the module bundles the Ktor Darwin engine.

4. Initialize Koin

import io.moondev.server.moonServerModule

startKoin {
    modules(moonServerModule /*, ... */)
}

MoonServerClient becomes available from any Koin scope.

5. Use in a ViewModel / Store (MVI)

class HealthStore(private val client: MoonServerClient) {
    suspend fun ping(): String =
        try {
            client.get("/health")
        } catch (e: MoonServerException) {
            "down: ${e.code} ${e.message}"
        }
}

6. Compose UI binding

@Composable
fun ServerScreen(store: HealthStore = koinInject()) {
    val scope = rememberCoroutineScope()
    var result by remember { mutableStateOf("") }
    Button(onClick = { scope.launch { result = store.ping() } }) { Text("Ping") }
    Text(result)
}

7. Common issues

  • MoonServerException(code=401) on every call — check the auth token interceptor (the module pulls the token from AuthProvider if both are wired).
  • Timeouts on slow networks — raise the per-request timeout via the client's configuration hook; the default is conservative.
  • iOS "URLSessionTask suspended" logs — usually a background-mode conflict; see Apple docs on app-lifecycle + URLSession.

8. Next steps