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
 | 
					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 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(
 | 
					class InstancesUrlRequestCallback(
 | 
				
			||||||
    val onSuccess: (List<RentedInstance>) -> Unit,
 | 
					    onSuccess: (List<RentedInstance>) -> Unit,
 | 
				
			||||||
    val onFailure: (ApiFailure) -> Unit
 | 
					    onFailure: (ApiException) -> Unit
 | 
				
			||||||
) : UrlRequest.Callback() {
 | 
					) : JsonUrlRequestCallback({ json ->
 | 
				
			||||||
 | 
					 | 
				
			||||||
    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())
 | 
					 | 
				
			||||||
    val instances = ArrayList<RentedInstance>()
 | 
					    val instances = ArrayList<RentedInstance>()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            val instancesJson = jsonResponse.getJSONArray("instances")
 | 
					    val instancesJson = json.getJSONArray("instances")
 | 
				
			||||||
    for (i in 0..<instancesJson.length()) {
 | 
					    for (i in 0..<instancesJson.length()) {
 | 
				
			||||||
        instances.add(RentedInstance.fromJson(instancesJson.getJSONObject(i)))
 | 
					        instances.add(RentedInstance.fromJson(instancesJson.getJSONObject(i)))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            onSuccess(instances) // TODO handle json errors
 | 
					    onSuccess(instances)
 | 
				
			||||||
        } else {
 | 
					}, onFailure)
 | 
				
			||||||
            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"}"))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,16 +1,12 @@
 | 
				
			||||||
package eu.m724.vastapp.vastai.api
 | 
					package eu.m724.vastapp.vastai.api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import eu.m724.vastapp.vastai.ApiFailure
 | 
					import eu.m724.vastapp.vastai.api.exceptions.ApiException
 | 
				
			||||||
import org.json.JSONObject
 | 
					import org.json.JSONObject
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class JsonUrlRequestCallback(
 | 
					open class JsonUrlRequestCallback(
 | 
				
			||||||
    onSuccess: (JSONObject) -> Unit,
 | 
					    onSuccess: (JSONObject) -> Unit,
 | 
				
			||||||
    onFailure: (ApiFailure) -> Unit
 | 
					    onFailure: (ApiException) -> Unit
 | 
				
			||||||
) : StringUrlRequestCallback({ stringResponse ->
 | 
					) : StringUrlRequestCallback({ stringResponse ->
 | 
				
			||||||
    try {
 | 
					    val jsonResponse = JSONObject(stringResponse) // errors are handled in the super class
 | 
				
			||||||
        val jsonResponse = JSONObject(stringResponse)
 | 
					 | 
				
			||||||
    onSuccess(jsonResponse)
 | 
					    onSuccess(jsonResponse)
 | 
				
			||||||
    } catch (e: Exception) {
 | 
					 | 
				
			||||||
        onFailure(ApiFailure(e.message))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}, onFailure)
 | 
					}, onFailure)
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,9 @@
 | 
				
			||||||
package eu.m724.vastapp.vastai.api
 | 
					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.CronetException
 | 
				
			||||||
import org.chromium.net.UrlRequest
 | 
					import org.chromium.net.UrlRequest
 | 
				
			||||||
import org.chromium.net.UrlResponseInfo
 | 
					import org.chromium.net.UrlResponseInfo
 | 
				
			||||||
| 
						 | 
					@ -9,7 +12,7 @@ import java.nio.charset.CodingErrorAction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
open class StringUrlRequestCallback(
 | 
					open class StringUrlRequestCallback(
 | 
				
			||||||
    val onSuccess: (String) -> Unit,
 | 
					    val onSuccess: (String) -> Unit,
 | 
				
			||||||
    val onFailure: (ApiFailure) -> Unit
 | 
					    val onFailure: (ApiException) -> Unit
 | 
				
			||||||
) : UrlRequest.Callback() {
 | 
					) : UrlRequest.Callback() {
 | 
				
			||||||
    protected val stringResponse = StringBuilder()
 | 
					    protected val stringResponse = StringBuilder()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,15 +40,27 @@ open class StringUrlRequestCallback(
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
 | 
					    override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
 | 
				
			||||||
        if (info?.httpStatusCode == 200) {
 | 
					        if (info != null) {
 | 
				
			||||||
            onSuccess(stringResponse.toString())
 | 
					            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 {
 | 
					            } else {
 | 
				
			||||||
            onFailure(ApiFailure("${info?.httpStatusCode} ${info?.httpStatusText}"))
 | 
					                onFailure(ClientException(body))
 | 
				
			||||||
            println("API error: ${stringResponse.toString()}")
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun onFailed(request: UrlRequest?, info: UrlResponseInfo?, error: CronetException?) {
 | 
					    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
 | 
					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 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(
 | 
					class UserUrlRequestCallback(
 | 
				
			||||||
    val onSuccess: (User) -> Unit,
 | 
					    onSuccess: (User) -> Unit,
 | 
				
			||||||
    val onFailure: (ApiFailure) -> Unit
 | 
					    onFailure: (ApiException) -> Unit
 | 
				
			||||||
) : UrlRequest.Callback() {
 | 
					): JsonUrlRequestCallback({ json ->
 | 
				
			||||||
 | 
					 | 
				
			||||||
    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(
 | 
					    onSuccess(
 | 
				
			||||||
                    User(
 | 
					        User( // TODO move that to a static function in User
 | 
				
			||||||
                        id = jsonResponse.getString("id"),
 | 
					            id = json.getString("id"),
 | 
				
			||||||
                        username = jsonResponse.getString("username"),
 | 
					            username = json.getString("username"),
 | 
				
			||||||
                        email = jsonResponse.getString("email"),
 | 
					            email = json.getString("email"),
 | 
				
			||||||
                        apiKey = jsonResponse.getString("api_key"),
 | 
					            apiKey = json.getString("api_key"),
 | 
				
			||||||
                        credit = jsonResponse.getDouble("credit"),
 | 
					            credit = json.getDouble("credit"),
 | 
				
			||||||
                        balanceThreshold = jsonResponse.getDouble("balance_threshold"),
 | 
					            balanceThreshold = json.getDouble("balance_threshold"),
 | 
				
			||||||
                        balanceThresholdEnabled = jsonResponse.getBoolean("balance_threshold_enabled"),
 | 
					            balanceThresholdEnabled = json.getBoolean("balance_threshold_enabled"),
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
            } catch (e: Exception) {
 | 
					}, onFailure)
 | 
				
			||||||
                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"}"))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue