Skip to content

Commit

Permalink
Improve documentation with Http codes (#261)
Browse files Browse the repository at this point in the history
* Improve documentation dsl with Http codes

* Fix plaintext requiring T
  • Loading branch information
Syer10 authored Dec 1, 2021
1 parent efff68c commit 2051015
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package suwayomi.tachidesk.manga.controller
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import io.javalin.http.Context
import io.javalin.http.HttpCode
import suwayomi.tachidesk.manga.impl.CategoryManga
import suwayomi.tachidesk.manga.impl.Chapter
import suwayomi.tachidesk.manga.impl.Library
Expand Down Expand Up @@ -40,7 +41,8 @@ object MangaController {
)
},
withResults = {
json<MangaDataClass>("OK")
json<MangaDataClass>(HttpCode.OK)
httpCode(HttpCode.NOT_FOUND)
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package suwayomi.tachidesk.server.util

import io.javalin.http.Context
import io.javalin.http.HttpCode
import io.javalin.plugin.openapi.dsl.DocumentedHandler
import io.javalin.plugin.openapi.dsl.OpenApiDocumentation
import io.javalin.plugin.openapi.dsl.documented
Expand Down Expand Up @@ -114,19 +115,27 @@ sealed class Param<T> {
class ResultsBuilder {
val results = mutableListOf<ResultType<*>>()

inline fun <reified T> json(status: String) {
results += ResultType.MimeType(status, "application/json", T::class.java)
inline fun <reified T> json(code: HttpCode) {
results += ResultType.MimeType(code, "application/json", T::class.java)
}
inline fun <reified T> plainText(status: String) {
results += ResultType.MimeType(status, "text/plain", String::class.java)
fun plainText(code: HttpCode) {
results += ResultType.MimeType(code, "text/plain", String::class.java)
}
fun httpCode(code: HttpCode) {
results += ResultType.StatusCode(code)
}
}

sealed class ResultType <T> {
abstract fun applyTo(documentation: OpenApiDocumentation)
data class MimeType<T>(val status: String, val mime: String, private val clazz: Class<T>) : ResultType<T>() {
data class MimeType<T>(val code: HttpCode, val mime: String, private val clazz: Class<T>) : ResultType<T>() {
override fun applyTo(documentation: OpenApiDocumentation) {
documentation.result(code.status.toString(), clazz)
}
}
data class StatusCode(val code: HttpCode) : ResultType<Unit>() {
override fun applyTo(documentation: OpenApiDocumentation) {
documentation.result(status, clazz)
documentation.result<Unit>(code.status.toString())
}
}
}
Expand Down

0 comments on commit 2051015

Please sign in to comment.