add all client realizations for core repos
This commit is contained in:
@@ -62,3 +62,8 @@ inline fun <reified T> createStandardWebsocketFlow(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> HttpClient.createStandardWebsocketFlow(
|
||||
url: String,
|
||||
crossinline conversation: suspend (ByteArray) -> T
|
||||
) = createStandardWebsocketFlow(this, url, conversation)
|
||||
|
@@ -0,0 +1,17 @@
|
||||
package com.insanusmokrassar.postssystem.ktor
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
|
||||
typealias FromToDateTime = Pair<DateTime?, DateTime?>
|
||||
|
||||
val FromToDateTime.asFromToUrlPart: QueryParams
|
||||
get() = mapOf(
|
||||
"from" to first ?.unixMillis ?.toString(),
|
||||
"to" to second ?.unixMillis ?.toString()
|
||||
)
|
||||
|
||||
val QueryParams.extractFromToDateTime: FromToDateTime
|
||||
get() = FromToDateTime(
|
||||
get("from") ?.toDoubleOrNull() ?.let { DateTime(it) },
|
||||
get("to") ?.toDoubleOrNull() ?.let { DateTime(it) }
|
||||
)
|
@@ -0,0 +1,19 @@
|
||||
package com.insanusmokrassar.postssystem.ktor
|
||||
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.*
|
||||
|
||||
private val numberRegex = Regex("[\\d]{1,10}")
|
||||
private val pageRegex = Regex("page=${numberRegex.pattern}")
|
||||
private val sizeRegex = Regex("size=${numberRegex.pattern}")
|
||||
|
||||
val Pagination.asUrlQueryParts
|
||||
get() = mapOf(
|
||||
"page" to page.toString(),
|
||||
"size" to size.toString()
|
||||
)
|
||||
|
||||
val Map<String, String?>.extractPagination: Pagination
|
||||
get() = SimplePagination(
|
||||
get("page") ?.toIntOrNull() ?: 0,
|
||||
get("size") ?.toIntOrNull() ?: defaultMediumPageSize
|
||||
)
|
@@ -0,0 +1,16 @@
|
||||
package com.insanusmokrassar.postssystem.ktor
|
||||
|
||||
typealias QueryParams = Map<String, String?>
|
||||
|
||||
val QueryParams.asUrlQuery: String
|
||||
get() = keys.joinToString("&") { "${it}${get(it) ?.let { value -> "=$value" }}" }
|
||||
fun String.includeQueryParams(
|
||||
queryParams: QueryParams
|
||||
): String = "$this${if (contains("?")) "&" else "?"}${queryParams.asUrlQuery}"
|
||||
|
||||
val String.parseUrlQuery: QueryParams
|
||||
get() = split("&").map {
|
||||
it.split("=").let { pair ->
|
||||
pair.first() to pair.getOrNull(1)
|
||||
}
|
||||
}.toMap()
|
@@ -2,4 +2,4 @@ package com.insanusmokrassar.postssystem.ktor
|
||||
|
||||
import kotlinx.serialization.cbor.Cbor
|
||||
|
||||
val standardKtorSerializer = Cbor
|
||||
val standardKtorSerialFormat = Cbor
|
||||
|
@@ -3,13 +3,9 @@ package com.insanusmokrassar.postssystem.ktor.tests
|
||||
import com.insanusmokrassar.postssystem.ktor.client.createStandardWebsocketFlow
|
||||
import com.insanusmokrassar.postssystem.ktor.server.createKtorServer
|
||||
import com.insanusmokrassar.postssystem.ktor.server.includeWebsocketHandling
|
||||
import com.insanusmokrassar.postssystem.ktor.standardKtorSerializer
|
||||
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
|
||||
import io.ktor.application.install
|
||||
import io.ktor.application.log
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.http.cio.websocket.pingPeriod
|
||||
import io.ktor.http.cio.websocket.timeout
|
||||
import io.ktor.routing.route
|
||||
import io.ktor.routing.routing
|
||||
import io.ktor.websocket.WebSockets
|
||||
import kotlinx.coroutines.*
|
||||
@@ -17,7 +13,6 @@ import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import org.junit.Test
|
||||
import java.time.Duration
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
@@ -33,7 +28,7 @@ class WebsocketsTest {
|
||||
install(WebSockets)
|
||||
routing {
|
||||
includeWebsocketHandling(suburl, dataFlow) {
|
||||
standardKtorSerializer.dump(Int.serializer(), it)
|
||||
standardKtorSerialFormat.dump(Int.serializer(), it)
|
||||
}
|
||||
}
|
||||
}.also {
|
||||
@@ -50,7 +45,7 @@ class WebsocketsTest {
|
||||
"$serverUrl/$suburl",
|
||||
{ false } // always skip reconnection
|
||||
) {
|
||||
standardKtorSerializer.load(Int.serializer(), it)
|
||||
standardKtorSerialFormat.load(Int.serializer(), it)
|
||||
}
|
||||
|
||||
var currentlyCheckingData: Int? = null
|
||||
|
Reference in New Issue
Block a user