commonize url query parameters encoding/decoding
This commit is contained in:
parent
00068a064f
commit
4cc4d3d4ec
@ -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<T> = Pair<SerializationStrategy<T>, T>
|
||||
|
||||
@ -18,6 +17,11 @@ suspend fun <ResultType> HttpClient.uniget(
|
||||
standardKtorSerialFormat.decodeFromByteArray(resultDeserializer, it)
|
||||
}
|
||||
|
||||
fun <T> SerializationStrategy<T>.encodeUrlQueryValue(value: T) = standardKtorSerialFormat.encodeToHexString(
|
||||
this,
|
||||
value
|
||||
)
|
||||
|
||||
suspend fun <BodyType, ResultType> HttpClient.unipost(
|
||||
url: String,
|
||||
bodyInfo: BodyPair<BodyType>,
|
||||
|
@ -1,6 +0,0 @@
|
||||
package com.insanusmokrassar.postssystem.ktor
|
||||
|
||||
import kotlinx.serialization.*
|
||||
|
||||
fun <T> T.toHex(with: SerializationStrategy<T>) = standardKtorSerialFormat.encodeToHexString(with, this)
|
||||
fun <T> String.fromHex(with: DeserializationStrategy<T>): T = standardKtorSerialFormat.decodeFromHexString(with, this)
|
@ -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 <T> ApplicationCall.unianswer(
|
||||
answerSerializer: SerializationStrategy<T>,
|
||||
@ -27,18 +25,6 @@ suspend fun <T> ApplicationCall.uniload(
|
||||
request.receiveChannel().toByteArray()
|
||||
)
|
||||
|
||||
fun <T> ApplicationCall.uniloadFromQuery(
|
||||
parameterName: String,
|
||||
deserializer: DeserializationStrategy<T>
|
||||
): T? = getQueryParameter(parameterName) ?.fromHex(deserializer)
|
||||
|
||||
suspend fun <T> ApplicationCall.uniloadFromQueryOrSendError(
|
||||
parameterName: String,
|
||||
deserializer: DeserializationStrategy<T>
|
||||
): 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 <T> ApplicationCall.decodeUrlQueryValue(
|
||||
field: String,
|
||||
deserializer: DeserializationStrategy<T>
|
||||
) = getQueryParameter(field) ?.let {
|
||||
standardKtorSerialFormat.decodeFromHexString(
|
||||
deserializer,
|
||||
it
|
||||
)
|
||||
}
|
||||
|
||||
fun <T> ApplicationCall.decodeUrlQueryValueOrSendError(
|
||||
field: String,
|
||||
deserializer: DeserializationStrategy<T>
|
||||
) = decodeUrlQueryValue(field, deserializer).also {
|
||||
if (it == null) {
|
||||
respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
|
||||
}
|
||||
}
|
||||
|
@ -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<ObjectType, IdType> (
|
||||
baseUrl,
|
||||
getByIdRouting,
|
||||
mapOf(
|
||||
"id" to id.toHex(idsSerializer)
|
||||
"id" to idsSerializer.encodeUrlQueryValue(id)
|
||||
)
|
||||
),
|
||||
objectsSerializerNullable
|
||||
@ -40,7 +41,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
|
||||
baseUrl,
|
||||
containsRouting,
|
||||
mapOf(
|
||||
"id" to id.toHex(idsSerializer)
|
||||
"id" to idsSerializer.encodeUrlQueryValue(id)
|
||||
)
|
||||
),
|
||||
Boolean.serializer()
|
||||
|
@ -28,7 +28,7 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
|
||||
}
|
||||
|
||||
get(getByIdRouting) {
|
||||
val id = call.uniloadFromQueryOrSendError(
|
||||
val id = call.decodeUrlQueryValueOrSendError(
|
||||
"id",
|
||||
idsSerializer
|
||||
) ?: return@get
|
||||
@ -40,7 +40,7 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
|
||||
}
|
||||
|
||||
get(containsRouting) {
|
||||
val id = call.uniloadFromQueryOrSendError(
|
||||
val id = call.decodeUrlQueryValueOrSendError(
|
||||
"id",
|
||||
idsSerializer
|
||||
) ?: return@get
|
||||
|
@ -22,11 +22,11 @@ fun <Key, Value> 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 <Key, Value> 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 <Key, Value> 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 <Key, Value> 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 <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
||||
}
|
||||
|
||||
get(countByKeyRoute) {
|
||||
val key = call.uniloadFromQueryOrSendError(
|
||||
val key = call.decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
|
Loading…
Reference in New Issue
Block a user