merge callbacks
This commit is contained in:
parent
784879393f
commit
ebddf95465
4 changed files with 54 additions and 139 deletions
|
@ -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<RentedInstance>) -> 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) {
|
||||
val jsonResponse = JSONObject(stringResponse.toString())
|
||||
onSuccess: (List<RentedInstance>) -> Unit,
|
||||
onFailure: (ApiException) -> Unit
|
||||
) : JsonUrlRequestCallback({ json ->
|
||||
val instances = ArrayList<RentedInstance>()
|
||||
|
||||
val instancesJson = jsonResponse.getJSONArray("instances")
|
||||
val instancesJson = json.getJSONArray("instances")
|
||||
for (i in 0..<instancesJson.length()) {
|
||||
instances.add(RentedInstance.fromJson(instancesJson.getJSONObject(i)))
|
||||
}
|
||||
|
||||
onSuccess(instances) // TODO handle json errors
|
||||
} 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"}"))
|
||||
}
|
||||
}
|
||||
onSuccess(instances)
|
||||
}, onFailure)
|
|
@ -1,16 +1,12 @@
|
|||
package eu.m724.vastapp.vastai.api
|
||||
|
||||
import eu.m724.vastapp.vastai.ApiFailure
|
||||
import eu.m724.vastapp.vastai.api.exceptions.ApiException
|
||||
import org.json.JSONObject
|
||||
|
||||
class JsonUrlRequestCallback(
|
||||
open class JsonUrlRequestCallback(
|
||||
onSuccess: (JSONObject) -> Unit,
|
||||
onFailure: (ApiFailure) -> Unit
|
||||
onFailure: (ApiException) -> Unit
|
||||
) : StringUrlRequestCallback({ stringResponse ->
|
||||
try {
|
||||
val jsonResponse = JSONObject(stringResponse)
|
||||
val jsonResponse = JSONObject(stringResponse) // errors are handled in the super class
|
||||
onSuccess(jsonResponse)
|
||||
} catch (e: Exception) {
|
||||
onFailure(ApiFailure(e.message))
|
||||
}
|
||||
}, onFailure)
|
|
@ -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())
|
||||
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(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}"))
|
||||
println("API error: ${stringResponse.toString()}")
|
||||
onFailure(ClientException(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailed(request: UrlRequest?, info: UrlResponseInfo?, error: CronetException?) {
|
||||
onFailure(ApiFailure("Network error: ${error?.message ?: "Unknown"}"))
|
||||
onFailure(ApiException(error?.message, error))
|
||||
}
|
||||
}
|
|
@ -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) -> Unit,
|
||||
onFailure: (ApiException) -> Unit
|
||||
): JsonUrlRequestCallback({ json ->
|
||||
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"),
|
||||
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"),
|
||||
)
|
||||
)
|
||||
} 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"}"))
|
||||
}
|
||||
}
|
||||
}, onFailure)
|
Loading…
Reference in a new issue