mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-20 23:57:16 +00:00
bodyOrNull/respondOrNoContent
This commit is contained in:
parent
2cc6126765
commit
ac58b6a7e3
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
## 0.12.8
|
## 0.12.8
|
||||||
|
|
||||||
|
* `Ktor`:
|
||||||
|
* `Client`:
|
||||||
|
* New extension `HttpClient#bodyOrNull` which returns `null` in case when server responded with `No Content` (204)
|
||||||
|
* `Server`:
|
||||||
|
* New extension `ApplicationCall#respondOrNoContent` which responds `No Content` (204) when passed data is null
|
||||||
|
|
||||||
## 0.12.7
|
## 0.12.7
|
||||||
|
|
||||||
* `Repos`:
|
* `Repos`:
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package dev.inmo.micro_utils.ktor.client
|
||||||
|
|
||||||
|
import io.ktor.client.call.body
|
||||||
|
import io.ktor.client.statement.HttpResponse
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
|
||||||
|
suspend inline fun <reified T : Any> HttpResponse.bodyOrNull() = takeIf {
|
||||||
|
status == HttpStatusCode.OK
|
||||||
|
} ?.body<T>()
|
@ -0,0 +1,15 @@
|
|||||||
|
package dev.inmo.micro_utils.ktor.server
|
||||||
|
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.server.application.ApplicationCall
|
||||||
|
import io.ktor.server.response.respond
|
||||||
|
|
||||||
|
suspend inline fun <reified T : Any> ApplicationCall.respondOrNoContent(
|
||||||
|
data: T?
|
||||||
|
) {
|
||||||
|
if (data == null) {
|
||||||
|
respond(HttpStatusCode.NoContent)
|
||||||
|
} else {
|
||||||
|
respond(data)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user