2020-09-22 02:20:22 +00:00
|
|
|
package dev.inmo.micro_utils.ktor.client
|
|
|
|
|
2020-09-26 15:42:05 +00:00
|
|
|
import dev.inmo.micro_utils.ktor.common.*
|
2020-09-22 02:20:22 +00:00
|
|
|
import io.ktor.client.HttpClient
|
|
|
|
import io.ktor.client.request.get
|
|
|
|
import io.ktor.client.request.post
|
2020-11-27 07:30:02 +00:00
|
|
|
import kotlinx.serialization.*
|
2020-09-22 02:20:22 +00:00
|
|
|
|
|
|
|
typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>
|
|
|
|
|
2020-11-27 07:30:02 +00:00
|
|
|
class UnifiedRequester(
|
2021-12-14 07:25:58 +00:00
|
|
|
val client: HttpClient = HttpClient(),
|
|
|
|
val serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
|
2020-11-27 07:30:02 +00:00
|
|
|
) {
|
|
|
|
suspend fun <ResultType> uniget(
|
|
|
|
url: String,
|
|
|
|
resultDeserializer: DeserializationStrategy<ResultType>
|
2021-05-21 12:04:04 +00:00
|
|
|
): ResultType = client.uniget(url, resultDeserializer, serialFormat)
|
2020-11-27 07:30:02 +00:00
|
|
|
|
|
|
|
fun <T> encodeUrlQueryValue(
|
|
|
|
serializationStrategy: SerializationStrategy<T>,
|
|
|
|
value: T
|
2021-05-21 12:04:04 +00:00
|
|
|
) = serializationStrategy.encodeUrlQueryValue(
|
|
|
|
value,
|
|
|
|
serialFormat
|
2020-11-27 07:30:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suspend fun <BodyType, ResultType> unipost(
|
|
|
|
url: String,
|
|
|
|
bodyInfo: BodyPair<BodyType>,
|
|
|
|
resultDeserializer: DeserializationStrategy<ResultType>
|
2021-05-21 12:04:04 +00:00
|
|
|
) = client.unipost(url, bodyInfo, resultDeserializer, serialFormat)
|
2020-11-27 07:30:02 +00:00
|
|
|
|
|
|
|
fun <T> createStandardWebsocketFlow(
|
|
|
|
url: String,
|
|
|
|
checkReconnection: (Throwable?) -> Boolean = { true },
|
|
|
|
deserializer: DeserializationStrategy<T>
|
|
|
|
) = client.createStandardWebsocketFlow(url, checkReconnection, deserializer, serialFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
val defaultRequester = UnifiedRequester()
|
|
|
|
|
2020-09-22 02:20:22 +00:00
|
|
|
suspend fun <ResultType> HttpClient.uniget(
|
|
|
|
url: String,
|
2021-05-21 12:04:04 +00:00
|
|
|
resultDeserializer: DeserializationStrategy<ResultType>,
|
|
|
|
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
|
|
|
|
) = get<StandardKtorSerialInputData>(
|
|
|
|
url
|
|
|
|
).let {
|
|
|
|
serialFormat.decodeDefault(resultDeserializer, it)
|
|
|
|
}
|
2020-09-22 02:20:22 +00:00
|
|
|
|
2020-09-26 15:42:05 +00:00
|
|
|
|
2021-05-21 12:04:04 +00:00
|
|
|
fun <T> SerializationStrategy<T>.encodeUrlQueryValue(
|
|
|
|
value: T,
|
|
|
|
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
|
|
|
|
) = serialFormat.encodeHex(
|
|
|
|
this,
|
|
|
|
value
|
|
|
|
)
|
2020-09-22 02:20:22 +00:00
|
|
|
|
|
|
|
suspend fun <BodyType, ResultType> HttpClient.unipost(
|
|
|
|
url: String,
|
|
|
|
bodyInfo: BodyPair<BodyType>,
|
2021-05-21 12:04:04 +00:00
|
|
|
resultDeserializer: DeserializationStrategy<ResultType>,
|
|
|
|
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
|
|
|
|
) = post<StandardKtorSerialInputData>(url) {
|
|
|
|
body = serialFormat.encodeDefault(bodyInfo.first, bodyInfo.second)
|
|
|
|
}.let {
|
|
|
|
serialFormat.decodeDefault(resultDeserializer, it)
|
|
|
|
}
|