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

commonMain.com.plusmobileapps.firebase.auth.model.network.HttpClientExt.kt Maven / Gradle / Ivy

Go to download

A kotlin multiplatform mobile library for authenticating with Firebase for Android, iOS, and JVM

There is a newer version: 0.4
Show newest version
package com.plusmobileapps.firebase.auth.model.network

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.plugins.ResponseException
import io.ktor.client.plugins.ServerResponseException
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.request
import io.ktor.utils.io.errors.IOException
import kotlinx.serialization.SerializationException

suspend inline fun  HttpClient.safeRequest(
    block: HttpRequestBuilder.() -> Unit,
): ApiResponse =
    try {
        val response = request { block() }
        ApiResponse.Success(response.body())
    } catch (e: ClientRequestException) {
        ApiResponse.Error.HttpError(e.response.status.value, e.errorBody())
    } catch (e: ServerResponseException) {
        ApiResponse.Error.HttpError(e.response.status.value, e.errorBody())
    } catch (e: IOException) {
        ApiResponse.Error.NetworkError
    } catch (e: SerializationException) {
        ApiResponse.Error.SerializationError
    }

suspend inline fun  ResponseException.errorBody(): E? =
    try {
        response.body()
    } catch (e: SerializationException) {
        null
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy