diff --git a/CHANGELOG.md b/CHANGELOG.md index fb248dad33a..9deaf346932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## 0.4.7 + +* `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 * `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 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..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 @@ -64,10 +64,11 @@ inline fun HttpClient.createStandardWebsocketFlow( inline fun HttpClient.createStandardWebsocketFlow( url: String, crossinline checkReconnection: (Throwable?) -> Boolean = { true }, - deserializer: DeserializationStrategy + deserializer: DeserializationStrategy, + serialFormat: StandardKtorSerialFormat = 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..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 @@ -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: StandardKtorSerialFormat = 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/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..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 @@ -33,10 +33,11 @@ fun Route.includeWebsocketHandling( fun Route.includeWebsocketHandling( suburl: String, flow: Flow, - serializer: SerializationStrategy + serializer: SerializationStrategy, + serialFormat: StandardKtorSerialFormat = 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 0647ffcb641..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,8 +1,7 @@ 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.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.client.* +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.* @@ -12,41 +11,52 @@ 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: StandardKtorSerialFormat = 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..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,5 +1,8 @@ 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.KSerializer @@ -7,7 +10,7 @@ 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: 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 730829dfcde..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,7 +1,7 @@ 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.* import dev.inmo.micro_utils.repos.UpdatedValuePair import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* @@ -12,7 +12,7 @@ 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 +27,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: StandardKtorSerialFormat = 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..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,80 +1,90 @@ 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.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.client.* +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.* 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: StandardKtorSerialFormat = 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..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,27 +1,40 @@ 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 -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: 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 6b423797afc..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,7 +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.* import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.HttpClient @@ -11,23 +11,34 @@ 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: StandardKtorSerialFormat = 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 +47,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..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,5 +1,8 @@ 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.KSerializer @@ -7,19 +10,28 @@ 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: 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 ced11c17618..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.encodeUrlQueryValue -import dev.inmo.micro_utils.ktor.client.uniget -import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.micro_utils.ktor.client.* +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 @@ -15,81 +14,89 @@ 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: StandardKtorSerialFormat = 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..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,7 +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.* import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient @@ -11,26 +11,37 @@ 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: StandardKtorSerialFormat = 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 +50,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 +58,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 +67,7 @@ class KtorWriteOneToManyKeyValueRepo ( Unit.serializer(), ) - override suspend fun set(toSet: Map>) = client.unipost( + override suspend fun set(toSet: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, setRoute, 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))