Add IdempotencyPlugin for handling Idempotency-Key headers in Ktor applications
This commit is contained in:
parent
9ec8535ff7
commit
eedce74a85
|
|
@ -0,0 +1,26 @@
|
|||
package at.mocode.masterdata.api.plugins
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.util.*
|
||||
import java.time.Duration
|
||||
|
||||
/**
|
||||
* Minimaler Ktor-Plugin-Skeleton für Idempotency-Key Verarbeitung.
|
||||
* Aktuell: Stellt den Header `Idempotency-Key` als Call-Attribut bereit.
|
||||
* Erweiterung (Cache/Short-Circuit) folgt im nächsten Schritt.
|
||||
*/
|
||||
object IdempotencyPlugin {
|
||||
@JvmInline
|
||||
value class Configuration(val ttl: Duration = Duration.ofMinutes(5))
|
||||
|
||||
val IdempotencyKeyAttr: AttributeKey<String> = AttributeKey("IdempotencyKey")
|
||||
|
||||
fun install(application: Application, configuration: Configuration = Configuration()) {
|
||||
application.intercept(ApplicationCallPipeline.Plugins) {
|
||||
val key = call.request.headers["Idempotency-Key"]?.trim()
|
||||
if (!key.isNullOrBlank()) {
|
||||
call.attributes.put(IdempotencyKeyAttr, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user