From 992b2835974ca1f4e1bc6cc2df3868bf2ec17195 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 16:47:22 +0600 Subject: [PATCH] add kv swagger template --- .../key_value/KtorReadKeyValueRepoClient.kt | 5 +- repos/ktor/{server/src => }/crud.yml | 11 +- repos/ktor/kv.yml | 196 ++++++++++++++++++ .../key_value/KtorReadKeyValueRepoRoutes.kt | 5 +- 4 files changed, 203 insertions(+), 14 deletions(-) rename repos/ktor/{server/src => }/crud.yml (94%) create mode 100644 repos/ktor/kv.yml diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt index ded3486007d..2171773504a 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt @@ -5,6 +5,7 @@ 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( baseUrl, getRoute, mapOf( - idParameterName to idSerializer(k) + keyParameterName to idSerializer(k) ) ) ) { @@ -41,7 +42,7 @@ class KtorReadKeyValueRepoClient( buildStandardUrl( baseUrl, containsRoute, - idParameterName to idSerializer(key) + keyParameterName to idSerializer(key) ) ) { contentType(contentType) diff --git a/repos/ktor/server/src/crud.yml b/repos/ktor/crud.yml similarity index 94% rename from repos/ktor/server/src/crud.yml rename to repos/ktor/crud.yml index e5544211a6a..d634dc87b9c 100644 --- a/repos/ktor/server/src/crud.yml +++ b/repos/ktor/crud.yml @@ -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" diff --git a/repos/ktor/kv.yml b/repos/ktor/kv.yml new file mode 100644 index 00000000000..52e2c13650e --- /dev/null +++ b/repos/ktor/kv.yml @@ -0,0 +1,196 @@ +swagger: "2.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.0" + 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" + +parameters: + KeyInQuery: + in: "query" + name: "key" + 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" + 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 + responses: + "200": + description: "Element by key" + schema: + $ref: "#/definitions/Value" + "204": + description: "No value by id" + /values: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/PaginationInQueryPage" + - $ref: "#/parameters/PaginationInQuerySize" + - $ref: "#/parameters/ReversedInQuery" + responses: + "200": + description: "Pagination with elements" + 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 + responses: + "200": + description: "Object with id availability in repo" + schema: + type: boolean + /count: + get: + tags: + - "Read" + responses: + "200": + description: "Amount of objects in repo" + schema: + type: integer + + + /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" + responses: + "200": + description: "Will return 200 if everything has been completed ok" + /unset: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/KeysInBody" + responses: + "200": + description: "Objects with keys from body has been unset" + /unsetWithValues: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/ValuesInBody" + responses: + "200": + description: "Objects with values from body has been unset" diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt index 67eec8bda59..9b2d2f98e6a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt @@ -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 Route.configureReadKeyValueRepoRoutes ( get(getRoute) { val key = idDeserializer( - call.getQueryParameterOrSendError(idParameterName) ?: return@get + call.getQueryParameterOrSendError(keyParameterName) ?: return@get ) originalRepo.get(key) ?.let { @@ -63,7 +64,7 @@ inline fun Route.configureReadKeyValueRepoRoutes ( get(containsRoute) { val key = idDeserializer( - call.getQueryParameterOrSendError(idParameterName) ?: return@get + call.getQueryParameterOrSendError(keyParameterName) ?: return@get ) call.respond(originalRepo.contains(key))