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 ## 0.4.7
* `Ktor`
* `Client`
* New class `UnifiedRequester`
* `Repos`
* `Ktor`
* `Client`
* Rewriting of all clients on new `UnifiedRequester`
## 0.4.6 ## 0.4.6
* `Common` * `Common`

View File

@ -8,6 +8,7 @@ import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.readBytes import io.ktor.http.cio.websocket.readBytes
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.channelFlow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
/** /**
@ -64,10 +65,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: BinaryFormat = 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: 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( 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

@ -1,52 +1,64 @@
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.buildStandardUrl 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.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.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer 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: 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), 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,13 +1,16 @@
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.repos.* import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer 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: 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.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl 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.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.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* 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 +29,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: BinaryFormat = 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,91 @@
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.buildStandardUrl 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.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: BinaryFormat = 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,39 @@
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.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: 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.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl 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.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
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* 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: BinaryFormat = 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 +49,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,25 +1,37 @@
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.repos.* import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer 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: 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 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.buildStandardUrl 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.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
@ -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.reversedParameterName
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.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer 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: BinaryFormat = 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

@ -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.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl 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.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
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* 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: BinaryFormat = 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 +52,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 +60,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 +69,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,