From ebddf9546551a4c7113996a1b492e33458b7501b Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 7 Aug 2024 11:34:42 +0200 Subject: [PATCH] merge callbacks --- .../vastai/api/InstancesUrlRequestCallback.kt | 65 +++------------ .../vastai/api/JsonUrlRequestCallback.kt | 14 ++-- .../vastai/api/StringUrlRequestCallback.kt | 31 +++++-- .../vastai/api/UserUrlRequestCallback.kt | 83 ++++--------------- 4 files changed, 54 insertions(+), 139 deletions(-) diff --git a/app/src/main/java/eu/m724/vastapp/vastai/api/InstancesUrlRequestCallback.kt b/app/src/main/java/eu/m724/vastapp/vastai/api/InstancesUrlRequestCallback.kt index 29e43c2..edbbb67 100644 --- a/app/src/main/java/eu/m724/vastapp/vastai/api/InstancesUrlRequestCallback.kt +++ b/app/src/main/java/eu/m724/vastapp/vastai/api/InstancesUrlRequestCallback.kt @@ -1,63 +1,18 @@ package eu.m724.vastapp.vastai.api -import eu.m724.vastapp.vastai.ApiFailure +import eu.m724.vastapp.vastai.api.exceptions.ApiException import eu.m724.vastapp.vastai.data.RentedInstance -import org.chromium.net.CronetException -import org.chromium.net.UrlRequest -import org.chromium.net.UrlResponseInfo -import org.json.JSONObject -import java.nio.ByteBuffer -import java.nio.charset.CodingErrorAction class InstancesUrlRequestCallback( - val onSuccess: (List) -> Unit, - val onFailure: (ApiFailure) -> Unit -) : UrlRequest.Callback() { + onSuccess: (List) -> Unit, + onFailure: (ApiException) -> Unit +) : JsonUrlRequestCallback({ json -> + val instances = ArrayList() - private val stringResponse = StringBuilder() - - override fun onRedirectReceived( - request: UrlRequest?, - info: UrlResponseInfo?, - newLocationUrl: String? - ) { - request?.followRedirect() + val instancesJson = json.getJSONArray("instances") + for (i in 0..() - - val instancesJson = jsonResponse.getJSONArray("instances") - for (i in 0.. Unit, - onFailure: (ApiFailure) -> Unit + onFailure: (ApiException) -> Unit ) : StringUrlRequestCallback({ stringResponse -> - try { - val jsonResponse = JSONObject(stringResponse) - onSuccess(jsonResponse) - } catch (e: Exception) { - onFailure(ApiFailure(e.message)) - } + val jsonResponse = JSONObject(stringResponse) // errors are handled in the super class + onSuccess(jsonResponse) }, onFailure) \ No newline at end of file diff --git a/app/src/main/java/eu/m724/vastapp/vastai/api/StringUrlRequestCallback.kt b/app/src/main/java/eu/m724/vastapp/vastai/api/StringUrlRequestCallback.kt index dd9085d..2e421c3 100644 --- a/app/src/main/java/eu/m724/vastapp/vastai/api/StringUrlRequestCallback.kt +++ b/app/src/main/java/eu/m724/vastapp/vastai/api/StringUrlRequestCallback.kt @@ -1,6 +1,9 @@ package eu.m724.vastapp.vastai.api -import eu.m724.vastapp.vastai.ApiFailure +import eu.m724.vastapp.vastai.api.exceptions.ApiException +import eu.m724.vastapp.vastai.api.exceptions.ClientException +import eu.m724.vastapp.vastai.api.exceptions.ServerError +import eu.m724.vastapp.vastai.api.exceptions.UnauthorizedException import org.chromium.net.CronetException import org.chromium.net.UrlRequest import org.chromium.net.UrlResponseInfo @@ -9,7 +12,7 @@ import java.nio.charset.CodingErrorAction open class StringUrlRequestCallback( val onSuccess: (String) -> Unit, - val onFailure: (ApiFailure) -> Unit + val onFailure: (ApiException) -> Unit ) : UrlRequest.Callback() { protected val stringResponse = StringBuilder() @@ -37,15 +40,27 @@ open class StringUrlRequestCallback( } override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) { - if (info?.httpStatusCode == 200) { - onSuccess(stringResponse.toString()) - } else { - onFailure(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}")) - println("API error: ${stringResponse.toString()}") + if (info != null) { + val body = stringResponse.toString() + val statusCode = info.httpStatusCode + + if (statusCode == 200) { + try { + onSuccess(body) + } catch (e: Exception) { // TODO maybe do it differently + onFailure(ClientException(body)) + } + } else if (statusCode >= 500) { + onFailure(ServerError(statusCode, body)) + } else if (statusCode == 403) { + onFailure(UnauthorizedException(body)) + } else { + onFailure(ClientException(body)) + } } } override fun onFailed(request: UrlRequest?, info: UrlResponseInfo?, error: CronetException?) { - onFailure(ApiFailure("Network error: ${error?.message ?: "Unknown"}")) + onFailure(ApiException(error?.message, error)) } } \ No newline at end of file diff --git a/app/src/main/java/eu/m724/vastapp/vastai/api/UserUrlRequestCallback.kt b/app/src/main/java/eu/m724/vastapp/vastai/api/UserUrlRequestCallback.kt index ddef734..6e87550 100644 --- a/app/src/main/java/eu/m724/vastapp/vastai/api/UserUrlRequestCallback.kt +++ b/app/src/main/java/eu/m724/vastapp/vastai/api/UserUrlRequestCallback.kt @@ -1,72 +1,21 @@ package eu.m724.vastapp.vastai.api -import eu.m724.vastapp.vastai.ApiFailure +import eu.m724.vastapp.vastai.api.exceptions.ApiException import eu.m724.vastapp.vastai.data.User -import org.chromium.net.CronetException -import org.chromium.net.UrlRequest -import org.chromium.net.UrlResponseInfo -import org.json.JSONObject -import java.nio.ByteBuffer -import java.nio.charset.CodingErrorAction - class UserUrlRequestCallback( - val onSuccess: (User) -> Unit, - val onFailure: (ApiFailure) -> Unit -) : UrlRequest.Callback() { - - private val stringResponse = StringBuilder() - - override fun onRedirectReceived( - request: UrlRequest?, - info: UrlResponseInfo?, - newLocationUrl: String? - ) { - request?.followRedirect() - } - - override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) { - request?.read(ByteBuffer.allocateDirect(102400)) - } - - override fun onReadCompleted( - request: UrlRequest?, - info: UrlResponseInfo?, - byteBuffer: ByteBuffer? - ) { - byteBuffer?.clear() - request?.read(byteBuffer) - - stringResponse.append(Charsets.UTF_8.newDecoder().onUnmappableCharacter(CodingErrorAction.IGNORE).decode(byteBuffer)) - } - - override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) { - println(stringResponse) // TODO don't do that - if (info?.httpStatusCode == 200) { - try { - val jsonResponse = JSONObject(stringResponse.toString()) - onSuccess( - User( - id = jsonResponse.getString("id"), - username = jsonResponse.getString("username"), - email = jsonResponse.getString("email"), - apiKey = jsonResponse.getString("api_key"), - credit = jsonResponse.getDouble("credit"), - balanceThreshold = jsonResponse.getDouble("balance_threshold"), - balanceThresholdEnabled = jsonResponse.getBoolean("balance_threshold_enabled"), - ) - ) - } catch (e: Exception) { - onFailure(ApiFailure(e.message)) - println("API response error: $stringResponse") - } - } else { - onFailure(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}")) - println("API error: $stringResponse") - } - } - - override fun onFailed(request: UrlRequest?, info: UrlResponseInfo?, error: CronetException?) { - onFailure(ApiFailure("Network error: ${error?.message ?: "Unknown"}")) - } -} \ No newline at end of file + onSuccess: (User) -> Unit, + onFailure: (ApiException) -> Unit +): JsonUrlRequestCallback({ json -> + onSuccess( + User( // TODO move that to a static function in User + id = json.getString("id"), + username = json.getString("username"), + email = json.getString("email"), + apiKey = json.getString("api_key"), + credit = json.getDouble("credit"), + balanceThreshold = json.getDouble("balance_threshold"), + balanceThresholdEnabled = json.getBoolean("balance_threshold_enabled"), + ) + ) +}, onFailure) \ No newline at end of file