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

util.Request.kt Maven / Gradle / Ivy

package com.amplitude.experiment.util

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okio.IOException
import java.util.concurrent.CompletableFuture

private val json = Json {
    ignoreUnknownKeys = true
}

internal inline fun  OkHttpClient.request(
    request: Request,
): CompletableFuture {
    val future = CompletableFuture()
    val call = newCall(request)
    call.enqueue(object : Callback {
        override fun onResponse(call: Call, response: Response) {
            try {
                val result = response.use {
                    if (!response.isSuccessful) {
                        throw IOException("$request - error response: $response")
                    }
                    val body = response.body?.string() ?: throw IOException("$request - null response body")
                    json.decodeFromString(body)
                }
                future.complete(result)
            } catch (e: Exception) {
                future.completeExceptionally(e)
            }
        }

        override fun onFailure(call: Call, e: IOException) {
            future.completeExceptionally(e)
        }
    })
    return future
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy