diff --git a/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt b/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt index 77e38e75..2fbb2133 100644 --- a/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt +++ b/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt @@ -4,8 +4,7 @@ import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat 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> @@ -18,6 +17,11 @@ suspend fun HttpClient.uniget( standardKtorSerialFormat.decodeFromByteArray(resultDeserializer, it) } +fun SerializationStrategy.encodeUrlQueryValue(value: T) = standardKtorSerialFormat.encodeToHexString( + this, + value +) + suspend fun HttpClient.unipost( url: String, bodyInfo: BodyPair, diff --git a/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt b/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt deleted file mode 100644 index eb7cb1d3..00000000 --- a/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.insanusmokrassar.postssystem.ktor - -import kotlinx.serialization.* - -fun T.toHex(with: SerializationStrategy) = standardKtorSerialFormat.encodeToHexString(with, this) -fun String.fromHex(with: DeserializationStrategy): T = standardKtorSerialFormat.decodeFromHexString(with, this) diff --git a/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt b/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt index 78336b61..7aacd5af 100644 --- a/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt +++ b/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt @@ -1,14 +1,12 @@ package com.insanusmokrassar.postssystem.ktor.server -import com.insanusmokrassar.postssystem.ktor.fromHex import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import io.ktor.application.ApplicationCall import io.ktor.http.HttpStatusCode import io.ktor.response.respond import io.ktor.response.respondBytes import io.ktor.util.toByteArray -import kotlinx.serialization.DeserializationStrategy -import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.* suspend fun ApplicationCall.unianswer( answerSerializer: SerializationStrategy, @@ -27,18 +25,6 @@ suspend fun ApplicationCall.uniload( request.receiveChannel().toByteArray() ) -fun ApplicationCall.uniloadFromQuery( - parameterName: String, - deserializer: DeserializationStrategy -): T? = getQueryParameter(parameterName) ?.fromHex(deserializer) - -suspend fun ApplicationCall.uniloadFromQueryOrSendError( - parameterName: String, - deserializer: DeserializationStrategy -): T? = uniloadFromQuery(parameterName, deserializer) ?: null.also { - respond(HttpStatusCode.BadRequest, "Request query parameters must contains $parameterName") -} - suspend fun ApplicationCall.getParameterOrSendError( field: String ) = parameters[field].also { @@ -58,3 +44,22 @@ suspend fun ApplicationCall.getQueryParameterOrSendError( respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") } } + +fun ApplicationCall.decodeUrlQueryValue( + field: String, + deserializer: DeserializationStrategy +) = getQueryParameter(field) ?.let { + standardKtorSerialFormat.decodeFromHexString( + deserializer, + it + ) +} + +fun ApplicationCall.decodeUrlQueryValueOrSendError( + field: String, + deserializer: DeserializationStrategy +) = decodeUrlQueryValue(field, deserializer).also { + if (it == null) { + respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") + } +} diff --git a/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 511dd3a2..65be8c49 100644 --- a/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -1,6 +1,7 @@ package com.insanusmokrassar.postssystem.utils.repos.ktor.client.crud import com.insanusmokrassar.postssystem.ktor.* +import com.insanusmokrassar.postssystem.ktor.client.encodeUrlQueryValue import com.insanusmokrassar.postssystem.ktor.client.uniget import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult @@ -29,7 +30,7 @@ class KtorReadStandardCrudRepo ( baseUrl, getByIdRouting, mapOf( - "id" to id.toHex(idsSerializer) + "id" to idsSerializer.encodeUrlQueryValue(id) ) ), objectsSerializerNullable @@ -40,7 +41,7 @@ class KtorReadStandardCrudRepo ( baseUrl, containsRouting, mapOf( - "id" to id.toHex(idsSerializer) + "id" to idsSerializer.encodeUrlQueryValue(id) ) ), Boolean.serializer() diff --git a/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt b/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt index bbdc19cc..6e20f166 100644 --- a/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt +++ b/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt @@ -28,7 +28,7 @@ fun Route.configureReadStandardCrudRepoRoutes( } get(getByIdRouting) { - val id = call.uniloadFromQueryOrSendError( + val id = call.decodeUrlQueryValueOrSendError( "id", idsSerializer ) ?: return@get @@ -40,7 +40,7 @@ fun Route.configureReadStandardCrudRepoRoutes( } get(containsRouting) { - val id = call.uniloadFromQueryOrSendError( + val id = call.decodeUrlQueryValueOrSendError( "id", idsSerializer ) ?: return@get diff --git a/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt b/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt index 5f402ca7..1a48c7f8 100644 --- a/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt +++ b/utils/repos/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt @@ -22,11 +22,11 @@ fun Route.configureOneToManyReadKeyValueRepoRoutes( get(getRoute) { val pagination = call.request.queryParameters.extractPagination - val key = call.uniloadFromQueryOrSendError( + val key = call.decodeUrlQueryValueOrSendError( keyParameterName, keySerializer ) ?: return@get - val reversed = call.uniloadFromQuery( + val reversed = call.decodeUrlQueryValue( reversedParameterName, Boolean.serializer() ) ?: false @@ -39,7 +39,7 @@ fun Route.configureOneToManyReadKeyValueRepoRoutes( get(keysRoute) { val pagination = call.request.queryParameters.extractPagination - val reversed = call.uniloadFromQuery( + val reversed = call.decodeUrlQueryValue( reversedParameterName, Boolean.serializer() ) ?: false @@ -51,10 +51,10 @@ fun Route.configureOneToManyReadKeyValueRepoRoutes( } get(containsByKeyRoute) { - val key = standardKtorSerialFormat.decodeFromHexString( - keySerializer, - call.getQueryParameterOrSendError(keyParameterName) ?: return@get - ) + val key = call.decodeUrlQueryValueOrSendError( + keyParameterName, + keySerializer + ) ?: return@get call.unianswer( Boolean.serializer(), @@ -63,11 +63,11 @@ fun Route.configureOneToManyReadKeyValueRepoRoutes( } get(containsByKeyValueRoute) { - val key = call.uniloadFromQueryOrSendError( + val key = call.decodeUrlQueryValueOrSendError( keyParameterName, keySerializer ) ?: return@get - val value = call.uniloadFromQueryOrSendError( + val value = call.decodeUrlQueryValueOrSendError( valueParameterName, valueSealizer ) ?: return@get @@ -79,7 +79,7 @@ fun Route.configureOneToManyReadKeyValueRepoRoutes( } get(countByKeyRoute) { - val key = call.uniloadFromQueryOrSendError( + val key = call.decodeUrlQueryValueOrSendError( keyParameterName, keySerializer ) ?: return@get