-
Notifications
You must be signed in to change notification settings - Fork 2
/
KtorReadKeyValueRepoClient.kt
155 lines (144 loc) · 4.83 KB
/
KtorReadKeyValueRepoClient.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package dev.inmo.micro_utils.repos.ktor.client.key.value
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
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.keyParameterName
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
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 KtorReadKeyValueRepoClient<Key, Value>(
private val baseUrl: String,
private val httpClient: HttpClient,
private val contentType: ContentType,
private val objectType: TypeInfo,
private val paginationResultObjectsTypeInfo: TypeInfo,
private val paginationResultIdsTypeInfo: TypeInfo,
private val mapKeyValueTypeInfo: TypeInfo,
private val idSerializer: suspend (Key) -> String,
private val valueSerializer: suspend (Value) -> String
) : ReadKeyValueRepo<Key, Value> {
override suspend fun get(k: Key): Value? = httpClient.get(
buildStandardUrl(
baseUrl,
getRoute,
mapOf(
keyParameterName to idSerializer(k)
)
)
) {
contentType(contentType)
}.takeIf { it.status != HttpStatusCode.NoContent } ?.body<Value>(objectType)
override suspend fun contains(key: Key): Boolean = httpClient.get(
buildStandardUrl(
baseUrl,
containsRoute,
keyParameterName to idSerializer(key)
)
) {
contentType(contentType)
}.body()
override suspend fun getAll(): Map<Key, Value> = httpClient.get(
buildStandardUrl(
baseUrl,
getAllRoute
)
) {
contentType(contentType)
}.body(mapKeyValueTypeInfo)
override suspend fun values(
pagination: Pagination,
reversed: Boolean
): PaginationResult<Value> = httpClient.get(
buildStandardUrl(baseUrl, valuesRoute, pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()))
) {
contentType(contentType)
}.body(paginationResultObjectsTypeInfo)
override suspend fun keys(
pagination: Pagination,
reversed: Boolean
): PaginationResult<Key> = httpClient.get(
buildStandardUrl(baseUrl, keysRoute, pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()))
) {
contentType(contentType)
}.body(paginationResultIdsTypeInfo)
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(paginationResultIdsTypeInfo)
override suspend fun count(): Long = httpClient.get(
buildStandardUrl(
baseUrl,
dev.inmo.micro_utils.repos.ktor.common.countRoute
)
) {
contentType(contentType)
}.body()
}
inline fun <reified Key, reified Value> KtorReadKeyValueRepoClient(
baseUrl: String,
httpClient: HttpClient,
contentType: ContentType,
noinline idSerializer: suspend (Key) -> String,
noinline valueSerializer: suspend (Value) -> String
) = KtorReadKeyValueRepoClient<Key, Value>(
baseUrl,
httpClient,
contentType,
typeInfo<Value>(),
typeInfo<PaginationResult<Value>>(),
typeInfo<PaginationResult<Key>>(),
typeInfo<Map<Key, Value>>(),
idSerializer,
valueSerializer
)
inline fun <reified Key, reified Value> KtorReadKeyValueRepoClient(
baseUrl: String,
httpClient: HttpClient,
idsSerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StringFormat,
contentType: ContentType,
) = KtorReadKeyValueRepoClient<Key, Value>(
baseUrl,
httpClient,
contentType,
{
serialFormat.encodeToString(idsSerializer, it).encodeURLQueryComponent()
}
) {
serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent()
}
inline fun <reified Key, reified Value> KtorReadKeyValueRepoClient(
baseUrl: String,
httpClient: HttpClient,
idsSerializer: KSerializer<Key>,
valuesSerializer: KSerializer<Value>,
serialFormat: BinaryFormat,
contentType: ContentType,
) = KtorReadKeyValueRepoClient<Key, Value>(
baseUrl,
httpClient,
contentType,
{
serialFormat.encodeHex(idsSerializer, it)
}
) {
serialFormat.encodeHex(valuesSerializer, it)
}