make instance callback return instances
This commit is contained in:
parent
9f72e6cc72
commit
5d7dd31354
1 changed files with 5 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
|||
package eu.m724.vastapp.vastai.api
|
||||
|
||||
import eu.m724.vastapp.vastai.ApiFailure
|
||||
import eu.m724.vastapp.vastai.data.Instance
|
||||
import eu.m724.vastapp.vastai.data.User
|
||||
import org.chromium.net.CronetException
|
||||
import org.chromium.net.UrlRequest
|
||||
|
@ -10,7 +11,7 @@ import java.nio.ByteBuffer
|
|||
import java.nio.charset.CodingErrorAction
|
||||
|
||||
class InstancesUrlRequestCallback(
|
||||
val onSuccess: (List<JSONObject>) -> Unit,
|
||||
val onSuccess: (List<Instance>) -> Unit,
|
||||
val onFailure: (ApiFailure) -> Unit
|
||||
) : UrlRequest.Callback() {
|
||||
|
||||
|
@ -42,14 +43,14 @@ class InstancesUrlRequestCallback(
|
|||
override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
|
||||
if (info?.httpStatusCode == 200) {
|
||||
val jsonResponse = JSONObject(stringResponse.toString())
|
||||
val instances = ArrayList<JSONObject>()
|
||||
val instances = ArrayList<Instance>()
|
||||
|
||||
val instancesJson = jsonResponse.getJSONArray("instances")
|
||||
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 {
|
||||
onFailure(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}"))
|
||||
println("API error: $stringResponse")
|
||||
|
|
Loading…
Reference in a new issue