Compare commits

...

16 Commits

26 changed files with 569 additions and 89 deletions

View File

@@ -1,12 +1,37 @@
# Changelog
## 0.11.3
* `Ktor`:
* Support of `WebSockets` has been improved
* `Client`:
* New extensions: `HttpClient#openBaseWebSocketFlow`, `HttpClient#openWebSocketFlow`, `HttpClient#openSecureWebSocketFlow`
## 0.11.2
* `Ktor`:
* Support of `WebSockets` has been improved and added fixes inside of clients
## 0.11.1
* `Repos`
* `Ktor`
* In `configureReadKeyValueRepoRoutes` and `configureReadKeyValuesRepoRoutes` configurators fixed requiring of `reversed` property
## 0.11.0
* `Versions`
* `UUID`: `0.4.0` -> `0.4.1`
* `Ktor`
*
* `Client`:
* New extension fun `HttpResponse#throwOnUnsuccess`
* All old functions, classes and extensions has been rewritten with new ktor-way with types info and keeping `ContentNegotiation` in mind
* `Server`:
* All old functions, classes and extensions has been rewritten with new ktor-way with types info and keeping `ContentNegotiation` in mind
* `Repos`
* `Ktor`:
* Fully rewritten work with all declared repositories
* All old functions, classes and extensions has been rewritten with new ktor-way with types info and keeping `ContentNegotiation` in mind
## 0.10.8

View File

@@ -14,5 +14,5 @@ crypto_js_version=4.1.1
# Project data
group=dev.inmo
version=0.11.0
android_code_version=124
version=0.11.3
android_code_version=127

View File

@@ -9,7 +9,7 @@ jb-exposed = "0.38.2"
jb-dokka = "1.6.21"
klock = "2.7.0"
uuid = "0.4.0"
uuid = "0.4.1"
ktor = "2.0.2"

View File

@@ -1,5 +1,6 @@
package dev.inmo.micro_utils.ktor.client
import dev.inmo.micro_utils.common.Warning
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.micro_utils.ktor.common.*
@@ -7,35 +8,25 @@ import io.ktor.client.HttpClient
import io.ktor.client.plugins.pluginOrNull
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.websocket.Frame
import io.ktor.websocket.readBytes
import io.ktor.websocket.serialization.sendSerializedBase
import io.ktor.http.URLProtocol
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.isActive
import kotlinx.serialization.DeserializationStrategy
/**
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
* connection. Must return true in case if must be reconnected. By default always reconnecting
*/
inline fun <reified T> HttpClient.createStandardWebsocketFlow(
url: String,
@Warning("This feature is internal and should not be used directly. It is can be changed without any notification and warranty on compile-time or other guaranties")
inline fun <reified T : Any> openBaseWebSocketFlow(
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
noinline requestBuilder: HttpRequestBuilder.() -> Unit = {}
noinline webSocketSessionRequest: suspend SendChannel<T>.() -> Unit
): Flow<T> {
pluginOrNull(WebSockets) ?: error("Plugin $WebSockets must be installed for using createStandardWebsocketFlow")
val correctedUrl = url.asCorrectWebSocketUrl
return channelFlow {
do {
val reconnect = runCatchingSafely {
ws(correctedUrl, requestBuilder) {
for (received in incoming) {
sendSerialized(received.data)
}
}
webSocketSessionRequest()
checkReconnection(null)
}.getOrElse { e ->
checkReconnection(e).also {
@@ -53,3 +44,60 @@ inline fun <reified T> HttpClient.createStandardWebsocketFlow(
}
}
}
/**
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
* connection. Must return true in case if must be reconnected. By default always reconnecting
*/
inline fun <reified T : Any> HttpClient.openWebSocketFlow(
url: String,
useSecureConnection: Boolean,
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
noinline requestBuilder: HttpRequestBuilder.() -> Unit = {}
): Flow<T> {
pluginOrNull(WebSockets) ?: error("Plugin $WebSockets must be installed for using createStandardWebsocketFlow")
return openBaseWebSocketFlow<T>(checkReconnection) {
val block: suspend DefaultClientWebSocketSession.() -> Unit = {
while (isActive) {
send(receiveDeserialized<T>())
}
}
if (useSecureConnection) {
wss(url, requestBuilder, block)
} else {
ws(url, requestBuilder, block)
}
}
}
/**
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
* connection. Must return true in case if must be reconnected. By default always reconnecting
*/
inline fun <reified T : Any> HttpClient.openWebSocketFlow(
url: String,
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
noinline requestBuilder: HttpRequestBuilder.() -> Unit = {}
): Flow<T> = openWebSocketFlow(url, false, checkReconnection, requestBuilder)
/**
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
* connection. Must return true in case if must be reconnected. By default always reconnecting
*/
inline fun <reified T : Any> HttpClient.openSecureWebSocketFlow(
url: String,
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
noinline requestBuilder: HttpRequestBuilder.() -> Unit = {}
): Flow<T> = openWebSocketFlow(url, true, checkReconnection, requestBuilder)
/**
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
* connection. Must return true in case if must be reconnected. By default always reconnecting
*/
inline fun <reified T : Any> HttpClient.createStandardWebsocketFlow(
url: String,
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
noinline requestBuilder: HttpRequestBuilder.() -> Unit = {}
): Flow<T> = openWebSocketFlow(url, checkReconnection, requestBuilder)

View File

@@ -12,6 +12,7 @@ import io.ktor.websocket.send
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.SerializationStrategy
@Deprecated("This method will be removed soon")
fun <T> Route.includeWebsocketHandling(
suburl: String,
flow: Flow<T>,

View File

@@ -15,7 +15,8 @@ import kotlinx.serialization.SerializationStrategy
inline fun <reified T : Any> Route.includeWebsocketHandling(
suburl: String,
flow: Flow<T>,
protocol: URLProtocol? = null
protocol: URLProtocol? = null,
noinline dataMapper: suspend WebSocketServerSession.(T) -> T? = { it }
) {
application.apply {
pluginOrNull(WebSockets) ?: install(WebSockets)
@@ -23,7 +24,7 @@ inline fun <reified T : Any> Route.includeWebsocketHandling(
webSocket(suburl, protocol ?.name) {
safely {
flow.collect {
sendSerialized(it)
sendSerialized(dataMapper(it) ?: return@collect)
}
}
}

View File

@@ -13,6 +13,7 @@ import io.ktor.http.content.streamProvider
import io.ktor.server.application.call
import io.ktor.server.request.receiveMultipart
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
import io.ktor.server.routing.post
import kotlinx.coroutines.*
@@ -111,7 +112,7 @@ class TemporalFilesRoutingConfigurator(
temporalFilesMutex.withLock {
temporalFilesMap[fileId] = file
}
call.respond(fileId.string)
call.respondText(fileId.string)
launchSafelyWithoutExceptions { filesFlow.emit(fileId) }
} ?: call.respond(HttpStatusCode.BadRequest)
}

View File

@@ -1,6 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient
import io.ktor.http.ContentType
@@ -19,14 +20,14 @@ class KtorCRUDRepoClient<ObjectType, IdType, InputValue> (
inline operator fun <reified ObjectType, reified IdType, reified InputValue> invoke(
baseUrl: String,
httpClient: HttpClient,
objectTypeInfo: TypeInfo,
contentType: ContentType,
noinline idSerializer: suspend (IdType) -> String
) = KtorCRUDRepoClient(
KtorReadCRUDRepoClient(
baseUrl,
httpClient,
objectTypeInfo,
typeInfo<ObjectType>(),
typeInfo<PaginationResult<ObjectType>>(),
contentType,
idSerializer
),
@@ -41,33 +42,17 @@ class KtorCRUDRepoClient<ObjectType, IdType, InputValue> (
baseUrl: String,
subpart: String,
httpClient: HttpClient,
objectTypeInfo: TypeInfo,
contentType: ContentType,
noinline idSerializer: suspend (IdType) -> String
) = KtorCRUDRepoClient<ObjectType, IdType, InputValue>(
buildStandardUrl(baseUrl, subpart),
httpClient,
objectTypeInfo,
contentType,
idSerializer
)
}
}
inline fun <reified ObjectType, reified IdType, reified InputValue> KtorCRUDRepoClient(
baseUrl: String,
httpClient: HttpClient,
contentType: ContentType,
noinline idSerializer: suspend (IdType) -> String
) = KtorCRUDRepoClient<ObjectType, IdType, InputValue>(
baseUrl,
httpClient,
typeInfo<ObjectType>(),
contentType,
idSerializer
)
inline fun <reified ObjectType, reified IdType, reified InputValue> KtorCRUDRepoClient(
baseUrl: String,
httpClient: HttpClient,

View File

@@ -18,6 +18,7 @@ class KtorReadCRUDRepoClient<ObjectType, IdType> (
private val baseUrl: String,
private val httpClient: HttpClient,
private val objectType: TypeInfo,
private val paginationObjectType: TypeInfo,
private val contentType: ContentType,
private val idSerializer: suspend (IdType) -> String
) : ReadCRUDRepo<ObjectType, IdType> {
@@ -25,7 +26,7 @@ class KtorReadCRUDRepoClient<ObjectType, IdType> (
buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts)
) {
contentType(contentType)
}.body()
}.body(paginationObjectType)
override suspend fun getById(id: IdType): ObjectType? = httpClient.get(
buildStandardUrl(
@@ -70,6 +71,7 @@ inline fun <reified ObjectType, IdType> KtorReadCRUDRepoClient(
baseUrl,
httpClient,
typeInfo<ObjectType>(),
typeInfo<PaginationResult<ObjectType>>(),
contentType,
idSerializer
)

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value
package dev.inmo.micro_utils.repos.ktor.client.key.value
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.repos.*

View File

@@ -1,10 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value
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
@@ -30,7 +31,7 @@ class KtorReadKeyValueRepoClient<Key, Value>(
baseUrl,
getRoute,
mapOf(
idParameterName to idSerializer(k)
keyParameterName to idSerializer(k)
)
)
) {
@@ -41,7 +42,7 @@ class KtorReadKeyValueRepoClient<Key, Value>(
buildStandardUrl(
baseUrl,
containsRoute,
idParameterName to idSerializer(key)
keyParameterName to idSerializer(key)
)
) {
contentType(contentType)

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value
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

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
package dev.inmo.micro_utils.repos.ktor.client.key.values
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.repos.*

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
package dev.inmo.micro_utils.repos.ktor.client.key.values
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.pagination.*

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
package dev.inmo.micro_utils.repos.ktor.client.key.values
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess

View File

@@ -1,8 +1,8 @@
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorKeyValueRepoClient
import dev.inmo.micro_utils.repos.ktor.server.key_value.configureKeyValueRepoRoutes
import dev.inmo.micro_utils.repos.ktor.client.key.value.KtorKeyValueRepoClient
import dev.inmo.micro_utils.repos.ktor.server.key.value.configureKeyValueRepoRoutes
import io.ktor.client.HttpClient
import io.ktor.client.plugins.logging.Logging
import io.ktor.http.ContentType

View File

@@ -1,8 +1,8 @@
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.ktor.client.one_to_many.KtorKeyValuesRepoClient
import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureKeyValuesRepoRoutes
import dev.inmo.micro_utils.repos.ktor.client.key.values.KtorKeyValuesRepoClient
import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes
import io.ktor.client.HttpClient
import io.ktor.client.plugins.logging.Logging
import io.ktor.http.ContentType

View File

@@ -79,15 +79,6 @@ definitions:
type: array
items:
$ref: "#/definitions/NewValue"
Pagination:
type: object
properties:
page:
type: integer
description: "Page of pagination"
size:
type: integer
description: "Size of each page in pagination"
PaginationResult:
type: object
properties:
@@ -116,7 +107,7 @@ paths:
- $ref: "#/parameters/PaginationInQuerySize"
responses:
"200":
description: "Pagination of elements"
description: "Pagination with elements"
schema:
allOf:
- $ref: "#/definitions/PaginationResult"

201
repos/ktor/kv.yml Normal file
View File

@@ -0,0 +1,201 @@
openapi: "3.0.0"
info:
description: "This is a template for the KeyValue repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value)"
version: "0.11.1"
title: "KeyValue Repo"
contact:
email: "ovsyannikov.alexey95@gmail.com"
tags:
- name: "Read"
description: "Operations with `get` request in most cases"
- name: "Write"
description: "Operations with `post` request in most cases"
components:
parameters:
KeyInQuery:
in: "query"
name: "key"
schema:
$ref: "#/components/schemas/Key"
PaginationInQueryPage:
in: "query"
name: "ppage"
description: "Page of pagination"
schema:
type: integer
required: false
PaginationInQuerySize:
in: "query"
name: "psize"
description: "Size of each page in pagination"
schema:
type: integer
required: false
ReversedInQuery:
in: "query"
name: "reversed"
description: "If passed, will tell to reverse the result pages"
schema:
type: boolean
required: false
ValueInQuery:
in: "query"
name: "value"
schema:
$ref: "#/components/schemas/Value"
schemas:
Key:
type: integer
description: "REWRITE THIS TYPE AS KEY IN SWAGGER FILE"
Value:
type: integer
description: "REWRITE THIS TYPE AS VALUE IN SWAGGER FILE"
PaginationResult:
type: object
properties:
page:
type: integer
description: "Page of pagination"
pagesNumber:
type: integer
description: "Count of pages with the size from this pagination"
size:
type: integer
description: "Size of each page in pagination"
results:
type: array
description: "Array of all elements on that page. Size of pagination and size of array can be different and it can be interpreted like current page is the last one"
items:
type: object
paths:
/get:
get:
tags:
- "Read"
parameters:
- allOf:
- $ref: "#/components/parameters/KeyInQuery"
- required: true
responses:
"200":
description: "Element by key"
content:
"*/*":
schema:
$ref: "#/components/schemas/Value"
"204":
description: "No value by id"
/values:
get:
tags:
- "Read"
parameters:
- $ref: "#/components/parameters/PaginationInQueryPage"
- $ref: "#/components/parameters/PaginationInQuerySize"
- $ref: "#/components/parameters/ReversedInQuery"
responses:
"200":
description: "Pagination with elements"
content:
"*/*":
schema:
allOf:
- $ref: "#/components/schemas/PaginationResult"
- properties:
results:
items:
$ref: "#/components/schemas/Value"
/keys:
get:
tags:
- "Read"
parameters:
- $ref: "#/components/parameters/PaginationInQueryPage"
- $ref: "#/components/parameters/PaginationInQuerySize"
- $ref: "#/components/parameters/ReversedInQuery"
- $ref: "#/components/parameters/ValueInQuery"
required: false
responses:
"200":
description: "Pagination with elements"
content:
"*/*":
schema:
allOf:
- $ref: "#/components/schemas/PaginationResult"
- properties:
results:
items:
$ref: "#/components/schemas/Key"
/contains:
get:
tags:
- "Read"
parameters:
- $ref: "#/components/parameters/KeyInQuery"
required: true
responses:
"200":
description: "Object with id availability in repo"
content:
"*/*":
schema:
type: boolean
/count:
get:
tags:
- "Read"
responses:
"200":
description: "Amount of objects in repo"
content:
"*/*":
schema:
type: integer
/set:
post:
tags:
- "Write"
requestBody:
content:
"*/*":
schema:
type: object
additionalProperties:
$ref: "#/components/schemas/Value"
description: "Map with new elements to set. Use keys as a keys of this map"
responses:
"200":
description: "Will return 200 if everything has been completed ok"
/unset:
post:
tags:
- "Write"
requestBody:
content:
"*/*":
schema:
type: array
items:
$ref: "#/components/schemas/Key"
responses:
"200":
description: "Objects with keys from body has been unset"
/unsetWithValues:
post:
tags:
- "Write"
requestBody:
content:
"*/*":
schema:
type: array
items:
$ref: "#/components/schemas/Value"
responses:
"200":
description: "Objects with values from body has been unset"

222
repos/ktor/kvs.yml Normal file
View File

@@ -0,0 +1,222 @@
swagger: "2.0"
info:
description: "This is a template for the KeyValues repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values)"
version: "0.11.0"
title: "KeyValues Repo"
contact:
email: "ovsyannikov.alexey95@gmail.com"
tags:
- name: "Read"
description: "Operations with `get` request in most cases"
- name: "Write"
description: "Operations with `post` request in most cases"
parameters:
KeyInQuery:
in: "query"
name: "key"
allOf:
- $ref: "#/definitions/Key"
KeyInBody:
in: "body"
name: "body"
allOf:
- $ref: "#/definitions/Key"
KeysInBody:
in: "body"
name: "body"
type: array
items:
$ref: "#/definitions/Key"
ValuesInBody:
in: "body"
name: "body"
type: array
items:
$ref: "#/definitions/Value"
PaginationInQueryPage:
in: "query"
type: integer
name: "ppage"
description: "Page of pagination"
required: false
PaginationInQuerySize:
in: "query"
type: integer
name: "psize"
description: "Size of each page in pagination"
required: false
ReversedInQuery:
in: "query"
type: boolean
name: "reversed"
description: "If passed, will tell to reverse the result pages"
required: false
ValueInQuery:
in: "query"
name: "value"
allOf:
- $ref: "#/definitions/Value"
ValueInBody:
in: "body"
name: "body"
allOf:
- $ref: "#/definitions/Value"
MapInBody:
in: "body"
name: "body"
allOf:
- $ref: "#/definitions/Map"
definitions:
Key:
type: integer
description: "REWRITE THIS TYPE AS KEY IN SWAGGER FILE"
Value:
type: integer
description: "REWRITE THIS TYPE AS VALUE IN SWAGGER FILE"
Map:
type: object
description: "Map of objects"
PaginationResult:
type: object
properties:
page:
type: integer
description: "Page of pagination"
pagesNumber:
type: integer
description: "Count of pages with the size from this pagination"
size:
type: integer
description: "Size of each page in pagination"
results:
type: array
description: "Array of all elements on that page. Size of pagination and size of array can be different and it can be interpreted like current page is the last one"
items:
type: object
paths:
/get:
get:
tags:
- "Read"
parameters:
- $ref: "#/parameters/KeyInQuery"
required: true
- $ref: "#/parameters/PaginationInQueryPage"
- $ref: "#/parameters/PaginationInQuerySize"
- $ref: "#/parameters/ReversedInQuery"
responses:
"200":
description: "Elements by query"
schema:
allOf:
- $ref: "#/definitions/PaginationResult"
- properties:
results:
items:
$ref: "#/definitions/Value"
/keys:
get:
tags:
- "Read"
parameters:
- $ref: "#/parameters/PaginationInQueryPage"
- $ref: "#/parameters/PaginationInQuerySize"
- $ref: "#/parameters/ReversedInQuery"
- $ref: "#/parameters/ValueInQuery"
required: false
responses:
"200":
description: "Pagination with elements"
schema:
allOf:
- $ref: "#/definitions/PaginationResult"
- properties:
results:
items:
$ref: "#/definitions/Key"
/contains:
get:
tags:
- "Read"
parameters:
- $ref: "#/parameters/KeyInQuery"
required: true
- $ref: "#/parameters/ValueInQuery"
required: false
responses:
"200":
description: "Object with key and optionally availability in repo"
schema:
type: boolean
/count:
get:
tags:
- "Read"
responses:
"200":
description: "Amount of objects in repo"
schema:
type: integer
/add:
post:
tags:
- "Write"
parameters:
- allOf:
- $ref: "#/parameters/MapInBody"
- additionalProperties:
$ref: "#/definitions/Value"
description: "Map with new elements to add. Use keys as a keys of this map. That values will be added to the end of current data by their keys"
responses:
"200":
description: "Will return 200 if everything has been completed ok"
/set:
post:
tags:
- "Write"
parameters:
- allOf:
- $ref: "#/parameters/MapInBody"
- additionalProperties:
$ref: "#/definitions/Value"
description: "Map with new elements to set. Use keys as a keys of this map. That values will overwrite all exists data by their keys"
responses:
"200":
description: "Will return 200 if everything has been completed ok"
/remove:
post:
tags:
- "Write"
parameters:
- allOf:
- $ref: "#/parameters/MapInBody"
- additionalProperties:
$ref: "#/definitions/Value"
description: "Map with data to remove. Removing will be processed by each value for its key"
responses:
"200":
description: "Objects with keys and values from body has been unset"
/clear:
post:
tags:
- "Write"
parameters:
- $ref: "#/parameters/KeyInBody"
responses:
"200":
description: "Data with corresponding key has been removed"
/clearWithValues:
post:
tags:
- "Write"
parameters:
- $ref: "#/parameters/ValueInBody"
responses:
"200":
description: "Will remove value from all keys data"

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value
package dev.inmo.micro_utils.repos.ktor.server.key.value
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.repos.KeyValueRepo

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value
package dev.inmo.micro_utils.repos.ktor.server.key.value
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.server.*
@@ -8,6 +8,7 @@ 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.countRoute
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.http.*
@@ -30,7 +31,7 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValueRepoRoutes (
get(getRoute) {
val key = idDeserializer(
call.getQueryParameterOrSendError(idParameterName) ?: return@get
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
originalRepo.get(key) ?.let {
@@ -50,7 +51,7 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValueRepoRoutes (
get(keysRoute) {
val pagination = call.request.queryParameters.extractPagination
val reversed = call.getQueryParameterOrSendError(reversedParameterName) ?.toBoolean() ?: false
val reversed = call.getQueryParameter(reversedParameterName) ?.toBoolean() ?: false
val value = call.getQueryParameter(valueParameterName) ?.let {
valueDeserializer(it)
}
@@ -63,7 +64,7 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValueRepoRoutes (
get(containsRoute) {
val key = idDeserializer(
call.getQueryParameterOrSendError(idParameterName) ?: return@get
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
call.respond(originalRepo.contains(key))

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value
package dev.inmo.micro_utils.repos.ktor.server.key.value
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteKeyValueRepo

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
package dev.inmo.micro_utils.repos.ktor.server.key.values
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.repos.*
@@ -8,22 +8,22 @@ import kotlinx.serialization.*
inline fun <reified Key : Any, reified Value : Any> Route.configureKeyValuesRepoRoutes (
originalRepo: KeyValuesRepo<Key, Value>,
noinline idDeserializer: suspend (String) -> Key,
noinline keyDeserializer: suspend (String) -> Key,
noinline valueDeserializer: suspend (String) -> Value
) {
configureReadKeyValuesRepoRoutes(originalRepo, idDeserializer, valueDeserializer)
configureReadKeyValuesRepoRoutes(originalRepo, keyDeserializer, valueDeserializer)
configureWriteKeyValuesRepoRoutes(originalRepo)
}
inline fun <reified Key : Any, reified Value : Any> Route.configureKeyValuesRepoRoutes(
originalRepo: KeyValuesRepo<Key, Value>,
idsSerializer: DeserializationStrategy<Key>,
keySerializer: DeserializationStrategy<Key>,
valueSerializer: DeserializationStrategy<Value>,
serialFormat: StringFormat
) = configureKeyValuesRepoRoutes(
originalRepo,
{
serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent())
serialFormat.decodeFromString(keySerializer, it.decodeURLQueryComponent())
},
{
serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent())
@@ -32,13 +32,13 @@ inline fun <reified Key : Any, reified Value : Any> Route.configureKeyValuesRepo
inline fun <reified Key : Any, reified Value : Any> Route.configureKeyValuesRepoRoutes(
originalRepo: KeyValuesRepo<Key, Value>,
idsSerializer: DeserializationStrategy<Key>,
keySerializer: DeserializationStrategy<Key>,
valueSerializer: DeserializationStrategy<Value>,
serialFormat: BinaryFormat
) = configureKeyValuesRepoRoutes(
originalRepo,
{
serialFormat.decodeHex(idsSerializer, it)
serialFormat.decodeHex(keySerializer, it)
},
{
serialFormat.decodeHex(valueSerializer, it)

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
package dev.inmo.micro_utils.repos.ktor.server.key.values
import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.server.*
@@ -7,6 +7,7 @@ import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
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.one_to_many.*
import io.ktor.http.*
import io.ktor.server.application.call
@@ -20,14 +21,14 @@ import kotlinx.serialization.*
@OptIn(InternalAPI::class)
inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes (
originalRepo: ReadKeyValuesRepo<Key, Value>,
noinline idDeserializer: suspend (String) -> Key,
noinline keyDeserializer: suspend (String) -> Key,
noinline valueDeserializer: suspend (String) -> Value
) {
val paginationWithValuesTypeInfo = typeInfo<PaginationResult<Value>>()
val paginationWithKeysTypeInfo = typeInfo<PaginationResult<Key>>()
get(getRoute) {
val key = idDeserializer(
val key = keyDeserializer(
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
val pagination = call.request.queryParameters.extractPagination
@@ -41,7 +42,7 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes (
get(keysRoute) {
val pagination = call.request.queryParameters.extractPagination
val reversed = call.getQueryParameterOrSendError(reversedParameterName) ?.toBoolean() ?: false
val reversed = call.getQueryParameter(reversedParameterName) ?.toBoolean() ?: false
val value = call.getQueryParameter(valueParameterName) ?.let {
valueDeserializer(it)
}
@@ -53,7 +54,7 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes (
}
get(containsRoute) {
val key = idDeserializer(
val key = keyDeserializer(
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
val value = call.getQueryParameter(valueParameterName) ?.let {
@@ -65,9 +66,9 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes (
)
}
get(dev.inmo.micro_utils.repos.ktor.common.countRoute) {
get(countRoute) {
val id = call.getQueryParameter(keyParameterName) ?.let {
idDeserializer(it)
keyDeserializer(it)
}
call.respond(
id ?.let { originalRepo.count(it) } ?: originalRepo.count()
@@ -77,13 +78,13 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes (
inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes(
originalRepo: ReadKeyValuesRepo<Key, Value>,
idsSerializer: DeserializationStrategy<Key>,
keySerializer: DeserializationStrategy<Key>,
valueSerializer: DeserializationStrategy<Value>,
serialFormat: StringFormat
) = configureReadKeyValuesRepoRoutes(
originalRepo,
{
serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent())
serialFormat.decodeFromString(keySerializer, it.decodeURLQueryComponent())
},
{
serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent())
@@ -92,13 +93,13 @@ inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes(
inline fun <reified Key, reified Value> Route.configureReadKeyValuesRepoRoutes(
originalRepo: ReadKeyValuesRepo<Key, Value>,
idsSerializer: DeserializationStrategy<Key>,
keySerializer: DeserializationStrategy<Key>,
valueSerializer: DeserializationStrategy<Value>,
serialFormat: BinaryFormat
) = configureReadKeyValuesRepoRoutes(
originalRepo,
{
serialFormat.decodeHex(idsSerializer, it)
serialFormat.decodeHex(keySerializer, it)
},
{
serialFormat.decodeHex(valueSerializer, it)

View File

@@ -1,4 +1,4 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
package dev.inmo.micro_utils.repos.ktor.server.key.values
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteKeyValuesRepo