ultrasonic-app-subsonic-and.../core/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/Extensions.kt

86 lines
2.8 KiB
Kotlin
Raw Normal View History

package org.moire.ultrasonic.api.subsonic
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.IOException
import okhttp3.ResponseBody
import org.moire.ultrasonic.api.subsonic.response.StreamResponse
import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse
import retrofit2.Response
/**
* Converts a Response to a StreamResponse
*/
fun Response<out ResponseBody>.toStreamResponse(): StreamResponse {
val response = this
return if (response.isSuccessful) {
val responseBody = response.body()
val contentType = responseBody?.contentType()
if (
contentType != null &&
2021-12-20 22:18:55 +01:00
contentType.type.equals("application", true) &&
contentType.subtype.equals("json", true)
) {
val error = SubsonicAPIClient.jacksonMapper.readValue<SubsonicResponse>(
responseBody.byteStream()
)
StreamResponse(apiError = error.error, responseHttpCode = response.code())
} else {
StreamResponse(
stream = responseBody?.byteStream(),
responseHttpCode = response.code()
)
}
} else {
StreamResponse(responseHttpCode = response.code())
}
}
/**
* This extension checks API call results for errors, API version, etc
* It creates Exceptions from the results returned by the Subsonic API
*/
@Suppress("ThrowsCount")
2021-12-20 22:18:55 +01:00
fun <T : SubsonicResponse> Response<T>.throwOnFailure(): Response<T> {
val response = this
if (response.isSuccessful && response.body()!!.status === SubsonicResponse.Status.OK) {
Merge update build tools #755 by Holger Müller Squashed commit of the following: commit 4491c65b1bfa8f507e9c998878254d61bc52f962 Merge: 51ff716f 77865a14 Author: tzugen <67737443+tzugen@users.noreply.github.com> Date: Tue Jun 21 20:50:05 2022 +0200 Merge branch 'develop' into gradle-update commit 51ff716ff5fc8aeb29bc557844747c0127ab1f78 Author: Holger Müller <github@euhm.de> Date: Tue Jun 21 20:38:52 2022 +0200 fixed lint warning commit 18c31a5704e7d92a04009e2f3ed8f8b9ca28b577 Author: Holger Müller <github@euhm.de> Date: Tue Jun 21 20:38:35 2022 +0200 fixed lint warning commit 603654c262ed86a66b4127513764cd252cdedfff Author: Holger Müller <github@euhm.de> Date: Tue Jun 21 20:37:51 2022 +0200 API is > lollipop ... target removed commit b38a7211de0f206465640d39f8f3189695afbd38 Author: Holger Müller <github@euhm.de> Date: Tue Jun 21 20:37:07 2022 +0200 new created after fixes commit 4929a526f7e9154755cae2c16fdf431550ae2c2a Author: tzugen <tzugen@riseup.net> Date: Tue Jun 21 10:43:16 2022 +0200 Disable ObsoleteLintCustomCheck commit d0c30f0b6b1d4de2f3c46a166a4f1058223ad3a8 Author: tzugen <tzugen@riseup.net> Date: Tue Jun 21 10:14:06 2022 +0200 Update more libs commit e2fa447bbfbd19119393297c19de0f3237e1ccd3 Merge: d4ead495 ff9c7b24 Author: tzugen <67737443+tzugen@users.noreply.github.com> Date: Tue Jun 21 09:47:03 2022 +0200 Merge branch 'develop' into gradle-update commit d4ead49548d11f51dd9c29478972dcd4553d352a Merge: 2dac6a7e 9a73d72f Author: Holger Müller <github@euhm.de> Date: Tue Jun 21 08:50:42 2022 +0200 merged with develop branch commit 2dac6a7e01e4ce5dc619426553ea08743bd9524e Author: Holger Müller <github@euhm.de> Date: Mon Jun 20 21:45:22 2022 +0200 update to android image tag 2022.06.1 commit f3dc259c390c256a66f96c3f003e34daf0fdea14 Author: Holger Müller <github@euhm.de> Date: Mon Jun 20 20:56:37 2022 +0200 rebuild lint-baseline.xml commit c71bc1212a8570f8ee99a80926c7b3b6036c672f Author: Holger Müller <github@euhm.de> Date: Mon Jun 20 20:55:00 2022 +0200 removed unneeded cast commit eca136dabedab838e8a02be1a6fd740c4d0934ec Author: Holger Müller <github@euhm.de> Date: Fri Jun 17 23:58:37 2022 +0200 commit signed commit 540f47633485cddeb4e68626532e54707a66bab8 Author: Holger Müller <github@euhm.de> Date: Fri Jun 17 23:40:59 2022 +0200 commit signed Signed-off-by: Holger Müller <github@euhm.de> commit 986bd013a49afc56ff8992634f2ce126339eecfc Author: Holger Müller <github@euhm.de> Date: Fri Jun 17 23:27:20 2022 +0200 push to latest gradle version, set targetSdk to 33 Signed-off-by: tzugen <tzugen@riseup.net>
2022-06-21 21:05:58 +02:00
return this
}
if (!response.isSuccessful) {
throw IOException("Server error, code: " + response.code())
} else if (
response.body()!!.status === SubsonicResponse.Status.ERROR &&
response.body()!!.error != null
) {
throw SubsonicRESTException(response.body()!!.error!!)
} else {
throw IOException("Failed to perform request: " + response.code())
}
}
/**
* This extension checks API call results for errors, API version, etc
* @return Boolean: True if everything was ok, false if an error was found
*/
fun Response<out SubsonicResponse>.falseOnFailure(): Boolean {
return (this.isSuccessful && this.body()!!.status === SubsonicResponse.Status.OK)
}
/**
* This call wraps Subsonic API calls so their results can be checked for errors, API version, etc
* It creates Exceptions from a StreamResponse
*/
fun StreamResponse.throwOnFailure(): StreamResponse {
val response = this
if (response.hasError() || response.stream == null) {
if (response.apiError != null) {
throw SubsonicRESTException(response.apiError)
} else {
throw IOException(
"Failed to make endpoint request, code: " + response.responseHttpCode
)
}
}
return this
}