From 26d5f5a5f59c2ca17a550be443903bb8d578f3bc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 27 Nov 2020 13:30:02 +0600 Subject: [PATCH] UnifiedRequester --- CHANGELOG.md | 8 +++ .../ktor/client/FlowsWebsockets.kt | 6 +- .../ktor/client/StandardHttpClientGetPost.kt | 61 ++++++++++++++----- .../client/crud/KtorReadStandardCrudRepo.kt | 30 ++++++--- .../ktor/client/crud/KtorStandardCrudRepo.kt | 22 ++++++- .../client/crud/KtorWriteStandardCrudRepo.kt | 30 ++++++--- .../key_value/KtorReadStandardKeyValueRepo.kt | 51 ++++++++++------ .../key_value/KtorStandartKeyValueRepo.kt | 22 +++++-- .../KtorWriteStandardKeyValueRepo.kt | 23 +++++-- .../one_to_many/KtorOneToManyKeyValueRepo.kt | 20 ++++-- .../KtorReadOneToManyKeyValueRepo.kt | 49 +++++++++------ .../KtorWriteOneToManyKeyValueRepo.kt | 29 ++++++--- 12 files changed, 251 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f2315b557a..d1dcd63392b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt index 92bb32dffd2..e011d755a85 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt @@ -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 HttpClient.createStandardWebsocketFlow( inline fun HttpClient.createStandardWebsocketFlow( url: String, crossinline checkReconnection: (Throwable?) -> Boolean = { true }, - deserializer: DeserializationStrategy + deserializer: DeserializationStrategy, + serialFormat: BinaryFormat = standardKtorSerialFormat ) = createStandardWebsocketFlow( url, checkReconnection ) { - standardKtorSerialFormat.decodeDefault(deserializer, it) + serialFormat.decodeDefault(deserializer, it) } diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt index d5d79b41c25..b410dc3e664 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/StandardHttpClientGetPost.kt @@ -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 = Pair, T> +class UnifiedRequester( + private val client: HttpClient = HttpClient(), + private val serialFormat: BinaryFormat = standardKtorSerialFormat +) { + suspend fun uniget( + url: String, + resultDeserializer: DeserializationStrategy + ): ResultType = client.get( + url + ).let { + serialFormat.decodeDefault(resultDeserializer, it) + } + + + fun encodeUrlQueryValue( + serializationStrategy: SerializationStrategy, + value: T + ) = serialFormat.encodeHex( + serializationStrategy, + value + ) + + suspend fun unipost( + url: String, + bodyInfo: BodyPair, + resultDeserializer: DeserializationStrategy + ) = client.post(url) { + body = serialFormat.encodeDefault(bodyInfo.first, bodyInfo.second) + }.let { + serialFormat.decodeDefault(resultDeserializer, it) + } + + fun createStandardWebsocketFlow( + url: String, + checkReconnection: (Throwable?) -> Boolean = { true }, + deserializer: DeserializationStrategy + ) = client.createStandardWebsocketFlow(url, checkReconnection, deserializer, serialFormat) +} + +val defaultRequester = UnifiedRequester() + suspend fun HttpClient.uniget( url: String, resultDeserializer: DeserializationStrategy -) = get( - url -).let { - standardKtorSerialFormat.decodeDefault(resultDeserializer, it) -} +) = defaultRequester.uniget(url, resultDeserializer) -fun SerializationStrategy.encodeUrlQueryValue(value: T) = standardKtorSerialFormat.encodeHex( - this, - value -) +fun SerializationStrategy.encodeUrlQueryValue(value: T) = defaultRequester.encodeUrlQueryValue(this, value) suspend fun HttpClient.unipost( url: String, bodyInfo: BodyPair, resultDeserializer: DeserializationStrategy -) = post(url) { - body = standardKtorSerialFormat.encodeDefault(bodyInfo.first, bodyInfo.second) -}.let { - standardKtorSerialFormat.decodeDefault(resultDeserializer, it) -} +) = defaultRequester.unipost(url, bodyInfo, resultDeserializer) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 0647ffcb641..dacc0b8ec64 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -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 ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val objectsSerializer: KSerializer, private val objectsSerializerNullable: KSerializer, private val idsSerializer: KSerializer ) : ReadStandardCRUDRepo { private val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) - override suspend fun getByPagination(pagination: Pagination): PaginationResult = client.uniget( + constructor( + baseUrl: String, + client: HttpClient, + objectsSerializer: KSerializer, + objectsSerializerNullable: KSerializer, + idsSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer + ) + + override suspend fun getByPagination(pagination: Pagination): PaginationResult = 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 diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt index 97d8a0abbab..801affd5b99 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt @@ -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 ( baseUrl: String, baseSubpart: String, - client: HttpClient, + unifiedRequester: UnifiedRequester, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, @@ -15,16 +18,29 @@ class KtorStandardCrudRepo ( ) : StandardCRUDRepo, ReadStandardCRUDRepo by KtorReadStandardCrudRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, objectsSerializer, objectsNullableSerializer, idsSerializer ), WriteStandardCRUDRepo by KtorWriteStandardCrudRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer + ) { + constructor( + baseUrl: String, + baseSubpart: String, + client: HttpClient, + objectsSerializer: KSerializer, + objectsNullableSerializer: KSerializer, + inputsSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this( + baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer ) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt index 730829dfcde..c6d270fa178 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt @@ -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 ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val objectsSerializer: KSerializer, private val objectsNullableSerializer: KSerializer, private val inputsSerializer: KSerializer, @@ -27,39 +29,51 @@ class KtorWriteStandardCrudRepo ( ) private val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer) - override val newObjectsFlow: Flow = client.createStandardWebsocketFlow( + constructor( + baseUrl: String, + client: HttpClient, + objectsSerializer: KSerializer, + objectsSerializerNullable: KSerializer, + inputsSerializer: KSerializer, + idsSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, inputsSerializer, idsSerializer + ) + + override val newObjectsFlow: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, newObjectsFlowRouting), deserializer = objectsSerializer ) - override val updatedObjectsFlow: Flow = client.createStandardWebsocketFlow( + override val updatedObjectsFlow: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, updatedObjectsFlowRouting), deserializer = objectsSerializer ) - override val deletedObjectsIdsFlow: Flow = client.createStandardWebsocketFlow( + override val deletedObjectsIdsFlow: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting), deserializer = idsSerializer ) - override suspend fun create(values: List): List = client.unipost( + override suspend fun create(values: List): List = 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>): List = client.unipost( + override suspend fun update(values: List>): List = unifiedRequester.unipost( buildStandardUrl(baseUrl, updateManyRouting), BodyPair(listInputUpdateSerializer, values), listObjectsSerializer ) - override suspend fun deleteById(ids: List) = client.unipost( + override suspend fun deleteById(ids: List) = unifiedRequester.unipost( buildStandardUrl(baseUrl, deleteByIdRouting), BodyPair(listIdsSerializer, ids), Unit.serializer() diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index cf55b6b1740..7f2ea16eff6 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -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 ( - private var baseUrl: String, - private var client: HttpClient = HttpClient(), - private var keySerializer: KSerializer, - private var valueSerializer: KSerializer, - private var valueNullableSerializer: KSerializer, + private val baseUrl: String, + private val unifiedRequester: UnifiedRequester, + private val keySerializer: KSerializer, + private val valueSerializer: KSerializer, + private val valueNullableSerializer: KSerializer ) : ReadStandardKeyValueRepo { - override suspend fun get(k: Key): Value? = client.uniget( + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + valueNullableSerializer: KSerializer, + 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 = client.uniget( + override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult = 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 = client.uniget( + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = 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 = client.uniget( + override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = 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, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt index 80c028c18fb..423ceb33aad 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt @@ -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 ( baseUrl: String, baseSubpart: String, - client: HttpClient = HttpClient(), + unifiedRequester: UnifiedRequester, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer ) : StandardKeyValueRepo, ReadStandardKeyValueRepo by KtorReadStandardKeyValueRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer, valueNullableSerializer ), WriteStandardKeyValueRepo by KtorWriteStandardKeyValueRepo( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer - ) \ No newline at end of file + ) { + constructor( + baseUrl: String, + baseSubpart: String, + client: HttpClient = HttpClient(), + keySerializer: KSerializer, + valueSerializer: KSerializer, + valueNullableSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer) +} \ No newline at end of file diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt index 6b423797afc..85fe734fcfc 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt @@ -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 ( private var baseUrl: String, - private var client: HttpClient = HttpClient(), + private var unifiedRequester: UnifiedRequester, private var keySerializer: KSerializer, private var valueSerializer: KSerializer, ) : WriteStandardKeyValueRepo { private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) private val keysListSerializer = ListSerializer(keySerializer) - override val onNewValue: Flow> = client.createStandardWebsocketFlow( + + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer + ) + + override val onNewValue: Flow> = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onNewValueRoute), deserializer = PairSerializer(keySerializer, valueSerializer) ) - override val onValueRemoved: Flow = client.createStandardWebsocketFlow( + override val onValueRemoved: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onValueRemovedRoute), deserializer = keySerializer ) - override suspend fun set(toSet: Map) = client.unipost( + override suspend fun set(toSet: Map) = unifiedRequester.unipost( buildStandardUrl( baseUrl, setRoute @@ -36,7 +49,7 @@ class KtorWriteStandardKeyValueRepo ( Unit.serializer() ) - override suspend fun unset(toUnset: List) = client.unipost( + override suspend fun unset(toUnset: List) = unifiedRequester.unipost( buildStandardUrl( baseUrl, unsetRoute, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt index 0dd46f380e2..4bc426adb6c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt @@ -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( baseUrl: String, baseSubpart: String, - client: HttpClient, + unifiedRequester: UnifiedRequester, keySerializer: KSerializer, valueSerializer: KSerializer, ) : OneToManyKeyValueRepo, ReadOneToManyKeyValueRepo by KtorReadOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer, ), WriteOneToManyKeyValueRepo by KtorWriteOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", - client, + unifiedRequester, keySerializer, valueSerializer, - ) \ No newline at end of file + ) { + constructor( + baseUrl: String, + baseSubpart: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) +} \ No newline at end of file diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt index ced11c17618..d9d37f53301 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt @@ -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 ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, - private val valueSerializer: KSerializer, + private val valueSerializer: KSerializer ) : ReadOneToManyKeyValueRepo { private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer) private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer) - override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult = client.uniget( + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) + + override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult = 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 = client.uniget( + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = 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 = client.uniget( + override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = 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, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt index 0da3137ff1f..b1d1f4b6c0d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt @@ -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 ( private val baseUrl: String, - private val client: HttpClient = HttpClient(), + private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, private val valueSerializer: KSerializer ) : WriteOneToManyKeyValueRepo { private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) - override val onNewValue: Flow> = client.createStandardWebsocketFlow( + + constructor( + baseUrl: String, + client: HttpClient, + keySerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: BinaryFormat = standardKtorSerialFormat + ) : this ( + baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer + ) + + override val onNewValue: Flow> = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onNewValueRoute), deserializer = keyValueSerializer ) - override val onValueRemoved: Flow> = client.createStandardWebsocketFlow( + override val onValueRemoved: Flow> = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onValueRemovedRoute), deserializer = keyValueSerializer ) - override val onDataCleared: Flow = client.createStandardWebsocketFlow( + override val onDataCleared: Flow = unifiedRequester.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onDataClearedRoute), deserializer = keySerializer ) - override suspend fun remove(toRemove: Map>) = client.unipost( + override suspend fun remove(toRemove: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, removeRoute, @@ -39,7 +52,7 @@ class KtorWriteOneToManyKeyValueRepo ( Unit.serializer(), ) - override suspend fun add(toAdd: Map>) = client.unipost( + override suspend fun add(toAdd: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, addRoute, @@ -47,7 +60,7 @@ class KtorWriteOneToManyKeyValueRepo ( 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 ( Unit.serializer(), ) - override suspend fun set(toSet: Map>) = client.unipost( + override suspend fun set(toSet: Map>) = unifiedRequester.unipost( buildStandardUrl( baseUrl, setRoute,