mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-18 05:40:25 +00:00
complete improvements in repos ktor parts
This commit is contained in:
@@ -4,6 +4,7 @@ import dev.inmo.micro_utils.ktor.client.*
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.countRouting
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.idParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
|
@@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.countRouting
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.idParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
|
@@ -11,11 +11,7 @@ import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import io.ktor.util.reflect.typeInfo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.*
|
||||
|
||||
class KtorWriteStandardCrudRepoClient<ObjectType, IdType, InputValue> (
|
||||
private val baseUrl: String,
|
||||
@@ -50,7 +46,7 @@ class KtorWriteStandardCrudRepoClient<ObjectType, IdType, InputValue> (
|
||||
buildStandardUrl(baseUrl, deleteByIdRouting)
|
||||
) {
|
||||
deleteByIdSetup(ids)
|
||||
}.status
|
||||
}.throwOnUnsuccess { "Unable to delete $ids" }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@@ -4,8 +4,12 @@ import dev.inmo.micro_utils.ktor.client.*
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.containsRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.countRoute
|
||||
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.key_value.keyParameterName
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.reversedParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
|
@@ -4,9 +4,8 @@ import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.containsRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.containsByKeyRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
@@ -83,7 +82,7 @@ class KtorReadStandardKeyValueRepoClient<Key, Value>(
|
||||
override suspend fun count(): Long = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
countRouting
|
||||
dev.inmo.micro_utils.repos.ktor.common.countRoute
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
|
@@ -1,21 +1,17 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.key_value
|
||||
|
||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
||||
import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.http.*
|
||||
import io.ktor.util.InternalAPI
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import io.ktor.util.reflect.typeInfo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.*
|
||||
|
||||
class KtorWriteStandardKeyValueRepoClient<Key, Value>(
|
||||
private val baseUrl: String,
|
||||
@@ -35,7 +31,7 @@ class KtorWriteStandardKeyValueRepoClient<Key, Value>(
|
||||
body = toUnset
|
||||
bodyType = objectsListTypeInfo
|
||||
contentType(contentType)
|
||||
}.status
|
||||
}.throwOnUnsuccess { "Unable to unset data with values $toUnset" }
|
||||
}
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
@@ -46,7 +42,7 @@ class KtorWriteStandardKeyValueRepoClient<Key, Value>(
|
||||
body = toUnset
|
||||
bodyType = idsListTypeInfo
|
||||
contentType(contentType)
|
||||
}.status
|
||||
}.throwOnUnsuccess { "Unable to unset $toUnset" }
|
||||
}
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
@@ -57,7 +53,7 @@ class KtorWriteStandardKeyValueRepoClient<Key, Value>(
|
||||
body = toSet
|
||||
bodyType = idsToObjectsMapTypeInfo
|
||||
contentType(contentType)
|
||||
}.status
|
||||
}.throwOnUnsuccess { "Unable to set $toSet" }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@@ -0,0 +1,159 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
||||
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.http.*
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import io.ktor.util.reflect.typeInfo
|
||||
import kotlinx.serialization.*
|
||||
|
||||
class KtorReadStandardKeyValuesRepoClient<Key, Value>(
|
||||
private val baseUrl: String,
|
||||
private val httpClient: HttpClient,
|
||||
private val contentType: ContentType,
|
||||
private val paginationResultValuesTypeInfo: TypeInfo,
|
||||
private val paginationResultKeysTypeInfo: TypeInfo,
|
||||
private val keySerializer: suspend (Key) -> String,
|
||||
private val valueSerializer: suspend (Value) -> String
|
||||
) : ReadOneToManyKeyValueRepo<Key, Value> {
|
||||
override suspend fun get(
|
||||
k: Key,
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<Value> = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
getRoute,
|
||||
pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()) + (keyParameterName to keySerializer(k))
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body(paginationResultValuesTypeInfo)
|
||||
|
||||
override suspend fun keys(
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<Key> = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
keysRoute,
|
||||
pagination.asUrlQueryParts + (reversedParameterName to reversed.toString())
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body(paginationResultKeysTypeInfo)
|
||||
|
||||
override suspend fun keys(
|
||||
v: Value,
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<Key> = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
keysRoute,
|
||||
pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()) + (valueParameterName to valueSerializer(v))
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body(paginationResultKeysTypeInfo)
|
||||
|
||||
override suspend fun contains(k: Key): Boolean = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
containsRoute,
|
||||
keyParameterName to keySerializer(k)
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body()
|
||||
|
||||
override suspend fun contains(k: Key, v: Value): Boolean = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
containsRoute,
|
||||
keyParameterName to keySerializer(k),
|
||||
valueParameterName to valueSerializer(v)
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body()
|
||||
|
||||
override suspend fun count(): Long = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
countRouting
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body()
|
||||
|
||||
override suspend fun count(k: Key): Long = httpClient.get(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
countRouting,
|
||||
keyParameterName to keySerializer(k),
|
||||
)
|
||||
) {
|
||||
contentType(contentType)
|
||||
}.body()
|
||||
}
|
||||
|
||||
inline fun <reified Key, reified Value> KtorReadStandardKeyValuesRepoClient(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline keySerializer: suspend (Key) -> String,
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorReadStandardKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
typeInfo<PaginationResult<Value>>(),
|
||||
typeInfo<PaginationResult<Key>>(),
|
||||
keySerializer,
|
||||
valueSerializer
|
||||
)
|
||||
|
||||
inline fun <reified Key, reified Value> KtorReadStandardKeyValuesRepoClient(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
idsSerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
serialFormat: StringFormat,
|
||||
contentType: ContentType,
|
||||
) = KtorReadStandardKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
{
|
||||
serialFormat.encodeToString(idsSerializer, it).encodeURLQueryComponent()
|
||||
}
|
||||
) {
|
||||
serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent()
|
||||
}
|
||||
|
||||
inline fun <reified Key, reified Value> KtorReadStandardKeyValuesRepoClient(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
idsSerializer: KSerializer<Key>,
|
||||
valuesSerializer: KSerializer<Value>,
|
||||
serialFormat: BinaryFormat,
|
||||
contentType: ContentType,
|
||||
) = KtorReadStandardKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
{
|
||||
serialFormat.encodeHex(idsSerializer, it)
|
||||
}
|
||||
) {
|
||||
serialFormat.encodeHex(valuesSerializer, it)
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
||||
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.encodeURLQueryComponent
|
||||
import kotlinx.serialization.*
|
||||
|
||||
class KtorStandardKeyValuesRepoClient<Key, Value> (
|
||||
readDelegate: ReadOneToManyKeyValueRepo<Key, Value>,
|
||||
writeDelegate: WriteOneToManyKeyValueRepo<Key, Value>
|
||||
) : OneToManyKeyValueRepo<Key, Value> by DelegateBasedOneToManyKeyValueRepo(
|
||||
readDelegate,
|
||||
writeDelegate
|
||||
) {
|
||||
companion object {
|
||||
inline operator fun <reified Key : Any, reified Value : Any> invoke(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline keySerializer: suspend (Key) -> String,
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorStandardKeyValuesRepoClient(
|
||||
KtorReadStandardKeyValuesRepoClient(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
keySerializer,
|
||||
valueSerializer
|
||||
),
|
||||
KtorWriteStandardKeyValuesRepoClient(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType
|
||||
)
|
||||
)
|
||||
inline operator fun <reified Key : Any, reified Value : Any> invoke(
|
||||
baseUrl: String,
|
||||
subpart: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline keySerializer: suspend (Key) -> String,
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorStandardKeyValuesRepoClient(
|
||||
buildStandardUrl(baseUrl, subpart),
|
||||
httpClient,
|
||||
contentType,
|
||||
keySerializer,
|
||||
valueSerializer
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified Key : Any, reified Value : Any> KtorStandardKeyValuesRepoClient(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
keySerializer: SerializationStrategy<Key>,
|
||||
valueSerializer: SerializationStrategy<Value>,
|
||||
serialFormat: StringFormat,
|
||||
) = KtorStandardKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
{
|
||||
serialFormat.encodeToString(keySerializer, it).encodeURLQueryComponent()
|
||||
}
|
||||
) {
|
||||
serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent()
|
||||
}
|
||||
|
||||
inline fun <reified Key : Any, reified Value : Any> KtorStandardKeyValuesRepoClient(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
keySerializer: SerializationStrategy<Key>,
|
||||
valueSerializer: SerializationStrategy<Value>,
|
||||
serialFormat: BinaryFormat,
|
||||
) = KtorStandardKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
{
|
||||
serialFormat.encodeHex(keySerializer, it)
|
||||
}
|
||||
) {
|
||||
serialFormat.encodeHex(valueSerializer, it)
|
||||
}
|
@@ -0,0 +1,106 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
||||
|
||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
||||
import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.http.*
|
||||
import io.ktor.util.InternalAPI
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import io.ktor.util.reflect.typeInfo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class KtorWriteStandardKeyValuesRepoClient<Key : Any, Value : Any>(
|
||||
private val baseUrl: String,
|
||||
private val httpClient: HttpClient,
|
||||
private val contentType: ContentType,
|
||||
override val onNewValue: Flow<Pair<Key, Value>>,
|
||||
override val onValueRemoved: Flow<Pair<Key, Value>>,
|
||||
override val onDataCleared: Flow<Key>,
|
||||
private val keyTypeInfo: TypeInfo,
|
||||
private val valueTypeInfo: TypeInfo,
|
||||
private val keyToValuesMapTypeInfo: TypeInfo
|
||||
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||
httpClient.post(
|
||||
buildStandardUrl(baseUrl, addRoute)
|
||||
) {
|
||||
body = toAdd
|
||||
bodyType = keyToValuesMapTypeInfo
|
||||
contentType(contentType)
|
||||
}.throwOnUnsuccess { "Unable to add $toAdd" }
|
||||
}
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||
httpClient.post(
|
||||
buildStandardUrl(baseUrl, removeRoute)
|
||||
) {
|
||||
body = toRemove
|
||||
bodyType = keyToValuesMapTypeInfo
|
||||
contentType(contentType)
|
||||
}.throwOnUnsuccess { "Unable to remove $toRemove" }
|
||||
}
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
override suspend fun clear(k: Key) {
|
||||
httpClient.post(
|
||||
buildStandardUrl(baseUrl, clearRoute)
|
||||
) {
|
||||
body = k
|
||||
bodyType = keyTypeInfo
|
||||
contentType(contentType)
|
||||
}.throwOnUnsuccess { "Unable to clear data with key $k" }
|
||||
}
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
override suspend fun clearWithValue(v: Value) {
|
||||
httpClient.post(
|
||||
buildStandardUrl(baseUrl, clearWithValueRoute)
|
||||
) {
|
||||
body = v
|
||||
bodyType = valueTypeInfo
|
||||
contentType(contentType)
|
||||
}.throwOnUnsuccess { "Unable to clear data with value $v" }
|
||||
}
|
||||
|
||||
@OptIn(InternalAPI::class)
|
||||
override suspend fun set(toSet: Map<Key, List<Value>>) {
|
||||
httpClient.post(
|
||||
buildStandardUrl(baseUrl, setRoute)
|
||||
) {
|
||||
body = toSet
|
||||
bodyType = keyToValuesMapTypeInfo
|
||||
contentType(contentType)
|
||||
}.throwOnUnsuccess { "Unable to set data $toSet" }
|
||||
}
|
||||
|
||||
companion object {
|
||||
inline operator fun <reified Key : Any, reified Value : Any> invoke(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType
|
||||
) = KtorWriteStandardKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
typeInfo<Key>(),
|
||||
typeInfo<Value>(),
|
||||
typeInfo<Map<Key, List<Value>>>()
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user