Skip to content

Commit

Permalink
improve dsl for response status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Jul 11, 2024
1 parent 934f714 commit cbd97bf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private fun Application.myModule() {
// information about possible responses
response {
// information about a "200 OK" response
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
// a description of the response
description = "successful request - always returns 'Hello World!'"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private fun Application.myModule() {
body<Unit>()
}
response {
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
description = "successful request - always returns 'Hello World!'"
header<String>("x-random") {
description = "A header with some random number"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private fun Application.myModule() {
// information about the request
response {
// information about a "200 OK" response
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
// body of the response
body<MyResponseBody>()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.ktor.server.netty.Netty
import io.ktor.server.response.respond
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import sun.jvm.hotspot.oops.CellTypeState.value

fun main() {
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
Expand Down Expand Up @@ -78,7 +79,7 @@ private fun Application.myModule() {
}
}
response {
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
body<List<Pet>> {
description = "the list of available pets"
example("Pet List") {
Expand Down Expand Up @@ -130,7 +131,7 @@ private fun Application.myModule() {
}
}
response {
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
body<Pet> {
description = "the created pet"
example("Bird") {
Expand Down Expand Up @@ -175,7 +176,7 @@ private fun Application.myModule() {
}
}
response {
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
body<Pet>{
description = "the pet with the given id"
example("Bird") {
Expand All @@ -194,7 +195,7 @@ private fun Application.myModule() {
}
}
}
HttpStatusCode.NotFound to {
code(HttpStatusCode.NotFound) {
description = "the pet with the given id was not found"
}
default {
Expand All @@ -221,10 +222,10 @@ private fun Application.myModule() {
}
}
response {
HttpStatusCode.NoContent to {
code(HttpStatusCode.NoContent) {
description = "the pet was successfully deleted"
}
HttpStatusCode.NotFound to {
code(HttpStatusCode.NotFound) {
description = "the pet with the given id was not found"
}
default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ private fun Application.myModule() {
// information the possible responses
response {
// document the "200 OK"-response
HttpStatusCode.OK to {
code(HttpStatusCode.OK) {
description = "Calculation was performed successfully."
// specify the schema of the response-body and some additional information
body<CalculationResult> {
description = "the result of an operation together with the original request"
}
}
// document the "422 UnprocessableEntity"-response
HttpStatusCode.UnprocessableEntity to {
code(HttpStatusCode.UnprocessableEntity) {
description = "The requested calculation could not be performed, e.g. due to division by zero."
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ class OpenApiResponses {
*/
infix fun HttpStatusCode.to(block: OpenApiResponse.() -> Unit) = this.value.toString() to block

/**
* Information of response for a given http status code
*/
fun code(statusCode: String, block: OpenApiResponse.() -> Unit) {
responses[statusCode] = OpenApiResponse(statusCode).apply(block)
}

/**
* Information of response for a given http status code
*/
fun code(statusCode: HttpStatusCode, block: OpenApiResponse.() -> Unit) = code(statusCode.toString(), block)


/**
* Information of the default response
Expand Down

0 comments on commit cbd97bf

Please sign in to comment.