write klient impl

This commit is contained in:
000Sanya 2020-08-26 12:35:19 +10:00
parent 4e25d97ba0
commit 89bef924be
3 changed files with 63 additions and 13 deletions

View File

@ -2,10 +2,13 @@ package com.insanusmokrassar.postssystem.utils.repos.ktor.client
import com.insanusmokrassar.postssystem.ktor.asUrlQueryParts import com.insanusmokrassar.postssystem.ktor.asUrlQueryParts
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
import com.insanusmokrassar.postssystem.ktor.client.BodyPair
import com.insanusmokrassar.postssystem.ktor.client.uniget import com.insanusmokrassar.postssystem.ktor.client.uniget
import com.insanusmokrassar.postssystem.ktor.client.unipost
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.* import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
@ -72,7 +75,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
baseUrl, baseUrl,
countByKeyRoute, countByKeyRoute,
mapOf( mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k) keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k) // TODO: Леша, придумай короче запись, типа keySerializer.toHexString(k)
) )
), ),
Long.serializer() Long.serializer()
@ -91,19 +94,53 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
class KtorOneToManyWriteKeyValueRepo<Key, Value> ( class KtorOneToManyWriteKeyValueRepo<Key, Value> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
) : OneToManyWriteKeyValueRepo<Key, Value> { ) : OneToManyWriteKeyValueRepo<Key, Value> {
override suspend fun add(k: Key, v: Value) { override suspend fun add(k: Key, v: Value) = client.unipost(
TODO("Not yet implemented") buildStandardUrl(
} baseUrl,
addRoute,
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
Unit.serializer(),
)
override suspend fun remove(k: Key, v: Value) { override suspend fun remove(k: Key, v: Value) = client.unipost(
TODO("Not yet implemented") buildStandardUrl(
} baseUrl,
removeRoute,
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
Unit.serializer(),
)
override suspend fun clear(k: Key) { override suspend fun clear(k: Key) = client.unipost(
TODO("Not yet implemented") buildStandardUrl(
} baseUrl,
clearRoute,
),
BodyPair(keySerializer, k),
Unit.serializer(),
)
} }
class KtorOneToManyKeyValueRepo { class KtorOneToManyKeyValueRepo<Key, Value>(
} baseUrl: String,
baseSubpart: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
) : OneToManyKeyValueRepo<Key, Value>,
OneToManyReadKeyValueRepo<Key, Value> by KtorOneToManyReadKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
keySerializer,
valueSerializer,
),
OneToManyWriteKeyValueRepo<Key, Value> by KtorOneToManyWriteKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
keySerializer,
valueSerializer,
)

View File

@ -0,0 +1,9 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common
import kotlinx.serialization.Serializable
@Serializable
data class KeyValuePostObject<Key, Value>(
val key: Key,
val value: Value
)

View File

@ -5,4 +5,8 @@ const val keysRoute = "keys"
const val containsByKeyRoute = "containsByKey" const val containsByKeyRoute = "containsByKey"
const val containsByKeyValueRoute = "containsByKeyValue" const val containsByKeyValueRoute = "containsByKeyValue"
const val countByKeyRoute = "countByKey" const val countByKeyRoute = "countByKey"
const val countRoute = "count" const val countRoute = "count"
const val addRoute = "add"
const val removeRoute = "remove"
const val clearRoute = "clear"