Merge pull request #24 from InsanusMokrassar/0.4.7

0.4.7
This commit is contained in:
InsanusMokrassar 2020-11-27 19:30:40 +06:00 committed by GitHub
commit 2b6ef8b4ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 766 additions and 358 deletions

View File

@ -1,5 +1,19 @@
# Changelog # 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 ## 0.4.6
* `Common` * `Common`

View File

@ -41,5 +41,5 @@ dokka_version=1.4.0
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.4.6 version=0.4.7
android_code_version=10 android_code_version=11

View File

@ -64,10 +64,11 @@ inline fun <T> HttpClient.createStandardWebsocketFlow(
inline fun <T> HttpClient.createStandardWebsocketFlow( inline fun <T> HttpClient.createStandardWebsocketFlow(
url: String, url: String,
crossinline checkReconnection: (Throwable?) -> Boolean = { true }, crossinline checkReconnection: (Throwable?) -> Boolean = { true },
deserializer: DeserializationStrategy<T> deserializer: DeserializationStrategy<T>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) = createStandardWebsocketFlow( ) = createStandardWebsocketFlow(
url, url,
checkReconnection checkReconnection
) { ) {
standardKtorSerialFormat.decodeDefault(deserializer, it) serialFormat.decodeDefault(deserializer, it)
} }

View File

@ -4,32 +4,61 @@ import dev.inmo.micro_utils.ktor.common.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.request.get import io.ktor.client.request.get
import io.ktor.client.request.post import io.ktor.client.request.post
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.*
import kotlinx.serialization.SerializationStrategy
typealias BodyPair<T> = Pair<SerializationStrategy<T>, T> typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>
class UnifiedRequester(
private val client: HttpClient = HttpClient(),
private val serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) {
suspend fun <ResultType> uniget(
url: String,
resultDeserializer: DeserializationStrategy<ResultType>
): ResultType = client.get<StandardKtorSerialInputData>(
url
).let {
serialFormat.decodeDefault(resultDeserializer, it)
}
fun <T> encodeUrlQueryValue(
serializationStrategy: SerializationStrategy<T>,
value: T
) = serialFormat.encodeHex(
serializationStrategy,
value
)
suspend fun <BodyType, ResultType> unipost(
url: String,
bodyInfo: BodyPair<BodyType>,
resultDeserializer: DeserializationStrategy<ResultType>
) = client.post<StandardKtorSerialInputData>(url) {
body = serialFormat.encodeDefault(bodyInfo.first, bodyInfo.second)
}.let {
serialFormat.decodeDefault(resultDeserializer, it)
}
fun <T> createStandardWebsocketFlow(
url: String,
checkReconnection: (Throwable?) -> Boolean = { true },
deserializer: DeserializationStrategy<T>
) = client.createStandardWebsocketFlow(url, checkReconnection, deserializer, serialFormat)
}
val defaultRequester = UnifiedRequester()
suspend fun <ResultType> HttpClient.uniget( suspend fun <ResultType> HttpClient.uniget(
url: String, url: String,
resultDeserializer: DeserializationStrategy<ResultType> resultDeserializer: DeserializationStrategy<ResultType>
) = get<StandardKtorSerialInputData>( ) = defaultRequester.uniget(url, resultDeserializer)
url
).let {
standardKtorSerialFormat.decodeDefault(resultDeserializer, it)
}
fun <T> SerializationStrategy<T>.encodeUrlQueryValue(value: T) = standardKtorSerialFormat.encodeHex( fun <T> SerializationStrategy<T>.encodeUrlQueryValue(value: T) = defaultRequester.encodeUrlQueryValue(this, value)
this,
value
)
suspend fun <BodyType, ResultType> HttpClient.unipost( suspend fun <BodyType, ResultType> HttpClient.unipost(
url: String, url: String,
bodyInfo: BodyPair<BodyType>, bodyInfo: BodyPair<BodyType>,
resultDeserializer: DeserializationStrategy<ResultType> resultDeserializer: DeserializationStrategy<ResultType>
) = post<StandardKtorSerialInputData>(url) { ) = defaultRequester.unipost(url, bodyInfo, resultDeserializer)
body = standardKtorSerialFormat.encodeDefault(bodyInfo.first, bodyInfo.second)
}.let {
standardKtorSerialFormat.decodeDefault(resultDeserializer, it)
}

View File

@ -33,10 +33,11 @@ fun <T> Route.includeWebsocketHandling(
fun <T> Route.includeWebsocketHandling( fun <T> Route.includeWebsocketHandling(
suburl: String, suburl: String,
flow: Flow<T>, flow: Flow<T>,
serializer: SerializationStrategy<T> serializer: SerializationStrategy<T>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) = includeWebsocketHandling( ) = includeWebsocketHandling(
suburl, suburl,
flow flow
) { ) {
standardKtorSerialFormat.encodeDefault(serializer, it) serialFormat.encodeDefault(serializer, it)
} }

View File

@ -3,12 +3,87 @@ package dev.inmo.micro_utils.ktor.server
import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.common.*
import io.ktor.application.ApplicationCall import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.request.receive import io.ktor.request.receive
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.response.respondBytes import io.ktor.response.respondBytes
import kotlinx.serialization.DeserializationStrategy import io.ktor.routing.Route
import kotlinx.serialization.SerializationStrategy 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 <T> Route.includeWebsocketHandling(
suburl: String,
flow: Flow<T>,
serializer: SerializationStrategy<T>
) = includeWebsocketHandling(suburl, flow, serializer, serialFormat)
suspend fun <T> PipelineContext<*, ApplicationCall>.unianswer(
answerSerializer: SerializationStrategy<T>,
answer: T
) {
call.respondBytes (
serialFormat.encodeDefault(answerSerializer, answer),
serialFormatContentType
)
}
suspend fun <T> PipelineContext<*, ApplicationCall>.uniload(
deserializer: DeserializationStrategy<T>
) = 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 <T> PipelineContext<*, ApplicationCall>.decodeUrlQueryValue(
field: String,
deserializer: DeserializationStrategy<T>
) = getQueryParameter(field) ?.let {
serialFormat.decodeHex(
deserializer,
it
)
}
suspend fun <T> PipelineContext<*, ApplicationCall>.decodeUrlQueryValueOrSendError(
field: String,
deserializer: DeserializationStrategy<T>
) = decodeUrlQueryValue(field, deserializer).also {
if (it == null) {
call.respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
}
}
}
val defaultUnifiedRouter = UnifiedRouter()
suspend fun <T> ApplicationCall.unianswer( suspend fun <T> ApplicationCall.unianswer(
answerSerializer: SerializationStrategy<T>, answerSerializer: SerializationStrategy<T>,

View File

@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.crud package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.client.uniget import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
@ -12,41 +11,52 @@ import kotlinx.serialization.builtins.serializer
class KtorReadStandardCrudRepo<ObjectType, IdType> ( class KtorReadStandardCrudRepo<ObjectType, IdType> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val unifiedRequester: UnifiedRequester,
private val objectsSerializer: KSerializer<ObjectType>, private val objectsSerializer: KSerializer<ObjectType>,
private val objectsSerializerNullable: KSerializer<ObjectType?>, private val objectsSerializerNullable: KSerializer<ObjectType?>,
private val idsSerializer: KSerializer<IdType> private val idsSerializer: KSerializer<IdType>
) : ReadStandardCRUDRepo<ObjectType, IdType> { ) : ReadStandardCRUDRepo<ObjectType, IdType> {
private val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) private val paginationResultSerializer = PaginationResult.serializer(objectsSerializer)
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> = client.uniget( constructor(
baseUrl: String,
client: HttpClient,
objectsSerializer: KSerializer<ObjectType>,
objectsSerializerNullable: KSerializer<ObjectType?>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer
)
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> = unifiedRequester.uniget(
buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts), buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts),
paginationResultSerializer paginationResultSerializer
) )
override suspend fun getById(id: IdType): ObjectType? = client.uniget( override suspend fun getById(id: IdType): ObjectType? = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
getByIdRouting, getByIdRouting,
mapOf( mapOf(
"id" to idsSerializer.encodeUrlQueryValue(id) "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id)
) )
), ),
objectsSerializerNullable objectsSerializerNullable
) )
override suspend fun contains(id: IdType): Boolean = client.uniget( override suspend fun contains(id: IdType): Boolean = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
containsRouting, containsRouting,
mapOf( mapOf(
"id" to idsSerializer.encodeUrlQueryValue(id) "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id)
) )
), ),
Boolean.serializer() Boolean.serializer()
) )
override suspend fun count(): Long = client.uniget( override suspend fun count(): Long = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
countRouting countRouting

View File

@ -1,5 +1,8 @@
package dev.inmo.micro_utils.repos.ktor.client.crud 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 dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -7,7 +10,7 @@ import kotlinx.serialization.KSerializer
class KtorStandardCrudRepo<ObjectType, IdType, InputValue> ( class KtorStandardCrudRepo<ObjectType, IdType, InputValue> (
baseUrl: String, baseUrl: String,
baseSubpart: String, baseSubpart: String,
client: HttpClient, unifiedRequester: UnifiedRequester,
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
@ -15,16 +18,29 @@ class KtorStandardCrudRepo<ObjectType, IdType, InputValue> (
) : StandardCRUDRepo<ObjectType, IdType, InputValue>, ) : StandardCRUDRepo<ObjectType, IdType, InputValue>,
ReadStandardCRUDRepo<ObjectType, IdType> by KtorReadStandardCrudRepo( ReadStandardCRUDRepo<ObjectType, IdType> by KtorReadStandardCrudRepo(
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, unifiedRequester,
objectsSerializer, objectsSerializer,
objectsNullableSerializer, objectsNullableSerializer,
idsSerializer idsSerializer
), ),
WriteStandardCRUDRepo<ObjectType, IdType, InputValue> by KtorWriteStandardCrudRepo( WriteStandardCRUDRepo<ObjectType, IdType, InputValue> by KtorWriteStandardCrudRepo(
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, unifiedRequester,
objectsSerializer, objectsSerializer,
objectsNullableSerializer, objectsNullableSerializer,
inputsSerializer, inputsSerializer,
idsSerializer idsSerializer
) {
constructor(
baseUrl: String,
baseSubpart: String,
client: HttpClient,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this(
baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer
) )
}

View File

@ -1,7 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.crud package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.* 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.UpdatedValuePair
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
@ -12,7 +12,7 @@ import kotlinx.serialization.builtins.*
class KtorWriteStandardCrudRepo<ObjectType, IdType, InputValue> ( class KtorWriteStandardCrudRepo<ObjectType, IdType, InputValue> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val unifiedRequester: UnifiedRequester,
private val objectsSerializer: KSerializer<ObjectType>, private val objectsSerializer: KSerializer<ObjectType>,
private val objectsNullableSerializer: KSerializer<ObjectType?>, private val objectsNullableSerializer: KSerializer<ObjectType?>,
private val inputsSerializer: KSerializer<InputValue>, private val inputsSerializer: KSerializer<InputValue>,
@ -27,39 +27,51 @@ class KtorWriteStandardCrudRepo<ObjectType, IdType, InputValue> (
) )
private val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer) private val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer)
override val newObjectsFlow: Flow<ObjectType> = client.createStandardWebsocketFlow( constructor(
baseUrl: String,
client: HttpClient,
objectsSerializer: KSerializer<ObjectType>,
objectsSerializerNullable: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, inputsSerializer, idsSerializer
)
override val newObjectsFlow: Flow<ObjectType> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, newObjectsFlowRouting), buildStandardUrl(baseUrl, newObjectsFlowRouting),
deserializer = objectsSerializer deserializer = objectsSerializer
) )
override val updatedObjectsFlow: Flow<ObjectType> = client.createStandardWebsocketFlow( override val updatedObjectsFlow: Flow<ObjectType> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, updatedObjectsFlowRouting), buildStandardUrl(baseUrl, updatedObjectsFlowRouting),
deserializer = objectsSerializer deserializer = objectsSerializer
) )
override val deletedObjectsIdsFlow: Flow<IdType> = client.createStandardWebsocketFlow( override val deletedObjectsIdsFlow: Flow<IdType> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting), buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting),
deserializer = idsSerializer deserializer = idsSerializer
) )
override suspend fun create(values: List<InputValue>): List<ObjectType> = client.unipost( override suspend fun create(values: List<InputValue>): List<ObjectType> = unifiedRequester.unipost(
buildStandardUrl(baseUrl, createRouting), buildStandardUrl(baseUrl, createRouting),
BodyPair(listInputSerializer, values), BodyPair(listInputSerializer, values),
listObjectsSerializer 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), buildStandardUrl(baseUrl, updateRouting),
BodyPair(inputUpdateSerializer, id to value), BodyPair(inputUpdateSerializer, id to value),
objectsNullableSerializer objectsNullableSerializer
) )
override suspend fun update(values: List<UpdatedValuePair<IdType, InputValue>>): List<ObjectType> = client.unipost( override suspend fun update(values: List<UpdatedValuePair<IdType, InputValue>>): List<ObjectType> = unifiedRequester.unipost(
buildStandardUrl(baseUrl, updateManyRouting), buildStandardUrl(baseUrl, updateManyRouting),
BodyPair(listInputUpdateSerializer, values), BodyPair(listInputUpdateSerializer, values),
listObjectsSerializer listObjectsSerializer
) )
override suspend fun deleteById(ids: List<IdType>) = client.unipost( override suspend fun deleteById(ids: List<IdType>) = unifiedRequester.unipost(
buildStandardUrl(baseUrl, deleteByIdRouting), buildStandardUrl(baseUrl, deleteByIdRouting),
BodyPair(listIdsSerializer, ids), BodyPair(listIdsSerializer, ids),
Unit.serializer() Unit.serializer()

View File

@ -1,80 +1,90 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value 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.*
import dev.inmo.micro_utils.ktor.client.uniget import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo 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.key_value.*
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
class KtorReadStandardKeyValueRepo<Key, Value> ( class KtorReadStandardKeyValueRepo<Key, Value> (
private var baseUrl: String, private val baseUrl: String,
private var client: HttpClient = HttpClient(), private val unifiedRequester: UnifiedRequester,
private var keySerializer: KSerializer<Key>, private val keySerializer: KSerializer<Key>,
private var valueSerializer: KSerializer<Value>, private val valueSerializer: KSerializer<Value>,
private var valueNullableSerializer: KSerializer<Value?>, private val valueNullableSerializer: KSerializer<Value?>
) : ReadStandardKeyValueRepo<Key, Value> { ) : ReadStandardKeyValueRepo<Key, Value> {
override suspend fun get(k: Key): Value? = client.uniget( constructor(
baseUrl: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
valueNullableSerializer: KSerializer<Value?>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer
)
override suspend fun get(k: Key): Value? = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
getRoute, getRoute,
mapOf( mapOf(
keyParameterName to keySerializer.encodeUrlQueryValue(k) keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k)
) )
), ),
valueNullableSerializer valueNullableSerializer
) )
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = client.uniget( override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
valuesRoute, valuesRoute,
mapOf( mapOf(
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed)
) + pagination.asUrlQueryParts ) + pagination.asUrlQueryParts
), ),
PaginationResult.serializer(valueSerializer) PaginationResult.serializer(valueSerializer)
) )
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget( override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
keysRoute, keysRoute,
mapOf( mapOf(
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed)
) + pagination.asUrlQueryParts ) + pagination.asUrlQueryParts
), ),
PaginationResult.serializer(keySerializer) PaginationResult.serializer(keySerializer)
) )
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget( override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
keysRoute, keysRoute,
mapOf( mapOf(
valueParameterName to valueSerializer.encodeUrlQueryValue(v), valueParameterName to unifiedRequester.encodeUrlQueryValue(valueSerializer, v),
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed)
) + pagination.asUrlQueryParts ) + pagination.asUrlQueryParts
), ),
PaginationResult.serializer(keySerializer) PaginationResult.serializer(keySerializer)
) )
override suspend fun contains(key: Key): Boolean = client.uniget( override suspend fun contains(key: Key): Boolean = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
containsRoute, containsRoute,
mapOf( mapOf(
keyParameterName to keySerializer.encodeUrlQueryValue(key) keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, key)
), ),
), ),
Boolean.serializer(), Boolean.serializer(),
) )
override suspend fun count(): Long = client.uniget( override suspend fun count(): Long = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
countRoute, countRoute,

View File

@ -1,27 +1,40 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value 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 dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.*
class KtorStandartKeyValueRepo<K, V> ( class KtorStandartKeyValueRepo<K, V> (
baseUrl: String, baseUrl: String,
baseSubpart: String, baseSubpart: String,
client: HttpClient = HttpClient(), unifiedRequester: UnifiedRequester,
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?> valueNullableSerializer: KSerializer<V?>
) : StandardKeyValueRepo<K, V>, ) : StandardKeyValueRepo<K, V>,
ReadStandardKeyValueRepo<K, V> by KtorReadStandardKeyValueRepo( ReadStandardKeyValueRepo<K, V> by KtorReadStandardKeyValueRepo(
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, unifiedRequester,
keySerializer, keySerializer,
valueSerializer, valueSerializer,
valueNullableSerializer valueNullableSerializer
), ),
WriteStandardKeyValueRepo<K, V> by KtorWriteStandardKeyValueRepo( WriteStandardKeyValueRepo<K, V> by KtorWriteStandardKeyValueRepo(
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, unifiedRequester,
keySerializer, keySerializer,
valueSerializer valueSerializer
) ) {
constructor(
baseUrl: String,
baseSubpart: String,
client: HttpClient = HttpClient(),
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer)
}

View File

@ -1,7 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.ktor.client.* 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.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
@ -11,23 +11,34 @@ import kotlinx.serialization.builtins.*
class KtorWriteStandardKeyValueRepo<K, V> ( class KtorWriteStandardKeyValueRepo<K, V> (
private var baseUrl: String, private var baseUrl: String,
private var client: HttpClient = HttpClient(), private var unifiedRequester: UnifiedRequester,
private var keySerializer: KSerializer<K>, private var keySerializer: KSerializer<K>,
private var valueSerializer: KSerializer<V>, private var valueSerializer: KSerializer<V>,
) : WriteStandardKeyValueRepo<K, V> { ) : WriteStandardKeyValueRepo<K, V> {
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
private val keysListSerializer = ListSerializer(keySerializer) private val keysListSerializer = ListSerializer(keySerializer)
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
constructor(
baseUrl: String,
client: HttpClient,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer
)
override val onNewValue: Flow<Pair<K, V>> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute), buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = PairSerializer(keySerializer, valueSerializer) deserializer = PairSerializer(keySerializer, valueSerializer)
) )
override val onValueRemoved: Flow<K> = client.createStandardWebsocketFlow( override val onValueRemoved: Flow<K> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute), buildStandardUrl(baseUrl, onValueRemovedRoute),
deserializer = keySerializer deserializer = keySerializer
) )
override suspend fun set(toSet: Map<K, V>) = client.unipost( override suspend fun set(toSet: Map<K, V>) = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
setRoute setRoute
@ -36,7 +47,7 @@ class KtorWriteStandardKeyValueRepo<K, V> (
Unit.serializer() Unit.serializer()
) )
override suspend fun unset(toUnset: List<K>) = client.unipost( override suspend fun unset(toUnset: List<K>) = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
unsetRoute, unsetRoute,

View File

@ -1,5 +1,8 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many 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 dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -7,19 +10,28 @@ import kotlinx.serialization.KSerializer
class KtorOneToManyKeyValueRepo<Key, Value>( class KtorOneToManyKeyValueRepo<Key, Value>(
baseUrl: String, baseUrl: String,
baseSubpart: String, baseSubpart: String,
client: HttpClient, unifiedRequester: UnifiedRequester,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
) : OneToManyKeyValueRepo<Key, Value>, ) : OneToManyKeyValueRepo<Key, Value>,
ReadOneToManyKeyValueRepo<Key, Value> by KtorReadOneToManyKeyValueRepo<Key, Value> ( ReadOneToManyKeyValueRepo<Key, Value> by KtorReadOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, unifiedRequester,
keySerializer, keySerializer,
valueSerializer, valueSerializer,
), ),
WriteOneToManyKeyValueRepo<Key, Value> by KtorWriteOneToManyKeyValueRepo<Key, Value> ( WriteOneToManyKeyValueRepo<Key, Value> by KtorWriteOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, unifiedRequester,
keySerializer, keySerializer,
valueSerializer, valueSerializer,
) ) {
constructor(
baseUrl: String,
baseSubpart: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer)
}

View File

@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many 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.*
import dev.inmo.micro_utils.ktor.client.uniget import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.keyParameterName import dev.inmo.micro_utils.repos.ktor.common.keyParameterName
@ -15,81 +14,89 @@ import kotlinx.serialization.builtins.serializer
class KtorReadOneToManyKeyValueRepo<Key, Value> ( class KtorReadOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val unifiedRequester: UnifiedRequester,
private val keySerializer: KSerializer<Key>, private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>, private val valueSerializer: KSerializer<Value>
) : ReadOneToManyKeyValueRepo<Key, Value> { ) : ReadOneToManyKeyValueRepo<Key, Value> {
private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer) private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer)
private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer) private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer)
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> = client.uniget( constructor(
baseUrl: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer)
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
getRoute, getRoute,
mapOf( mapOf(
keyParameterName to keySerializer.encodeUrlQueryValue(k), keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k),
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed)
) + pagination.asUrlQueryParts ) + pagination.asUrlQueryParts
), ),
paginationValueResultSerializer paginationValueResultSerializer
) )
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget( override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
keysRoute, keysRoute,
mapOf( mapOf(
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed)
) + pagination.asUrlQueryParts ) + pagination.asUrlQueryParts
), ),
paginationKeyResultSerializer paginationKeyResultSerializer
) )
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget( override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
keysRoute, keysRoute,
mapOf( mapOf(
valueParameterName to valueSerializer.encodeUrlQueryValue(v), valueParameterName to unifiedRequester.encodeUrlQueryValue(valueSerializer, v),
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed) reversedParameterName to unifiedRequester.encodeUrlQueryValue(Boolean.serializer(), reversed)
) + pagination.asUrlQueryParts ) + pagination.asUrlQueryParts
), ),
paginationKeyResultSerializer paginationKeyResultSerializer
) )
override suspend fun contains(k: Key): Boolean = client.uniget( override suspend fun contains(k: Key): Boolean = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
containsByKeyRoute, containsByKeyRoute,
mapOf(keyParameterName to keySerializer.encodeUrlQueryValue(k)) mapOf(keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k))
), ),
Boolean.serializer() 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( buildStandardUrl(
baseUrl, baseUrl,
containsByKeyValueRoute, containsByKeyValueRoute,
mapOf( mapOf(
keyParameterName to keySerializer.encodeUrlQueryValue(k), keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k),
valueParameterName to valueSerializer.encodeUrlQueryValue(v), valueParameterName to unifiedRequester.encodeUrlQueryValue(valueSerializer, v),
) )
), ),
Boolean.serializer() Boolean.serializer()
) )
override suspend fun count(k: Key): Long = client.uniget( override suspend fun count(k: Key): Long = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
countByKeyRoute, countByKeyRoute,
mapOf( mapOf(
keyParameterName to keySerializer.encodeUrlQueryValue(k) keyParameterName to unifiedRequester.encodeUrlQueryValue(keySerializer, k)
) )
), ),
Long.serializer() Long.serializer()
) )
override suspend fun count(): Long = client.uniget( override suspend fun count(): Long = unifiedRequester.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
countRoute, countRoute,

View File

@ -1,7 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.* 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.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
@ -11,26 +11,37 @@ import kotlinx.serialization.builtins.*
class KtorWriteOneToManyKeyValueRepo<Key, Value> ( class KtorWriteOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val unifiedRequester: UnifiedRequester,
private val keySerializer: KSerializer<Key>, private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value> private val valueSerializer: KSerializer<Value>
) : WriteOneToManyKeyValueRepo<Key, Value> { ) : WriteOneToManyKeyValueRepo<Key, Value> {
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
constructor(
baseUrl: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer
)
override val onNewValue: Flow<Pair<Key, Value>> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute), buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = keyValueSerializer deserializer = keyValueSerializer
) )
override val onValueRemoved: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow( override val onValueRemoved: Flow<Pair<Key, Value>> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute), buildStandardUrl(baseUrl, onValueRemovedRoute),
deserializer = keyValueSerializer deserializer = keyValueSerializer
) )
override val onDataCleared: Flow<Key> = client.createStandardWebsocketFlow( override val onDataCleared: Flow<Key> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute), buildStandardUrl(baseUrl, onDataClearedRoute),
deserializer = keySerializer deserializer = keySerializer
) )
override suspend fun remove(toRemove: Map<Key, List<Value>>) = client.unipost( override suspend fun remove(toRemove: Map<Key, List<Value>>) = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
removeRoute, removeRoute,
@ -39,7 +50,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
Unit.serializer(), Unit.serializer(),
) )
override suspend fun add(toAdd: Map<Key, List<Value>>) = client.unipost( override suspend fun add(toAdd: Map<Key, List<Value>>) = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
addRoute, addRoute,
@ -47,7 +58,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
BodyPair(keyValueMapSerializer, toAdd), BodyPair(keyValueMapSerializer, toAdd),
Unit.serializer(), Unit.serializer(),
) )
override suspend fun clear(k: Key) = client.unipost( override suspend fun clear(k: Key) = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
clearRoute, clearRoute,
@ -56,7 +67,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
Unit.serializer(), Unit.serializer(),
) )
override suspend fun set(toSet: Map<Key, List<Value>>) = client.unipost( override suspend fun set(toSet: Map<Key, List<Value>>) = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
setRoute, setRoute,

View File

@ -1,12 +1,14 @@
package dev.inmo.micro_utils.repos.ktor.server.crud package dev.inmo.micro_utils.repos.ktor.server.crud
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.unianswer 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.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -16,47 +18,65 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>, originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>,
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
idsSerializer: KSerializer<IdType> idsSerializer: KSerializer<IdType>,
unifiedRouter: UnifiedRouter
) { ) {
val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) val paginationResultSerializer = PaginationResult.serializer(objectsSerializer)
get(getByPaginationRouting) { get(getByPaginationRouting) {
unifiedRouter.apply {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
call.unianswer( unianswer(
paginationResultSerializer, paginationResultSerializer,
originalRepo.getByPagination(pagination) originalRepo.getByPagination(pagination)
) )
} }
}
get(getByIdRouting) { get(getByIdRouting) {
val id = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val id = decodeUrlQueryValueOrSendError(
"id", "id",
idsSerializer idsSerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
objectsNullableSerializer, objectsNullableSerializer,
originalRepo.getById(id) originalRepo.getById(id)
) )
} }
}
get(containsRouting) { get(containsRouting) {
val id = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val id = decodeUrlQueryValueOrSendError(
"id", "id",
idsSerializer idsSerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(id) originalRepo.contains(id)
) )
} }
}
get(countRouting) { get(countRouting) {
call.unianswer( unifiedRouter.apply {
unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count() originalRepo.count()
) )
} }
}
} }
inline fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,6 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.server.crud 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 dev.inmo.micro_utils.repos.StandardCRUDRepo
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -11,10 +16,24 @@ fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes(
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType> idsSerializer: KSerializer<IdType>,
unifiedRouter: UnifiedRouter
) { ) {
route(baseSubpart) { route(baseSubpart) {
configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer) configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, unifiedRouter)
configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer) configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, unifiedRouter)
} }
} }
fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes(
baseSubpart: String,
originalRepo: StandardCRUDRepo<ObjectType, IdType, InputValue>,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureStandardCrudRepoRoutes(
baseSubpart, originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,9 +1,12 @@
package dev.inmo.micro_utils.repos.ktor.server.crud 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.ktor.server.*
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -14,7 +17,8 @@ fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType> idsSerializer: KSerializer<IdType>,
unifiedRouter: UnifiedRouter
) { ) {
val listObjectsSerializer = ListSerializer(objectsSerializer) val listObjectsSerializer = ListSerializer(objectsSerializer)
val listInputSerializer = ListSerializer(inputsSerializer) val listInputSerializer = ListSerializer(inputsSerializer)
@ -25,6 +29,7 @@ fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
) )
val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer) val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer)
unifiedRouter.apply {
includeWebsocketHandling( includeWebsocketHandling(
newObjectsFlowRouting, newObjectsFlowRouting,
originalRepo.newObjectsFlow, originalRepo.newObjectsFlow,
@ -40,43 +45,64 @@ fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
originalRepo.deletedObjectsIdsFlow, originalRepo.deletedObjectsIdsFlow,
idsSerializer idsSerializer
) )
}
post(createRouting) { post(createRouting) {
call.unianswer( unifiedRouter.apply {
unianswer(
listObjectsSerializer, listObjectsSerializer,
originalRepo.create( originalRepo.create(
call.uniload(listInputSerializer) uniload(listInputSerializer)
) )
) )
} }
}
post(updateRouting) { post(updateRouting) {
val (id, input) = call.uniload(inputUpdateSerializer) unifiedRouter.apply {
call.unianswer( val (id, input) = uniload(inputUpdateSerializer)
unianswer(
objectsNullableSerializer, objectsNullableSerializer,
originalRepo.update( originalRepo.update(
id, input id, input
) )
) )
} }
}
post(updateManyRouting) { post(updateManyRouting) {
val updates = call.uniload(listInputUpdateSerializer) unifiedRouter.apply {
call.unianswer( val updates = uniload(listInputUpdateSerializer)
unianswer(
listObjectsSerializer, listObjectsSerializer,
originalRepo.update( originalRepo.update(
updates updates
) )
) )
} }
}
post(deleteByIdRouting) { post(deleteByIdRouting) {
val ids = call.uniload(listIdsSerializer) unifiedRouter.apply {
call.unianswer( val ids = uniload(listIdsSerializer)
unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.deleteById( originalRepo.deleteById(
ids ids
) )
) )
} }
}
} }
fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
originalRepo: WriteStandardCRUDRepo<ObjectType, IdType, InputValue>,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureWriteStandardCrudRepoRoutes(
originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,16 +1,22 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value 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 dev.inmo.micro_utils.repos.StandardKeyValueRepo
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
fun <K, V> Route.configureStandartKeyValueRepoRoutes( fun <K, V> Route.configureStandardKeyValueRepoRoutes(
baseSubpart: String, baseSubpart: String,
originalRepo: StandardKeyValueRepo<K, V>, originalRepo: StandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>, valueNullableSerializer: KSerializer<V?>,
unifiedRouter: UnifiedRouter
) { ) {
route(baseSubpart) { route(baseSubpart) {
configureReadStandartKeyValueRepoRoutes( configureReadStandartKeyValueRepoRoutes(
@ -18,11 +24,23 @@ fun <K, V> Route.configureStandartKeyValueRepoRoutes(
keySerializer, keySerializer,
valueSerializer, valueSerializer,
valueNullableSerializer, valueNullableSerializer,
unifiedRouter
) )
configureWriteStandartKeyValueRepoRoutes( configureWriteStandardKeyValueRepoRoutes(
originalRepo, originalRepo,
keySerializer, keySerializer,
valueSerializer, valueSerializer,
unifiedRouter
) )
} }
} }
fun <K, V> Route.configureStandartKeyValueRepoRoutes(
baseSubpart: String,
originalRepo: StandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureStandardKeyValueRepoRoutes(baseSubpart, originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,5 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value 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.ktor.server.*
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination 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.key_value.*
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -17,62 +20,84 @@ fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>, valueNullableSerializer: KSerializer<V?>,
unifiedRouter: UnifiedRouter
) { ) {
get(getRoute) { get(getRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
valueNullableSerializer, valueNullableSerializer,
originalRepo.get(key) originalRepo.get(key)
) )
} }
}
get(valuesRoute) { get(valuesRoute) {
unifiedRouter.apply {
val parination = call.request.queryParameters.extractPagination; val parination = call.request.queryParameters.extractPagination;
val reversed = call.decodeUrlQueryValueOrSendError( val reversed = decodeUrlQueryValueOrSendError(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
PaginationResult.serializer(valueSerializer), PaginationResult.serializer(valueSerializer),
originalRepo.values(parination, reversed) originalRepo.values(parination, reversed)
) )
} }
}
get(keysRoute) { get(keysRoute) {
unifiedRouter.apply {
val parination = call.request.queryParameters.extractPagination; val parination = call.request.queryParameters.extractPagination;
val reversed = call.decodeUrlQueryValueOrSendError( val reversed = decodeUrlQueryValueOrSendError(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: return@get ) ?: return@get
val value = call.decodeUrlQueryValue(valueParameterName, valueSerializer) val value = decodeUrlQueryValue(valueParameterName, valueSerializer)
call.unianswer( unianswer(
PaginationResult.serializer(keySerializer), PaginationResult.serializer(keySerializer),
value ?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed) value?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed)
) )
} }
}
get(containsRoute) { get(containsRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(key) originalRepo.contains(key)
) )
} }
}
get(countRoute) { get(countRoute) {
call.unianswer( unifiedRouter.apply {
unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count() originalRepo.count()
) )
} }
}
} }
inline fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
originalRepo: ReadStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureReadStandartKeyValueRepoRoutes(
originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,21 +1,25 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value 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.ktor.server.*
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.* 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.Route
import io.ktor.routing.post import io.ktor.routing.post
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes ( fun <K, V> Route.configureWriteStandardKeyValueRepoRoutes (
originalRepo: WriteStandardKeyValueRepo<K, V>, originalRepo: WriteStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
unifiedRouter: UnifiedRouter
) { ) {
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
val keysListSerializer = ListSerializer(keySerializer) val keysListSerializer = ListSerializer(keySerializer)
unifiedRouter.apply {
includeWebsocketHandling( includeWebsocketHandling(
onNewValueRoute, onNewValueRoute,
originalRepo.onNewValue, originalRepo.onNewValue,
@ -27,18 +31,31 @@ fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
originalRepo.onValueRemoved, originalRepo.onValueRemoved,
keySerializer keySerializer
) )
}
post(setRoute) { post(setRoute) {
val toSet = call.uniload( unifiedRouter.apply {
val toSet = uniload(
keyValueMapSerializer keyValueMapSerializer
) )
call.unianswer(Unit.serializer(), originalRepo.set(toSet)) unianswer(Unit.serializer(), originalRepo.set(toSet))
}
} }
post(unsetRoute) { post(unsetRoute) {
val toUnset = call.uniload(keysListSerializer) unifiedRouter.apply {
val toUnset = uniload(keysListSerializer)
call.unianswer(Unit.serializer(), originalRepo.unset(toUnset)) unianswer(Unit.serializer(), originalRepo.unset(toUnset))
}
} }
} }
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
originalRepo: WriteStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureWriteStandardKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,6 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many 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 dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -9,10 +14,22 @@ fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
baseSubpart: String, baseSubpart: String,
originalRepo: OneToManyKeyValueRepo<Key, Value>, originalRepo: OneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
unifiedRouter: UnifiedRouter
) { ) {
route(baseSubpart) { route(baseSubpart) {
configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer) configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter)
configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer) configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter)
} }
} }
fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
baseSubpart: String,
originalRepo: OneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureOneToManyKeyValueRepoRoutes(
baseSubpart, originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,5 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many 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.ktor.server.*
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination 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.valueParameterName
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -18,89 +21,110 @@ import kotlinx.serialization.builtins.serializer
fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes( fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
originalRepo: ReadOneToManyKeyValueRepo<Key, Value>, originalRepo: ReadOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
unifiedRouter: UnifiedRouter
) { ) {
val paginationKeyResult = PaginationResult.serializer(keySerializer) val paginationKeyResult = PaginationResult.serializer(keySerializer)
val paginationValueResult = PaginationResult.serializer(valueSealizer) val paginationValueResult = PaginationResult.serializer(valueSerializer)
get(getRoute) { get(getRoute) {
unifiedRouter.apply {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
val key = call.decodeUrlQueryValueOrSendError( val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
val reversed = call.decodeUrlQueryValue( val reversed = decodeUrlQueryValue(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: false ) ?: false
call.unianswer( unianswer(
paginationValueResult, paginationValueResult,
originalRepo.get(key, pagination, reversed) originalRepo.get(key, pagination, reversed)
) )
} }
}
get(keysRoute) { get(keysRoute) {
unifiedRouter.apply {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
val reversed = call.decodeUrlQueryValue( val reversed = decodeUrlQueryValue(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: false ) ?: false
val value: Value? = call.decodeUrlQueryValue( val value: Value? = decodeUrlQueryValue(
valueParameterName, valueParameterName,
valueSealizer valueSerializer
) )
call.unianswer( unianswer(
paginationKeyResult, paginationKeyResult,
value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed) value?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed)
) )
} }
}
get(containsByKeyRoute) { get(containsByKeyRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(key) originalRepo.contains(key)
) )
} }
}
get(containsByKeyValueRoute) { get(containsByKeyValueRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
val value = call.decodeUrlQueryValueOrSendError( val value = decodeUrlQueryValueOrSendError(
valueParameterName, valueParameterName,
valueSealizer valueSerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(key, value) originalRepo.contains(key, value)
) )
} }
}
get(countByKeyRoute) { get(countByKeyRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count(key) originalRepo.count(key)
) )
} }
}
get(countRoute) { get(countRoute) {
call.unianswer( unifiedRouter.apply {
unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count() originalRepo.count()
) )
} }
}
} }
inline fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
originalRepo: ReadOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,9 +1,12 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many 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.ktor.server.*
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -13,10 +16,12 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>, originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
unifiedRouter: UnifiedRouter
) { ) {
val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
unifiedRouter.apply {
includeWebsocketHandling( includeWebsocketHandling(
onNewValueRoute, onNewValueRoute,
originalRepo.onNewValue, originalRepo.onNewValue,
@ -32,42 +37,57 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo.onDataCleared, originalRepo.onDataCleared,
keySerializer keySerializer
) )
}
post(addRoute) { post(addRoute) {
val obj = call.uniload(keyValueMapSerializer) unifiedRouter.apply {
val obj = uniload(keyValueMapSerializer)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.add(obj) originalRepo.add(obj)
) )
} }
}
post(removeRoute) { post(removeRoute) {
val obj = call.uniload( unifiedRouter.apply {
keyValueMapSerializer val obj = uniload(keyValueMapSerializer)
)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.remove(obj), originalRepo.remove(obj),
) )
} }
}
post(clearRoute) { post(clearRoute) {
val key = call.uniload(keySerializer) unifiedRouter.apply {
val key = uniload(keySerializer)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.clear(key), originalRepo.clear(key),
) )
} }
}
post(setRoute) { post(setRoute) {
val obj = call.uniload(keyValueMapSerializer) unifiedRouter.apply {
val obj = uniload(keyValueMapSerializer)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.set(obj) originalRepo.set(obj)
) )
} }
}
} }
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))