From 74ae91cba610389e578bc8b268634aedcd753b69 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 27 Nov 2020 11:57:40 +0600 Subject: [PATCH 1/4] start 0.4.7 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb248dad33a..7f2315b557a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.4.7 + ## 0.4.6 * `Common` diff --git a/gradle.properties b/gradle.properties index 5d73253b5e1..73bf3ac769b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -41,5 +41,5 @@ dokka_version=1.4.0 # Project data group=dev.inmo -version=0.4.6 -android_code_version=10 +version=0.4.7 +android_code_version=11 From 26d5f5a5f59c2ca17a550be443903bb8d578f3bc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 27 Nov 2020 13:30:02 +0600 Subject: [PATCH 2/4] UnifiedRequester --- CHANGELOG.md | 8 +++ .../ktor/client/FlowsWebsockets.kt | 6 +- .../ktor/client/StandardHttpClientGetPost.kt | 61 ++++++++++++++----- .../client/crud/KtorReadStandardCrudRepo.kt | 30 ++++++--- .../ktor/client/crud/KtorStandardCrudRepo.kt | 22 ++++++- .../client/crud/KtorWriteStandardCrudRepo.kt | 30 ++++++--- .../key_value/KtorReadStandardKeyValueRepo.kt | 51 ++++++++++------ .../key_value/KtorStandartKeyValueRepo.kt | 22 +++++-- .../KtorWriteStandardKeyValueRepo.kt | 23 +++++-- .../one_to_many/KtorOneToManyKeyValueRepo.kt | 20 ++++-- .../KtorReadOneToManyKeyValueRepo.kt | 49 +++++++++------ .../KtorWriteOneToManyKeyValueRepo.kt | 29 ++++++--- 12 files changed, 251 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f2315b557a..d1dcd63392b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## 0.4.7 +* `Ktor` + * `Client` + * New class `UnifiedRequester` +* `Repos` + * `Ktor` + * `Client` + * Rewriting of all clients on new `UnifiedRequester` + ## 0.4.6 * `Common` diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt index 92bb32dffd2..e011d755a85 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt @@ -8,6 +8,7 @@ import io.ktor.http.cio.websocket.Frame import io.ktor.http.cio.websocket.readBytes import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.channelFlow +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.DeserializationStrategy /** @@ -64,10 +65,11 @@ inline fun HttpClient.createStandardWebsocketFlow( inline fun HttpClient.createStandardWebsocketFlow( url: String, crossinline checkReconnection: (Throwable?) -> Boolean = { true }, - deserializer: DeserializationStrategy + deserializer: DeserializationStrategy, + serialFormat: BinaryFormat = standardKtorSerialFormat ) = createStandardWebsocketFlow( url, checkReconnection ) { - standardKtorSerialFormat.decodeDefault(deserializer, it) + serialFormat.decodeDefault(deserializer, it) } diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt index d5d79b41c25..b410dc3e664 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt @@ -4,32 +4,61 @@ import dev.inmo.micro_utils.ktor.common.* import io.ktor.client.HttpClient import io.ktor.client.request.get import io.ktor.client.request.post -import kotlinx.serialization.DeserializationStrategy -import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.* typealias BodyPair = Pair, T> +class UnifiedRequester( + private val client: HttpClient = HttpClient(), + private val serialFormat: BinaryFormat = standardKtorSerialFormat +) { + suspend fun uniget( + url: String, + resultDeserializer: DeserializationStrategy + ): ResultType = client.get( + url + ).let { + serialFormat.decodeDefault(resultDeserializer, it) + } + + + fun encodeUrlQueryValue( + serializationStrategy: SerializationStrategy, + value: T + ) = serialFormat.encodeHex( + serializationStrategy, + value + ) + + suspend fun unipost( + url: String, + bodyInfo: BodyPair, + resultDeserializer: DeserializationStrategy + ) = client.post(url) { + body = serialFormat.encodeDefault(bodyInfo.first, bodyInfo.second) + }.let { + serialFormat.decodeDefault(resultDeserializer, it) + } + + fun createStandardWebsocketFlow( + url: String, + checkReconnection: (Throwable?) -> Boolean = { true }, + deserializer: DeserializationStrategy + ) = client.createStandardWebsocketFlow(url, checkReconnection, deserializer, serialFormat) +} + +val defaultRequester = UnifiedRequester() + suspend fun HttpClient.uniget( url: String, resultDeserializer: DeserializationStrategy -) = get( - url -).let { - standardKtorSerialFormat.decodeDefault(resultDeserializer, it) -} +) = defaultRequester.uniget(url, resultDeserializer) -fun SerializationStrategy.encodeUrlQueryValue(value: T) = standardKtorSerialFormat.encodeHex( - this, - value -) +fun SerializationStrategy.encodeUrlQueryValue(value: T) = defaultRequester.encodeUrlQueryValue(this, value) suspend fun HttpClient.unipost( url: String, bodyInfo: BodyPair, resultDeserializer: DeserializationStrategy -) = post(url) { - body = standardKtorSerialFormat.encodeDefault(bodyInfo.first, bodyInfo.second) -}.let { - standardKtorSerialFormat.decodeDefault(resultDeserializer, it) -} +) = defaultRequester.unipost(url, bodyInfo, resultDeserializer) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 0647ffcb641..dacc0b8ec64 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -1,52 +1,64 @@ package dev.inmo.micro_utils.repos.ktor.client.crud -import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue -import dev.inmo.micro_utils.ktor.client.uniget +import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer class KtorReadStandardCrudRepo ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val objectsSerializer: KSerializer, private val objectsSerializerNullable: KSerializer, private val idsSerializer: KSerializer ) : ReadStandardCRUDRepo { private val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) - override suspend fun getByPagination(pagination: Pagination): PaginationResult = client.uniget( + constructor( + baseUrl: String, + client: HttpClient, + objectsSerializer: KSerializer, + objectsSerializerNullable: KSerializer, + idsSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer + ) + + override suspend fun getByPagination(pagination: Pagination): PaginationResult = unifiedRequester.uniget( buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts), paginationResultSerializer ) - override suspend fun getById(id: IdType): ObjectType? = client.uniget( + override suspend fun getById(id: IdType): ObjectType? = unifiedRequester.uniget( buildStandardUrl( baseUrl, getByIdRouting, mapOf( - "id" to idsSerializer.encodeUrlQueryValue(id) + "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) ) ), objectsSerializerNullable ) - override suspend fun contains(id: IdType): Boolean = client.uniget( + override suspend fun contains(id: IdType): Boolean = unifiedRequester.uniget( buildStandardUrl( baseUrl, containsRouting, mapOf( - "id" to idsSerializer.encodeUrlQueryValue(id) + "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) ) ), Boolean.serializer() ) - override suspend fun count(): Long = client.uniget( + override suspend fun count(): Long = unifiedRequester.uniget( buildStandardUrl( baseUrl, countRouting diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt index 97d8a0abbab..801affd5b99 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt @@ -1,13 +1,16 @@ package dev.inmo.micro_utils.repos.ktor.client.crud +import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer class KtorStandardCrudRepo ( baseUrl: String, baseSubpart: String, - client: HttpClient, + unifiedRequester: UnifiedRequester, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, @@ -15,16 +18,29 @@ class KtorStandardCrudRepo ( ) : StandardCRUDRepo, ReadStandardCRUDRepo by KtorReadStandardCrudRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, objectsSerializer, objectsNullableSerializer, idsSerializer ), WriteStandardCRUDRepo by KtorWriteStandardCrudRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer + ) { + constructor( + baseUrl: String, + baseSubpart: String, + client: HttpClient, + objectsSerializer: KSerializer, + objectsNullableSerializer: KSerializer, + inputsSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this( + baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer ) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt index 730829dfcde..c6d270fa178 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt @@ -2,17 +2,19 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.UpdatedValuePair import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* class KtorWriteStandardCrudRepo ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val objectsSerializer: KSerializer, private val objectsNullableSerializer: KSerializer, private val inputsSerializer: KSerializer, @@ -27,39 +29,51 @@ class KtorWriteStandardCrudRepo ( ) private val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer) - override val newObjectsFlow: Flow = client.createStandardWebsocketFlow( + constructor( + baseUrl: String, + client: HttpClient, + objectsSerializer: KSerializer, + objectsSerializerNullable: KSerializer, + inputsSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, inputsSerializer, idsSerializer + ) + + override val newObjectsFlow: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, newObjectsFlowRouting), deserializer = objectsSerializer ) - override val updatedObjectsFlow: Flow = client.createStandardWebsocketFlow( + override val updatedObjectsFlow: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, updatedObjectsFlowRouting), deserializer = objectsSerializer ) - override val deletedObjectsIdsFlow: Flow = client.createStandardWebsocketFlow( + override val deletedObjectsIdsFlow: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting), deserializer = idsSerializer ) - override suspend fun create(values: List): List = client.unipost( + override suspend fun create(values: List): List = unifiedRequester.unipost( buildStandardUrl(baseUrl, createRouting), BodyPair(listInputSerializer, values), listObjectsSerializer ) - override suspend fun update(id: IdType, value: InputValue): ObjectType? = client.unipost( + override suspend fun update(id: IdType, value: InputValue): ObjectType? = unifiedRequester.unipost( buildStandardUrl(baseUrl, updateRouting), BodyPair(inputUpdateSerializer, id to value), objectsNullableSerializer ) - override suspend fun update(values: List>): List = client.unipost( + override suspend fun update(values: List>): List = unifiedRequester.unipost( buildStandardUrl(baseUrl, updateManyRouting), BodyPair(listInputUpdateSerializer, values), listObjectsSerializer ) - override suspend fun deleteById(ids: List) = client.unipost( + override suspend fun deleteById(ids: List) = unifiedRequester.unipost( buildStandardUrl(baseUrl, deleteByIdRouting), BodyPair(listIdsSerializer, ids), Unit.serializer() diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index cf55b6b1740..7f2ea16eff6 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -1,80 +1,91 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value -import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue -import dev.inmo.micro_utils.ktor.client.uniget +import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import io.ktor.client.HttpClient -import kotlinx.serialization.KSerializer +import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer class KtorReadStandardKeyValueRepo ( - private var baseUrl: String, - private var client: HttpClient = HttpClient(), - private var keySerializer: KSerializer, - private var valueSerializer: KSerializer, - private var valueNullableSerializer: KSerializer, + private val baseUrl: String, + private val unifiedRequester: UnifiedRequester, + private val keySerializer: KSerializer, + private val valueSerializer: KSerializer, + private val valueNullableSerializer: KSerializer ) : ReadStandardKeyValueRepo { - override suspend fun get(k: Key): Value? = client.uniget( + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + valueNullableSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer + ) + + override suspend fun get(k: Key): Value? = unifiedRequester.uniget( buildStandardUrl( baseUrl, getRoute, mapOf( - keyParameterName to keySerializer.encodeUrlQueryValue(k) + keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k) ) ), valueNullableSerializer ) - override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( buildStandardUrl( baseUrl, valuesRoute, mapOf( - reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) + reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed) ) + pagination.asUrlQueryParts ), PaginationResult.serializer(valueSerializer) ) - override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( buildStandardUrl( baseUrl, keysRoute, mapOf( - reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) + reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed) ) + pagination.asUrlQueryParts ), PaginationResult.serializer(keySerializer) ) - override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( buildStandardUrl( baseUrl, keysRoute, mapOf( - valueParameterName to valueSerializer.encodeUrlQueryValue(v), - reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) + valueParameterName to unifiedRequester.encodeUrlQueryValue(valueSerializer, v), + reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed) ) + pagination.asUrlQueryParts ), PaginationResult.serializer(keySerializer) ) - override suspend fun contains(key: Key): Boolean = client.uniget( + override suspend fun contains(key: Key): Boolean = unifiedRequester.uniget( buildStandardUrl( baseUrl, containsRoute, mapOf( - keyParameterName to keySerializer.encodeUrlQueryValue(key) + keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, key) ), ), Boolean.serializer(), ) - override suspend fun count(): Long = client.uniget( + override suspend fun count(): Long = unifiedRequester.uniget( buildStandardUrl( baseUrl, countRoute, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt index 80c028c18fb..423ceb33aad 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt @@ -1,27 +1,39 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value +import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient -import kotlinx.serialization.KSerializer +import kotlinx.serialization.* class KtorStandartKeyValueRepo ( baseUrl: String, baseSubpart: String, - client: HttpClient = HttpClient(), + unifiedRequester: UnifiedRequester, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer ) : StandardKeyValueRepo, ReadStandardKeyValueRepo by KtorReadStandardKeyValueRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer, valueNullableSerializer ), WriteStandardKeyValueRepo by KtorWriteStandardKeyValueRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer - ) \ No newline at end of file + ) { + constructor( + baseUrl: String, + baseSubpart: String, + client: HttpClient = HttpClient(), + keySerializer: KSerializer, + valueSerializer: KSerializer, + valueNullableSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer) +} \ No newline at end of file diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt index 6b423797afc..85fe734fcfc 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt @@ -2,32 +2,45 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* class KtorWriteStandardKeyValueRepo ( private var baseUrl: String, - private var client: HttpClient = HttpClient(), + private var unifiedRequester: UnifiedRequester, private var keySerializer: KSerializer, private var valueSerializer: KSerializer, ) : WriteStandardKeyValueRepo { private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) private val keysListSerializer = ListSerializer(keySerializer) - override val onNewValue: Flow> = client.createStandardWebsocketFlow( + + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer + ) + + override val onNewValue: Flow> = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onNewValueRoute), deserializer = PairSerializer(keySerializer, valueSerializer) ) - override val onValueRemoved: Flow = client.createStandardWebsocketFlow( + override val onValueRemoved: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onValueRemovedRoute), deserializer = keySerializer ) - override suspend fun set(toSet: Map) = client.unipost( + override suspend fun set(toSet: Map) = unifiedRequester.unipost( buildStandardUrl( baseUrl, setRoute @@ -36,7 +49,7 @@ class KtorWriteStandardKeyValueRepo ( Unit.serializer() ) - override suspend fun unset(toUnset: List) = client.unipost( + override suspend fun unset(toUnset: List) = unifiedRequester.unipost( buildStandardUrl( baseUrl, unsetRoute, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt index 0dd46f380e2..4bc426adb6c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt @@ -1,25 +1,37 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many +import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer class KtorOneToManyKeyValueRepo( baseUrl: String, baseSubpart: String, - client: HttpClient, + unifiedRequester: UnifiedRequester, keySerializer: KSerializer, valueSerializer: KSerializer, ) : OneToManyKeyValueRepo, ReadOneToManyKeyValueRepo by KtorReadOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer, ), WriteOneToManyKeyValueRepo by KtorWriteOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer, - ) \ No newline at end of file + ) { + constructor( + baseUrl: String, + baseSubpart: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) +} \ No newline at end of file diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt index ced11c17618..d9d37f53301 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt @@ -1,8 +1,8 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many -import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue -import dev.inmo.micro_utils.ktor.client.uniget +import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.keyParameterName @@ -10,86 +10,95 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import io.ktor.client.HttpClient +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer class KtorReadOneToManyKeyValueRepo ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, - private val valueSerializer: KSerializer, + private val valueSerializer: KSerializer ) : ReadOneToManyKeyValueRepo { private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer) private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer) - override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) + + override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( buildStandardUrl( baseUrl, getRoute, mapOf( - keyParameterName to keySerializer.encodeUrlQueryValue(k), - reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) + keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k), + reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed) ) + pagination.asUrlQueryParts ), paginationValueResultSerializer ) - override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( buildStandardUrl( baseUrl, keysRoute, mapOf( - reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) + reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed) ) + pagination.asUrlQueryParts ), paginationKeyResultSerializer ) - override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( buildStandardUrl( baseUrl, keysRoute, mapOf( - valueParameterName to valueSerializer.encodeUrlQueryValue(v), - reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) + valueParameterName to unifiedRequester.encodeUrlQueryValue(valueSerializer, v), + reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed) ) + pagination.asUrlQueryParts ), paginationKeyResultSerializer ) - override suspend fun contains(k: Key): Boolean = client.uniget( + override suspend fun contains(k: Key): Boolean = unifiedRequester.uniget( buildStandardUrl( baseUrl, containsByKeyRoute, - mapOf(keyParameterName to keySerializer.encodeUrlQueryValue(k)) + mapOf(keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k)) ), Boolean.serializer() ) - override suspend fun contains(k: Key, v: Value): Boolean = client.uniget( + override suspend fun contains(k: Key, v: Value): Boolean = unifiedRequester.uniget( buildStandardUrl( baseUrl, containsByKeyValueRoute, mapOf( - keyParameterName to keySerializer.encodeUrlQueryValue(k), - valueParameterName to valueSerializer.encodeUrlQueryValue(v), + keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k), + valueParameterName to unifiedRequester.encodeUrlQueryValue(valueSerializer, v), ) ), Boolean.serializer() ) - override suspend fun count(k: Key): Long = client.uniget( + override suspend fun count(k: Key): Long = unifiedRequester.uniget( buildStandardUrl( baseUrl, countByKeyRoute, mapOf( - keyParameterName to keySerializer.encodeUrlQueryValue(k) + keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k) ) ), Long.serializer() ) - override suspend fun count(): Long = client.uniget( + override suspend fun count(): Long = unifiedRequester.uniget( buildStandardUrl( baseUrl, countRoute, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt index 0da3137ff1f..b1d1f4b6c0d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt @@ -2,35 +2,48 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* class KtorWriteOneToManyKeyValueRepo ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, private val valueSerializer: KSerializer ) : WriteOneToManyKeyValueRepo { private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) - override val onNewValue: Flow> = client.createStandardWebsocketFlow( + + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer + ) + + override val onNewValue: Flow> = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onNewValueRoute), deserializer = keyValueSerializer ) - override val onValueRemoved: Flow> = client.createStandardWebsocketFlow( + override val onValueRemoved: Flow> = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onValueRemovedRoute), deserializer = keyValueSerializer ) - override val onDataCleared: Flow = client.createStandardWebsocketFlow( + override val onDataCleared: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onDataClearedRoute), deserializer = keySerializer ) - override suspend fun remove(toRemove: Map>) = client.unipost( + override suspend fun remove(toRemove: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, removeRoute, @@ -39,7 +52,7 @@ class KtorWriteOneToManyKeyValueRepo ( Unit.serializer(), ) - override suspend fun add(toAdd: Map>) = client.unipost( + override suspend fun add(toAdd: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, addRoute, @@ -47,7 +60,7 @@ class KtorWriteOneToManyKeyValueRepo ( BodyPair(keyValueMapSerializer, toAdd), Unit.serializer(), ) - override suspend fun clear(k: Key) = client.unipost( + override suspend fun clear(k: Key) = unifiedRequester.unipost( buildStandardUrl( baseUrl, clearRoute, @@ -56,7 +69,7 @@ class KtorWriteOneToManyKeyValueRepo ( Unit.serializer(), ) - override suspend fun set(toSet: Map>) = client.unipost( + override suspend fun set(toSet: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, setRoute, From ab11e28bf7aa0a5a9daf740101c4145ded2d56b6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 27 Nov 2020 14:35:00 +0600 Subject: [PATCH 3/4] UnifiedRouter --- CHANGELOG.md | 4 + .../ktor/client/FlowsWebsockets.kt | 3 +- .../ktor/client/StandardHttpClientGetPost.kt | 2 +- .../micro_utils/ktor/server/FlowsWebsocket.kt | 5 +- .../ktor/server/ServerRoutingShortcuts.kt | 79 +++++++++- .../client/crud/KtorReadStandardCrudRepo.kt | 6 +- .../ktor/client/crud/KtorStandardCrudRepo.kt | 4 +- .../client/crud/KtorWriteStandardCrudRepo.kt | 6 +- .../key_value/KtorReadStandardKeyValueRepo.kt | 5 +- .../key_value/KtorStandartKeyValueRepo.kt | 3 +- .../KtorWriteStandardKeyValueRepo.kt | 6 +- .../one_to_many/KtorOneToManyKeyValueRepo.kt | 4 +- .../KtorReadOneToManyKeyValueRepo.kt | 6 +- .../KtorWriteOneToManyKeyValueRepo.kt | 6 +- .../server/crud/KtorReadStandardCrudRepo.kt | 76 +++++---- .../ktor/server/crud/KtorStandardCrudRepo.kt | 25 ++- .../server/crud/KtorWriteStandardCrudRepo.kt | 104 ++++++++----- .../key_value/KtorStandartKeyValueRepo.kt | 24 ++- .../key_value/KtorStandartReadKeyValueRepo.kt | 105 ++++++++----- .../KtorStandartWriteKeyValueRepo.kt | 77 +++++---- .../ConfigureOneToManyKeyValueRepoRoutes.kt | 25 ++- ...onfigureOneToManyReadKeyValueRepoRoutes.kt | 146 ++++++++++-------- ...nfigureOneToManyWriteKeyValueRepoRoutes.kt | 96 +++++++----- 23 files changed, 536 insertions(+), 281 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1dcd63392b..9deaf346932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,14 @@ * `Ktor` * `Client` * New class `UnifiedRequester` + * `Server` + * New class `UnifiedRouter` * `Repos` * `Ktor` * `Client` * Rewriting of all clients on new `UnifiedRequester` + * `Server` + * Rewriting of all clients on new `UnifiedRouter` ## 0.4.6 diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt index e011d755a85..0a8243f1598 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt @@ -8,7 +8,6 @@ import io.ktor.http.cio.websocket.Frame import io.ktor.http.cio.websocket.readBytes import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.channelFlow -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.DeserializationStrategy /** @@ -66,7 +65,7 @@ inline fun HttpClient.createStandardWebsocketFlow( url: String, crossinline checkReconnection: (Throwable?) -> Boolean = { true }, deserializer: DeserializationStrategy, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) = createStandardWebsocketFlow( url, checkReconnection diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt index b410dc3e664..a87e5ee9cf4 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt @@ -10,7 +10,7 @@ typealias BodyPair = Pair, T> class UnifiedRequester( private val client: HttpClient = HttpClient(), - private val serialFormat: BinaryFormat = standardKtorSerialFormat + private val serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) { suspend fun uniget( url: String, diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt index 7e951faf186..fea1e291c2e 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt @@ -33,10 +33,11 @@ fun Route.includeWebsocketHandling( fun Route.includeWebsocketHandling( suburl: String, flow: Flow, - serializer: SerializationStrategy + serializer: SerializationStrategy, + serialFormat: StandardKtorSerialFormat ) = includeWebsocketHandling( suburl, flow ) { - standardKtorSerialFormat.encodeDefault(serializer, it) + serialFormat.encodeDefault(serializer, it) } diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt index 30fc729ccf4..b020d634803 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt @@ -3,12 +3,87 @@ package dev.inmo.micro_utils.ktor.server import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.ktor.common.* import io.ktor.application.ApplicationCall +import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import io.ktor.request.receive import io.ktor.response.respond import io.ktor.response.respondBytes -import kotlinx.serialization.DeserializationStrategy -import kotlinx.serialization.SerializationStrategy +import io.ktor.routing.Route +import io.ktor.util.pipeline.PipelineContext +import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.* + +class UnifiedRouter( + private val serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + private val serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) { + fun Route.includeWebsocketHandling( + suburl: String, + flow: Flow, + serializer: SerializationStrategy + ) = includeWebsocketHandling(suburl, flow, serializer, serialFormat) + + suspend fun PipelineContext<*, ApplicationCall>.unianswer( + answerSerializer: SerializationStrategy, + answer: T + ) { + call.respondBytes ( + serialFormat.encodeDefault(answerSerializer, answer), + serialFormatContentType + ) + } + + suspend fun PipelineContext<*, ApplicationCall>.uniload( + deserializer: DeserializationStrategy + ) = safely { + serialFormat.decodeDefault( + deserializer, + call.receive() + ) + } + + suspend fun PipelineContext<*, ApplicationCall>.getParameterOrSendError( + field: String + ) = call.parameters[field].also { + if (it == null) { + call.respond(HttpStatusCode.BadRequest, "Request must contains $field") + } + } + + fun PipelineContext<*, ApplicationCall>.getQueryParameter( + field: String + ) = call.request.queryParameters[field] + + suspend fun PipelineContext<*, ApplicationCall>.getQueryParameterOrSendError( + field: String + ) = getQueryParameter(field).also { + if (it == null) { + call.respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") + } + } + + fun PipelineContext<*, ApplicationCall>.decodeUrlQueryValue( + field: String, + deserializer: DeserializationStrategy + ) = getQueryParameter(field) ?.let { + serialFormat.decodeHex( + deserializer, + it + ) + } + + suspend fun PipelineContext<*, ApplicationCall>.decodeUrlQueryValueOrSendError( + field: String, + deserializer: DeserializationStrategy + ) = decodeUrlQueryValue(field, deserializer).also { + if (it == null) { + call.respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") + } + } +} + +val defaultUnifiedRouter = UnifiedRouter() suspend fun ApplicationCall.unianswer( answerSerializer: SerializationStrategy, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index dacc0b8ec64..470249b0039 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -1,13 +1,11 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.* -import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer @@ -26,7 +24,7 @@ class KtorReadStandardCrudRepo ( objectsSerializer: KSerializer, objectsSerializerNullable: KSerializer, idsSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this ( baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt index 801affd5b99..19561cd92eb 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt @@ -1,10 +1,10 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer class KtorStandardCrudRepo ( @@ -39,7 +39,7 @@ class KtorStandardCrudRepo ( objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, idsSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this( baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt index c6d270fa178..71bc3823d92 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt @@ -1,14 +1,12 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.* -import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.UpdatedValuePair import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* @@ -36,7 +34,7 @@ class KtorWriteStandardCrudRepo ( objectsSerializerNullable: KSerializer, inputsSerializer: KSerializer, idsSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this ( baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, inputsSerializer, idsSerializer ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index 7f2ea16eff6..55bdf8f0ad8 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -1,8 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.* -import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* @@ -24,7 +23,7 @@ class KtorReadStandardKeyValueRepo ( keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this ( baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt index 423ceb33aad..c3faa058790 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt @@ -1,6 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient @@ -34,6 +35,6 @@ class KtorStandartKeyValueRepo ( keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer) } \ No newline at end of file diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt index 85fe734fcfc..4d720ee170a 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt @@ -1,13 +1,11 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.* -import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* @@ -25,7 +23,7 @@ class KtorWriteStandardKeyValueRepo ( client: HttpClient, keySerializer: KSerializer, valueSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this ( baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt index 4bc426adb6c..d50c23bb7e8 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt @@ -1,10 +1,10 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer class KtorOneToManyKeyValueRepo( @@ -32,6 +32,6 @@ class KtorOneToManyKeyValueRepo( client: HttpClient, keySerializer: KSerializer, valueSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) } \ No newline at end of file diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt index d9d37f53301..5eb413eda8c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt @@ -1,8 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.* -import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.keyParameterName @@ -10,7 +9,6 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import io.ktor.client.HttpClient -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer @@ -28,7 +26,7 @@ class KtorReadOneToManyKeyValueRepo ( client: HttpClient, keySerializer: KSerializer, valueSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult = unifiedRequester.uniget( diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt index b1d1f4b6c0d..5263c53f83c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt @@ -1,13 +1,11 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.* -import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow -import kotlinx.serialization.BinaryFormat import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* @@ -25,7 +23,7 @@ class KtorWriteOneToManyKeyValueRepo ( client: HttpClient, keySerializer: KSerializer, valueSerializer: KSerializer, - serialFormat: BinaryFormat = standardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this ( baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer ) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt index c270f8e47b9..82cba432cd2 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt @@ -1,12 +1,14 @@ package dev.inmo.micro_utils.repos.ktor.server.crud -import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError -import dev.inmo.micro_utils.ktor.server.unianswer +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.get import kotlinx.serialization.KSerializer @@ -16,47 +18,65 @@ fun Route.configureReadStandardCrudRepoRoutes( originalRepo: ReadStandardCRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, - idsSerializer: KSerializer + idsSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) get(getByPaginationRouting) { - val pagination = call.request.queryParameters.extractPagination + unifiedRouter.apply { + val pagination = call.request.queryParameters.extractPagination - call.unianswer( - paginationResultSerializer, - originalRepo.getByPagination(pagination) - ) + unianswer( + paginationResultSerializer, + originalRepo.getByPagination(pagination) + ) + } } get(getByIdRouting) { - val id = call.decodeUrlQueryValueOrSendError( - "id", - idsSerializer - ) ?: return@get + unifiedRouter.apply { + val id = decodeUrlQueryValueOrSendError( + "id", + idsSerializer + ) ?: return@get - call.unianswer( - objectsNullableSerializer, - originalRepo.getById(id) - ) + unianswer( + objectsNullableSerializer, + originalRepo.getById(id) + ) + } } get(containsRouting) { - val id = call.decodeUrlQueryValueOrSendError( - "id", - idsSerializer - ) ?: return@get + unifiedRouter.apply { + val id = decodeUrlQueryValueOrSendError( + "id", + idsSerializer + ) ?: return@get - call.unianswer( - Boolean.serializer(), - originalRepo.contains(id) - ) + unianswer( + Boolean.serializer(), + originalRepo.contains(id) + ) + } } get(countRouting) { - call.unianswer( - Long.serializer(), - originalRepo.count() - ) + unifiedRouter.apply { + unianswer( + Long.serializer(), + originalRepo.count() + ) + } } } + +inline fun Route.configureReadStandardCrudRepoRoutes( + originalRepo: ReadStandardCRUDRepo, + objectsSerializer: KSerializer, + objectsNullableSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt index f619b9da745..08685719ec8 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt @@ -1,6 +1,11 @@ package dev.inmo.micro_utils.repos.ktor.server.crud +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.server.UnifiedRouter +import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType import dev.inmo.micro_utils.repos.StandardCRUDRepo +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.route import kotlinx.serialization.KSerializer @@ -11,10 +16,24 @@ fun Route.configureStandardCrudRepoRoutes( objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, - idsSerializer: KSerializer + idsSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { route(baseSubpart) { - configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer) - configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer) + configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, unifiedRouter) + configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, unifiedRouter) } } + +fun Route.configureStandardCrudRepoRoutes( + baseSubpart: String, + originalRepo: StandardCRUDRepo, + objectsSerializer: KSerializer, + objectsNullableSerializer: KSerializer, + inputsSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureStandardCrudRepoRoutes( + baseSubpart, originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType) +) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt index b1ed324c201..79f04e84945 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt @@ -1,9 +1,12 @@ package dev.inmo.micro_utils.repos.ktor.server.crud +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.post import kotlinx.serialization.KSerializer @@ -14,7 +17,8 @@ fun Route.configureWriteStandardCrudRepoRoutes( objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, - idsSerializer: KSerializer + idsSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { val listObjectsSerializer = ListSerializer(objectsSerializer) val listInputSerializer = ListSerializer(inputsSerializer) @@ -25,58 +29,80 @@ fun Route.configureWriteStandardCrudRepoRoutes( ) val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer) - includeWebsocketHandling( - newObjectsFlowRouting, - originalRepo.newObjectsFlow, - objectsSerializer - ) - includeWebsocketHandling( - updatedObjectsFlowRouting, - originalRepo.updatedObjectsFlow, - objectsSerializer - ) - includeWebsocketHandling( - deletedObjectsIdsFlowRouting, - originalRepo.deletedObjectsIdsFlow, - idsSerializer - ) + unifiedRouter.apply { + includeWebsocketHandling( + newObjectsFlowRouting, + originalRepo.newObjectsFlow, + objectsSerializer + ) + includeWebsocketHandling( + updatedObjectsFlowRouting, + originalRepo.updatedObjectsFlow, + objectsSerializer + ) + includeWebsocketHandling( + deletedObjectsIdsFlowRouting, + originalRepo.deletedObjectsIdsFlow, + idsSerializer + ) + } post(createRouting) { - call.unianswer( - listObjectsSerializer, - originalRepo.create( - call.uniload(listInputSerializer) + unifiedRouter.apply { + unianswer( + listObjectsSerializer, + originalRepo.create( + uniload(listInputSerializer) + ) ) - ) + } } post(updateRouting) { - val (id, input) = call.uniload(inputUpdateSerializer) - call.unianswer( - objectsNullableSerializer, - originalRepo.update( - id, input + unifiedRouter.apply { + val (id, input) = uniload(inputUpdateSerializer) + unianswer( + objectsNullableSerializer, + originalRepo.update( + id, input + ) ) - ) + } } post(updateManyRouting) { - val updates = call.uniload(listInputUpdateSerializer) - call.unianswer( - listObjectsSerializer, - originalRepo.update( - updates + unifiedRouter.apply { + val updates = uniload(listInputUpdateSerializer) + unianswer( + listObjectsSerializer, + originalRepo.update( + updates + ) ) - ) + } } post(deleteByIdRouting) { - val ids = call.uniload(listIdsSerializer) - call.unianswer( - Unit.serializer(), - originalRepo.deleteById( - ids + unifiedRouter.apply { + val ids = uniload(listIdsSerializer) + unianswer( + Unit.serializer(), + originalRepo.deleteById( + ids + ) ) - ) + } } } + +fun Route.configureWriteStandardCrudRepoRoutes( + originalRepo: WriteStandardCRUDRepo, + objectsSerializer: KSerializer, + objectsNullableSerializer: KSerializer, + inputsSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureWriteStandardCrudRepoRoutes( + originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType) +) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt index b7a5948701a..d2cc8f19f04 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt @@ -1,16 +1,22 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.server.UnifiedRouter +import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType import dev.inmo.micro_utils.repos.StandardKeyValueRepo +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.route import kotlinx.serialization.KSerializer -fun Route.configureStandartKeyValueRepoRoutes( +fun Route.configureStandardKeyValueRepoRoutes( baseSubpart: String, originalRepo: StandardKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { route(baseSubpart) { configureReadStandartKeyValueRepoRoutes( @@ -18,11 +24,23 @@ fun Route.configureStandartKeyValueRepoRoutes( keySerializer, valueSerializer, valueNullableSerializer, + unifiedRouter ) - configureWriteStandartKeyValueRepoRoutes( + configureWriteStandardKeyValueRepoRoutes( originalRepo, keySerializer, valueSerializer, + unifiedRouter ) } -} \ No newline at end of file +} + +fun Route.configureStandartKeyValueRepoRoutes( + baseSubpart: String, + originalRepo: StandardKeyValueRepo, + keySerializer: KSerializer, + valueSerializer: KSerializer, + valueNullableSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureStandardKeyValueRepoRoutes(baseSubpart, originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) \ No newline at end of file diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt index 92990226f65..fd5511914e9 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt @@ -1,5 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination @@ -7,6 +9,7 @@ import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.get import kotlinx.serialization.KSerializer @@ -17,62 +20,84 @@ fun Route.configureReadStandartKeyValueRepoRoutes ( keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { get(getRoute) { - val key = call.decodeUrlQueryValueOrSendError( - keyParameterName, - keySerializer - ) ?: return@get + unifiedRouter.apply { + val key = decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get - call.unianswer( - valueNullableSerializer, - originalRepo.get(key) - ) + unianswer( + valueNullableSerializer, + originalRepo.get(key) + ) + } } get(valuesRoute) { - val parination = call.request.queryParameters.extractPagination; - val reversed = call.decodeUrlQueryValueOrSendError( - reversedParameterName, - Boolean.serializer() - ) ?: return@get + unifiedRouter.apply { + val parination = call.request.queryParameters.extractPagination; + val reversed = decodeUrlQueryValueOrSendError( + reversedParameterName, + Boolean.serializer() + ) ?: return@get - call.unianswer( - PaginationResult.serializer(valueSerializer), - originalRepo.values(parination, reversed) - ) + unianswer( + PaginationResult.serializer(valueSerializer), + originalRepo.values(parination, reversed) + ) + } } get(keysRoute) { - val parination = call.request.queryParameters.extractPagination; - val reversed = call.decodeUrlQueryValueOrSendError( - reversedParameterName, - Boolean.serializer() - ) ?: return@get - val value = call.decodeUrlQueryValue(valueParameterName, valueSerializer) + unifiedRouter.apply { + val parination = call.request.queryParameters.extractPagination; + val reversed = decodeUrlQueryValueOrSendError( + reversedParameterName, + Boolean.serializer() + ) ?: return@get + val value = decodeUrlQueryValue(valueParameterName, valueSerializer) - call.unianswer( - PaginationResult.serializer(keySerializer), - value ?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed) - ) + unianswer( + PaginationResult.serializer(keySerializer), + value?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed) + ) + } } get(containsRoute) { - val key = call.decodeUrlQueryValueOrSendError( - keyParameterName, - keySerializer - ) ?: return@get + unifiedRouter.apply { + val key = decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get - call.unianswer( - Boolean.serializer(), - originalRepo.contains(key) - ) + unianswer( + Boolean.serializer(), + originalRepo.contains(key) + ) + } } get(countRoute) { - call.unianswer( - Long.serializer(), - originalRepo.count() - ) + unifiedRouter.apply { + unianswer( + Long.serializer(), + originalRepo.count() + ) + } } -} \ No newline at end of file +} + +inline fun Route.configureReadStandartKeyValueRepoRoutes ( + originalRepo: ReadStandardKeyValueRepo, + keySerializer: KSerializer, + valueSerializer: KSerializer, + valueNullableSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureReadStandartKeyValueRepoRoutes( + originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType) +) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt index 57a16f20cba..ae288adc189 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt @@ -1,44 +1,61 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* -import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.post import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* +fun Route.configureWriteStandardKeyValueRepoRoutes ( + originalRepo: WriteStandardKeyValueRepo, + keySerializer: KSerializer, + valueSerializer: KSerializer, + unifiedRouter: UnifiedRouter +) { + val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) + val keysListSerializer = ListSerializer(keySerializer) + unifiedRouter.apply { + includeWebsocketHandling( + onNewValueRoute, + originalRepo.onNewValue, + PairSerializer(keySerializer, valueSerializer) + ) + + includeWebsocketHandling( + onValueRemovedRoute, + originalRepo.onValueRemoved, + keySerializer + ) + } + + post(setRoute) { + unifiedRouter.apply { + val toSet = uniload( + keyValueMapSerializer + ) + + unianswer(Unit.serializer(), originalRepo.set(toSet)) + } + } + + post(unsetRoute) { + unifiedRouter.apply { + val toUnset = uniload(keysListSerializer) + + unianswer(Unit.serializer(), originalRepo.unset(toUnset)) + } + } +} + fun Route.configureWriteStandartKeyValueRepoRoutes ( originalRepo: WriteStandardKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, -) { - val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) - val keysListSerializer = ListSerializer(keySerializer) - includeWebsocketHandling( - onNewValueRoute, - originalRepo.onNewValue, - PairSerializer(keySerializer, valueSerializer) - ) - - includeWebsocketHandling( - onValueRemovedRoute, - originalRepo.onValueRemoved, - keySerializer - ) - - post(setRoute) { - val toSet = call.uniload( - keyValueMapSerializer - ) - - call.unianswer(Unit.serializer(), originalRepo.set(toSet)) - } - - post(unsetRoute) { - val toUnset = call.uniload(keysListSerializer) - - call.unianswer(Unit.serializer(), originalRepo.unset(toUnset)) - } -} \ No newline at end of file + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureWriteStandardKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt index c17599baa2b..23a16dba545 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt @@ -1,6 +1,11 @@ package dev.inmo.micro_utils.repos.ktor.server.one_to_many +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat +import dev.inmo.micro_utils.ktor.server.UnifiedRouter +import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.route import kotlinx.serialization.KSerializer @@ -9,10 +14,22 @@ fun Route.configureOneToManyKeyValueRepoRoutes( baseSubpart: String, originalRepo: OneToManyKeyValueRepo, keySerializer: KSerializer, - valueSealizer: KSerializer, + valueSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { route(baseSubpart) { - configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer) - configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer) + configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter) + configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter) } -} \ No newline at end of file +} + +fun Route.configureOneToManyKeyValueRepoRoutes( + baseSubpart: String, + originalRepo: OneToManyKeyValueRepo, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureOneToManyKeyValueRepoRoutes( + baseSubpart, originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType) +) \ No newline at end of file diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt index 0cbcf2a5691..df0743a1d09 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt @@ -1,5 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.server.one_to_many +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination @@ -10,6 +12,7 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.get import kotlinx.serialization.KSerializer @@ -18,89 +21,110 @@ import kotlinx.serialization.builtins.serializer fun Route.configureOneToManyReadKeyValueRepoRoutes( originalRepo: ReadOneToManyKeyValueRepo, keySerializer: KSerializer, - valueSealizer: KSerializer, + valueSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { val paginationKeyResult = PaginationResult.serializer(keySerializer) - val paginationValueResult = PaginationResult.serializer(valueSealizer) + val paginationValueResult = PaginationResult.serializer(valueSerializer) get(getRoute) { - val pagination = call.request.queryParameters.extractPagination - val key = call.decodeUrlQueryValueOrSendError( - keyParameterName, - keySerializer - ) ?: return@get - val reversed = call.decodeUrlQueryValue( - reversedParameterName, - Boolean.serializer() - ) ?: false + unifiedRouter.apply { + val pagination = call.request.queryParameters.extractPagination + val key = decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get + val reversed = decodeUrlQueryValue( + reversedParameterName, + Boolean.serializer() + ) ?: false - call.unianswer( - paginationValueResult, - originalRepo.get(key, pagination, reversed) - ) + unianswer( + paginationValueResult, + originalRepo.get(key, pagination, reversed) + ) + } } get(keysRoute) { - val pagination = call.request.queryParameters.extractPagination - val reversed = call.decodeUrlQueryValue( - reversedParameterName, - Boolean.serializer() - ) ?: false - val value: Value? = call.decodeUrlQueryValue( - valueParameterName, - valueSealizer - ) + unifiedRouter.apply { + val pagination = call.request.queryParameters.extractPagination + val reversed = decodeUrlQueryValue( + reversedParameterName, + Boolean.serializer() + ) ?: false + val value: Value? = decodeUrlQueryValue( + valueParameterName, + valueSerializer + ) - call.unianswer( - paginationKeyResult, - value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed) - ) + unianswer( + paginationKeyResult, + value?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed) + ) + } } get(containsByKeyRoute) { - val key = call.decodeUrlQueryValueOrSendError( - keyParameterName, - keySerializer - ) ?: return@get + unifiedRouter.apply { + val key = decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get - call.unianswer( - Boolean.serializer(), - originalRepo.contains(key) - ) + unianswer( + Boolean.serializer(), + originalRepo.contains(key) + ) + } } get(containsByKeyValueRoute) { - val key = call.decodeUrlQueryValueOrSendError( - keyParameterName, - keySerializer - ) ?: return@get - val value = call.decodeUrlQueryValueOrSendError( - valueParameterName, - valueSealizer - ) ?: return@get + unifiedRouter.apply { + val key = decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get + val value = decodeUrlQueryValueOrSendError( + valueParameterName, + valueSerializer + ) ?: return@get - call.unianswer( - Boolean.serializer(), - originalRepo.contains(key, value) - ) + unianswer( + Boolean.serializer(), + originalRepo.contains(key, value) + ) + } } get(countByKeyRoute) { - val key = call.decodeUrlQueryValueOrSendError( - keyParameterName, - keySerializer - ) ?: return@get + unifiedRouter.apply { + val key = decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get - call.unianswer( - Long.serializer(), - originalRepo.count(key) - ) + unianswer( + Long.serializer(), + originalRepo.count(key) + ) + } } get(countRoute) { - call.unianswer( - Long.serializer(), - originalRepo.count() - ) + unifiedRouter.apply { + unianswer( + Long.serializer(), + originalRepo.count() + ) + } } -} \ No newline at end of file +} + +inline fun Route.configureOneToManyReadKeyValueRepoRoutes( + originalRepo: ReadOneToManyKeyValueRepo, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt index aeb8656a1eb..c9039bb87cb 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt @@ -1,9 +1,12 @@ package dev.inmo.micro_utils.repos.ktor.server.one_to_many +import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat +import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.application.call +import io.ktor.http.ContentType import io.ktor.routing.Route import io.ktor.routing.post import kotlinx.serialization.KSerializer @@ -13,61 +16,78 @@ fun Route.configureOneToManyWriteKeyValueRepoRoutes( originalRepo: WriteOneToManyKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, + unifiedRouter: UnifiedRouter ) { val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) - includeWebsocketHandling( - onNewValueRoute, - originalRepo.onNewValue, - keyValueSerializer - ) - includeWebsocketHandling( - onValueRemovedRoute, - originalRepo.onValueRemoved, - keyValueSerializer - ) - includeWebsocketHandling( - onDataClearedRoute, - originalRepo.onDataCleared, - keySerializer - ) + unifiedRouter.apply { + includeWebsocketHandling( + onNewValueRoute, + originalRepo.onNewValue, + keyValueSerializer + ) + includeWebsocketHandling( + onValueRemovedRoute, + originalRepo.onValueRemoved, + keyValueSerializer + ) + includeWebsocketHandling( + onDataClearedRoute, + originalRepo.onDataCleared, + keySerializer + ) + } post(addRoute) { - val obj = call.uniload(keyValueMapSerializer) + unifiedRouter.apply { + val obj = uniload(keyValueMapSerializer) - call.unianswer( - Unit.serializer(), - originalRepo.add(obj) - ) + unianswer( + Unit.serializer(), + originalRepo.add(obj) + ) + } } post(removeRoute) { - val obj = call.uniload( - keyValueMapSerializer - ) + unifiedRouter.apply { + val obj = uniload(keyValueMapSerializer) - call.unianswer( - Unit.serializer(), - originalRepo.remove(obj), - ) + unianswer( + Unit.serializer(), + originalRepo.remove(obj), + ) + } } post(clearRoute) { - val key = call.uniload(keySerializer) + unifiedRouter.apply { + val key = uniload(keySerializer) - call.unianswer( - Unit.serializer(), - originalRepo.clear(key), - ) + unianswer( + Unit.serializer(), + originalRepo.clear(key), + ) + } } post(setRoute) { - val obj = call.uniload(keyValueMapSerializer) + unifiedRouter.apply { + val obj = uniload(keyValueMapSerializer) - call.unianswer( - Unit.serializer(), - originalRepo.set(obj) - ) + unianswer( + Unit.serializer(), + originalRepo.set(obj) + ) + } } -} \ No newline at end of file +} + +fun Route.configureOneToManyWriteKeyValueRepoRoutes( + originalRepo: WriteOneToManyKeyValueRepo, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, + serialFormatContentType: ContentType = standardKtorSerialFormatContentType +) = configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) From 6cc0eefb3e6e320f655c20bf77c03ced1b508ef4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 27 Nov 2020 17:53:29 +0600 Subject: [PATCH 4/4] upfix of default in includeWebsocketHandling --- .../kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt index fea1e291c2e..ac265170316 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt @@ -34,7 +34,7 @@ fun Route.includeWebsocketHandling( suburl: String, flow: Flow, serializer: SerializationStrategy, - serialFormat: StandardKtorSerialFormat + serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) = includeWebsocketHandling( suburl, flow