
commonMain.ru.alexey.event.threads.HttpRequestResource.kt Maven / Gradle / Ivy
package ru.alexey.event.threads
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.resources.delete
import io.ktor.client.plugins.resources.get
import io.ktor.client.plugins.resources.post
import io.ktor.client.plugins.resources.put
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.utils.io.core.use
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.serialization.Serializable
import ru.alexey.event.threads.resources.Resource
import ru.alexey.event.threads.resources.valueResource
object HttpRequestResource {
inline fun httpGet(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
url: String,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.get(url, block).body()
}
}.await()
}
)
}
inline fun httpGet(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
resource: R,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.get(resource, block).body()
}
}.await()
}
)
}
inline fun httpPost(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
url: String,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.post(url, block).body()
}
}.await()
}
)
}
inline fun httpPost(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
resource: R,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.post(resource, block).body()
}
}.await()
}
)
}
inline fun httpDelete(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
url: String,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.delete(url, block).body()
}
}.await()
}
)
}
inline fun httpDelete(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
resource: R,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.delete(resource, block).body()
}
}.await()
}
)
}
inline fun httpPut(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
url: String,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.put(url, block).body()
}
}.await()
}
)
}
inline fun httpPut(
coroutineScope: CoroutineScope,
httpClient: HttpClient,
resource: R,
crossinline block: HttpRequestBuilder.() -> Unit = {}
): Resource> {
return valueResource(
object : ResponseWrapper {
override suspend fun unwrap() = coroutineScope.async {
httpClient.use {
it.put(resource, block).body()
}
}.await()
}
)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy