make instance callback return instances

This commit is contained in:
Minecon724 2024-07-31 12:44:10 +02:00
parent 9f72e6cc72
commit 5d7dd31354
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8

View file

@ -1,6 +1,7 @@
package eu.m724.vastapp.vastai.api package eu.m724.vastapp.vastai.api
import eu.m724.vastapp.vastai.ApiFailure import eu.m724.vastapp.vastai.ApiFailure
import eu.m724.vastapp.vastai.data.Instance
import eu.m724.vastapp.vastai.data.User import eu.m724.vastapp.vastai.data.User
import org.chromium.net.CronetException import org.chromium.net.CronetException
import org.chromium.net.UrlRequest import org.chromium.net.UrlRequest
@ -10,7 +11,7 @@ import java.nio.ByteBuffer
import java.nio.charset.CodingErrorAction import java.nio.charset.CodingErrorAction
class InstancesUrlRequestCallback( class InstancesUrlRequestCallback(
val onSuccess: (List<JSONObject>) -> Unit, val onSuccess: (List<Instance>) -> Unit,
val onFailure: (ApiFailure) -> Unit val onFailure: (ApiFailure) -> Unit
) : UrlRequest.Callback() { ) : UrlRequest.Callback() {
@ -42,14 +43,14 @@ class InstancesUrlRequestCallback(
override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) { override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
if (info?.httpStatusCode == 200) { if (info?.httpStatusCode == 200) {
val jsonResponse = JSONObject(stringResponse.toString()) val jsonResponse = JSONObject(stringResponse.toString())
val instances = ArrayList<JSONObject>() val instances = ArrayList<Instance>()
val instancesJson = jsonResponse.getJSONArray("instances") val instancesJson = jsonResponse.getJSONArray("instances")
for (i in 0..<instancesJson.length()) { for (i in 0..<instancesJson.length()) {
instances.add(instancesJson.getJSONObject(i)) instances.add(Instance.fromJson(instancesJson.getJSONObject(i)))
} }
onSuccess(instances) // TODO make it better onSuccess(instances) // TODO handle json errors
} else { } else {
onFailure(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}")) onFailure(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}"))
println("API error: $stringResponse") println("API error: $stringResponse")