This class is called by the EmailViewModel. It creates an HTTP client, crafts the log in POST request and handles the HTTP response message. It then returns the status of the log in attempt to the ViewModel.
This suspended function can be called by the ViewModel to attempt a log in request.
private val client = KtorClient.httpClient
val response: HttpResponse = client.post(".../api/auth/login") {
contentType(ContentType.Application.Json)
setBody(LoginRequest(email, password))
}
when (response.status.value) {
in 200..299 -> Result.success(response.body<AuthResponse>())
401 -> Result.failure(Exception("Invalid credentials"))
else -> {
val error = response.body<ApiError>()
Result.failure(Exception(error.message))
}
}
Your content here