Skip to content

Commit

Permalink
create a application call extension in order to make it more easier t…
Browse files Browse the repository at this point in the history
…o return the correct content type
  • Loading branch information
JohannesHepp committed Aug 28, 2024
1 parent a86aa1f commit 9a9c0d8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 47 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ install(StatusPages) {
cause.message,
"https://example.org"
)
call.respondText(
problem.toJson(),
ContentType.Application.ProblemJson,
httpStatus,
)
call.respondProblem(httpStatus, problem)
}
}
```
Expand Down
86 changes: 44 additions & 42 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,73 @@ val ktorVersion: String by project
val kotestVersion: String by project

plugins {
kotlin("jvm") version "2.0.0"
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.0"
id("maven-publish")
id("jacoco")
kotlin("jvm") version "2.0.0"
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.0"
id("maven-publish")
id("jacoco")
}

group = "schwarz.it"
version = "1.0.0"

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation(kotlin("reflect"))
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation(kotlin("reflect"))
implementation("io.ktor:ktor-server-core:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")

testImplementation(kotlin("test"))
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
testImplementation(kotlin("test"))
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
}

tasks.test {
useJUnitPlatform()
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
jvmToolchain(21)
}

publishing {
publications {
create<MavenPublication>("mavenPublication") {
from(components["java"])
}
}
repositories {
maven {
name = "artifactory"
credentials {
username = project.findProperty("artifactoryUsername") as String? ?: System.getenv("ARTIFACTORY_USER")
password = project.findProperty("artifactoryPassword") as String? ?: System.getenv("ARTIFACTORY_PASSWORD")
}
url = uri("https://schwarzit.jfrog.io/artifactory/xx-sit-odj-psftp-maven-release-local/")
}
}
publications {
create<MavenPublication>("mavenPublication") {
from(components["java"])
}
}
repositories {
maven {
name = "artifactory"
credentials {
username = project.findProperty("artifactoryUsername") as String? ?: System.getenv("ARTIFACTORY_USER")
password =
project.findProperty("artifactoryPassword") as String? ?: System.getenv("ARTIFACTORY_PASSWORD")
}
url = uri("https://schwarzit.jfrog.io/artifactory/xx-sit-odj-psftp-maven-release-local/")
}
}
}

jacoco {
reportsDirectory.set(layout.buildDirectory.dir("reports/jacoco/"))
reportsDirectory.set(layout.buildDirectory.dir("reports/jacoco/"))
}

with(tasks) {
test {
useJUnitPlatform()
finalizedBy(jacocoTestReport)
}
test {
useJUnitPlatform()
finalizedBy(jacocoTestReport)
}

jacocoTestReport {
dependsOn(test)
reports {
xml.required.set(true)
csv.required.set(false)
html.required.set(true)
xml.outputLocation.set(layout.buildDirectory.file("reports/jacoco/report.xml"))
html.outputLocation.set(layout.buildDirectory.dir("reports/jacoco/html"))
}
}
jacocoTestReport {
dependsOn(test)
reports {
xml.required.set(true)
csv.required.set(false)
html.required.set(true)
xml.outputLocation.set(layout.buildDirectory.file("reports/jacoco/report.xml"))
html.outputLocation.set(layout.buildDirectory.dir("reports/jacoco/html"))
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/schwarz/it/problem/details/Problem.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package schwarz.it.problem.details

import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.response.respondText
import kotlinx.serialization.json.Json

/**
Expand All @@ -13,6 +16,20 @@ private const val TYPE_DEFAULT = "about:blank"
*/
public fun problem(block: ProblemBuilder.() -> Unit): Problem = ProblemBuilder().apply(block).build()

/**
* Application Call extension in order to return problem details with correct content type in ktor
*/
suspend fun ApplicationCall.respondProblem(
status: HttpStatusCode,
problem: Problem,
) {
respondText(
problem.toJson(),
ContentType.Application.ProblemJson,
status,
)
}

/**
* Builder for a problem detail including all mutable components
* Problem implements RFC 9457 (https://datatracker.ietf.org/doc/rfc9457/)
Expand Down

0 comments on commit 9a9c0d8

Please sign in to comment.