Add more information to the Response class

This commit is contained in:
Niels van Velzen 2020-10-10 12:32:49 +02:00
parent 561e6e13fd
commit 4742870eaa
4 changed files with 76 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import io.ktor.client.features.websocket.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import kotlinx.serialization.json.Json
import org.jellyfin.apiclient.model.ClientInfo
import org.jellyfin.apiclient.model.DeviceInfo
@ -126,7 +127,7 @@ open class KtorClient(
body = defaultSerializer().write(requestBody)
}
return Response(response.receive())
return Response(response.receive(), response.status.value, response.headers.toMap())
}
suspend inline fun <reified T> get(

View File

@ -1,12 +1,81 @@
package org.jellyfin.apiclient.api.client
import io.ktor.http.*
import kotlin.reflect.KProperty
/**
* Response from a HTTP class in the [ApiClient].
*
* @param status - See [HttpStatusCode]
*/
class Response<T>(
private val data: T
val content: T,
val status: Int,
val headers: Map<String, List<String>>
) {
/**
* Get the response content using property delegation.
*
* ```kt
* val content by response
* ```
*/
@JvmSynthetic
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = data
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = content
fun getData() = data
/**
* Check if the HTTP response status is equal to a given status.
*
* ```kt
* val isOk = response isStatus 200
* val isOk = response.isStatus(200)
* ```
*/
infix fun isStatus(comparable: Int) = status == comparable
/**
* Check if the HTTP response status is not equal to a given status.
*
* ```kt
* val isNotOk = response isNotStatus 200
* val isNotOk = response.isNotStatus(200)
* ```
*/
infix fun isNotStatus(comparable: Int) = status != comparable
/**
* Check if the HTTP response status is in a given range.
*
* ```kt
* val isOk = response isStatusIn (200 until 300)
* val isOk = response.isStatusIn(IntRange(200, 299))
* ```
*/
infix fun isStatusIn(comparable: IntRange) = status in comparable
/**
* Check if the HTTP response status is in the 2xx range.
*/
fun isStatus200() = this isStatusIn (200 until 300)
/**
* Check if the HTTP response status is in the 4xx range.
*/
fun isStatus400() = this isStatusIn (400 until 500)
/**
* Check if the HTTP response status is in the 5xx range.
*/
fun isStatus500() = this isStatusIn (500 until 600)
/**
* Get a header by name. If multiple headers with the name exist the first is returned.
* Use [getHeaders] to get all headers with [name].
*/
fun getHeader(name: String) = headers[name]?.firstOrNull()
/**
* Get multiple headers sharing the same name. Use [getHeader] to retrieve the first occurrence.
*/
fun getHeaders(name: String) = headers[name]
}

View File

@ -7,7 +7,7 @@ import kotlin.coroutines.EmptyCoroutineContext
abstract class JavaDataCallback<T> : Continuation<Response<T>> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<Response<T>>) = onData(result.getOrNull()?.getData())
override fun resumeWith(result: Result<Response<T>>) = onData(result.getOrNull()?.content)
abstract fun onData(data: T?)
}

View File

@ -19,7 +19,7 @@ class Libraries(
val sessionApi = SessionApi(api)
val userViewsApi = UserViewsApi(api)
val sessionInfo = sessionApi.getSessions(deviceId = api.deviceInfo.id).getData().firstOrNull()
val sessionInfo = sessionApi.getSessions(deviceId = api.deviceInfo.id).content.firstOrNull()
if (sessionInfo == null) println("Unknown session")
val libraries by userViewsApi.getUserViews(sessionInfo!!.userId, includeHidden = false)