read klient impl

This commit is contained in:
000Sanya 2020-08-26 12:17:13 +10:00
parent ac7c3a1dc5
commit 4e25d97ba0
3 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,109 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
import com.insanusmokrassar.postssystem.ktor.asUrlQueryParts
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
import com.insanusmokrassar.postssystem.ktor.client.uniget
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.client.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToHexString
class KtorOneToManyReadKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
) : OneToManyReadKeyValueRepo<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(
buildStandardUrl(
baseUrl,
getRoute,
mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k),
reversedParameterName to standardKtorSerialFormat.encodeToHexString(reversed)
) + pagination.asUrlQueryParts
),
paginationValueResultSerializer
)
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget(
buildStandardUrl(
baseUrl,
keysRoute,
mapOf(
reversedParameterName to standardKtorSerialFormat.encodeToHexString(reversed)
) + pagination.asUrlQueryParts
),
paginationKeyResultSerializer
)
override suspend fun contains(k: Key): Boolean = client.uniget(
buildStandardUrl(
baseUrl,
containsByKeyRoute,
mapOf(keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k))
),
Boolean.serializer()
)
override suspend fun contains(k: Key, v: Value): Boolean = client.uniget(
buildStandardUrl(
baseUrl,
containsByKeyValueRoute,
mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k),
valueParameterName to standardKtorSerialFormat.encodeToHexString(valueSerializer, v),
)
),
Boolean.serializer()
)
override suspend fun count(k: Key): Long = client.uniget(
buildStandardUrl(
baseUrl,
countByKeyRoute,
mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k)
)
),
Long.serializer()
)
override suspend fun count(): Long = client.uniget(
buildStandardUrl(
baseUrl,
countRoute,
),
Long.serializer()
)
}
class KtorOneToManyWriteKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
) : OneToManyWriteKeyValueRepo<Key, Value> {
override suspend fun add(k: Key, v: Value) {
TODO("Not yet implemented")
}
override suspend fun remove(k: Key, v: Value) {
TODO("Not yet implemented")
}
override suspend fun clear(k: Key) {
TODO("Not yet implemented")
}
}
class KtorOneToManyKeyValueRepo {
}

View File

@ -0,0 +1,5 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common
const val keyParameterName = "key"
const val valueParameterName = "value"
const val reversedParameterName = "reversed"

View File

@ -0,0 +1,8 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common
const val getRoute = "get"
const val keysRoute = "keys"
const val containsByKeyRoute = "containsByKey"
const val containsByKeyValueRoute = "containsByKeyValue"
const val countByKeyRoute = "countByKey"
const val countRoute = "count"