UnifiedRequester

This commit is contained in:
InsanusMokrassar 2020-11-27 13:30:02 +06:00
parent 74ae91cba6
commit 26d5f5a5f5
12 changed files with 251 additions and 100 deletions

View File

@ -2,6 +2,14 @@
## 0.4.7
* `Ktor`
* `Client`
* New class `UnifiedRequester`
* `Repos`
* `Ktor`
* `Client`
* Rewriting of all clients on new `UnifiedRequester`
## 0.4.6
* `Common`

View File

@ -8,6 +8,7 @@ import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.readBytes
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.DeserializationStrategy
/**
@ -64,10 +65,11 @@ inline fun <T> HttpClient.createStandardWebsocketFlow(
inline fun <T> HttpClient.createStandardWebsocketFlow(
url: String,
crossinline checkReconnection: (Throwable?) -> Boolean = { true },
deserializer: DeserializationStrategy<T>
deserializer: DeserializationStrategy<T>,
serialFormat: BinaryFormat = standardKtorSerialFormat
) = createStandardWebsocketFlow(
url,
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.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>
class UnifiedRequester(
private val client: HttpClient = HttpClient(),
private val serialFormat: BinaryFormat = 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(
url: String,
resultDeserializer: DeserializationStrategy<ResultType>
) = get<StandardKtorSerialInputData>(
url
).let {
standardKtorSerialFormat.decodeDefault(resultDeserializer, it)
}
) = defaultRequester.uniget(url, resultDeserializer)
fun <T> SerializationStrategy<T>.encodeUrlQueryValue(value: T) = standardKtorSerialFormat.encodeHex(
this,
value
)
fun <T> SerializationStrategy<T>.encodeUrlQueryValue(value: T) = defaultRequester.encodeUrlQueryValue(this, value)
suspend fun <BodyType, ResultType> HttpClient.unipost(
url: String,
bodyInfo: BodyPair<BodyType>,
resultDeserializer: DeserializationStrategy<ResultType>
) = post<StandardKtorSerialInputData>(url) {
body = standardKtorSerialFormat.encodeDefault(bodyInfo.first, bodyInfo.second)
}.let {
standardKtorSerialFormat.decodeDefault(resultDeserializer, it)
}
) = defaultRequester.unipost(url, bodyInfo, resultDeserializer)

View File

@ -1,52 +1,64 @@
package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
import dev.inmo.micro_utils.ktor.client.uniget
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
class KtorReadStandardCrudRepo<ObjectType, IdType> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val unifiedRequester: UnifiedRequester,
private val objectsSerializer: KSerializer<ObjectType>,
private val objectsSerializerNullable: KSerializer<ObjectType?>,
private val idsSerializer: KSerializer<IdType>
) : ReadStandardCRUDRepo<ObjectType, IdType> {
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: BinaryFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer
)
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> = 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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,25 +1,37 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.UnifiedRequester
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer
class KtorOneToManyKeyValueRepo<Key, Value>(
baseUrl: String,
baseSubpart: String,
client: HttpClient,
unifiedRequester: UnifiedRequester,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
) : OneToManyKeyValueRepo<Key, Value>,
ReadOneToManyKeyValueRepo<Key, Value> by KtorReadOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
unifiedRequester,
keySerializer,
valueSerializer,
),
WriteOneToManyKeyValueRepo<Key, Value> by KtorWriteOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
unifiedRequester,
keySerializer,
valueSerializer,
)
) {
constructor(
baseUrl: String,
baseSubpart: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: BinaryFormat = standardKtorSerialFormat
) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer)
}

View File

@ -1,8 +1,8 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
import dev.inmo.micro_utils.ktor.client.uniget
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.keyParameterName
@ -10,86 +10,95 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
class KtorReadOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val unifiedRequester: UnifiedRequester,
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
private val valueSerializer: KSerializer<Value>
) : ReadOneToManyKeyValueRepo<Key, Value> {
private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer)
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: BinaryFormat = standardKtorSerialFormat
) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer)
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> = 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<Key> = client.uniget(
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = 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<Key> = client.uniget(
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = 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,

View File

@ -2,35 +2,48 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.*
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val unifiedRequester: UnifiedRequester,
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>
) : WriteOneToManyKeyValueRepo<Key, Value> {
private val keyValueSerializer = PairSerializer(keySerializer, 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: BinaryFormat = standardKtorSerialFormat
) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer
)
override val onNewValue: Flow<Pair<Key, Value>> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = keyValueSerializer
)
override val onValueRemoved: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
override val onValueRemoved: Flow<Pair<Key, Value>> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute),
deserializer = keyValueSerializer
)
override val onDataCleared: Flow<Key> = client.createStandardWebsocketFlow(
override val onDataCleared: Flow<Key> = unifiedRequester.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute),
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(
baseUrl,
removeRoute,
@ -39,7 +52,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
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(
baseUrl,
addRoute,
@ -47,7 +60,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
BodyPair(keyValueMapSerializer, toAdd),
Unit.serializer(),
)
override suspend fun clear(k: Key) = client.unipost(
override suspend fun clear(k: Key) = unifiedRequester.unipost(
buildStandardUrl(
baseUrl,
clearRoute,
@ -56,7 +69,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
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(
baseUrl,
setRoute,