MicroUtils/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt

143 lines
5.1 KiB
Kotlin
Raw Normal View History

2022-06-04 10:52:01 +00:00
package dev.inmo.micro_utils.repos.ktor.client.key.values
2023-01-14 07:15:59 +00:00
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.repos.*
2023-01-14 07:15:59 +00:00
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.onDataClearedRoute
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.onNewValueRoute
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.onValueRemovedRoute
import io.ktor.client.HttpClient
import io.ktor.http.ContentType
import io.ktor.http.encodeURLQueryComponent
2023-01-14 07:15:59 +00:00
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.*
2022-06-04 09:00:23 +00:00
class KtorKeyValuesRepoClient<Key, Value> (
readDelegate: ReadKeyValuesRepo<Key, Value>,
writeDelegate: WriteKeyValuesRepo<Key, Value>
) : KeyValuesRepo<Key, Value> by DelegateBasedKeyValuesRepo(
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,
2023-01-14 07:15:59 +00:00
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
),
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute),
),
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute),
),
noinline valueSerializer: suspend (Value) -> String
2022-06-04 09:00:23 +00:00
) = KtorKeyValuesRepoClient(
KtorReadKeyValuesRepoClient(
baseUrl,
httpClient,
contentType,
keySerializer,
valueSerializer
),
2022-06-04 09:00:23 +00:00
KtorWriteKeyValuesRepoClient(
baseUrl,
httpClient,
2023-01-14 07:15:59 +00:00
contentType,
onNewValue,
onValueRemoved,
onDataCleared
)
)
inline operator fun <reified Key : Any, reified Value : Any> invoke(
baseUrl: String,
subpart: String,
httpClient: HttpClient,
contentType: ContentType,
noinline keySerializer: suspend (Key) -> String,
2023-01-14 07:15:59 +00:00
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
),
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute),
),
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute),
),
noinline valueSerializer: suspend (Value) -> String
2022-06-04 09:00:23 +00:00
) = KtorKeyValuesRepoClient(
buildStandardUrl(baseUrl, subpart),
httpClient,
contentType,
keySerializer,
2023-01-14 07:15:59 +00:00
onNewValue,
onValueRemoved,
onDataCleared,
valueSerializer
)
}
}
2022-06-04 09:00:23 +00:00
inline fun <reified Key : Any, reified Value : Any> KtorKeyValuesRepoClient(
baseUrl: String,
httpClient: HttpClient,
contentType: ContentType,
keySerializer: SerializationStrategy<Key>,
valueSerializer: SerializationStrategy<Value>,
serialFormat: StringFormat,
2023-01-14 07:15:59 +00:00
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
),
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute),
),
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute),
),
2022-06-04 09:00:23 +00:00
) = KtorKeyValuesRepoClient<Key, Value>(
baseUrl,
httpClient,
contentType,
{
serialFormat.encodeToString(keySerializer, it).encodeURLQueryComponent()
2023-01-14 07:15:59 +00:00
},
onNewValue,
onValueRemoved,
onDataCleared
) {
serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent()
}
2022-06-04 09:00:23 +00:00
inline fun <reified Key : Any, reified Value : Any> KtorKeyValuesRepoClient(
baseUrl: String,
httpClient: HttpClient,
contentType: ContentType,
keySerializer: SerializationStrategy<Key>,
valueSerializer: SerializationStrategy<Value>,
serialFormat: BinaryFormat,
2023-01-14 07:15:59 +00:00
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
),
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute),
),
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute),
),
2022-06-04 09:00:23 +00:00
) = KtorKeyValuesRepoClient<Key, Value>(
baseUrl,
httpClient,
contentType,
{
serialFormat.encodeHex(keySerializer, it)
2023-01-14 07:15:59 +00:00
},
onNewValue,
onValueRemoved,
onDataCleared
) {
serialFormat.encodeHex(valueSerializer, it)
}