Best Fuel code snippet using com.github.kittinunf.fuel.core.requests.RequestTaskCallbacks
Deserializable.kt
Source:Deserializable.kt
2import com.github.kittinunf.fuel.Fuel3import com.github.kittinunf.fuel.core.deserializers.EmptyDeserializer4import com.github.kittinunf.fuel.core.requests.CancellableRequest5import com.github.kittinunf.fuel.core.requests.DefaultBody6import com.github.kittinunf.fuel.core.requests.RequestTaskCallbacks7import com.github.kittinunf.fuel.core.requests.suspendable8import com.github.kittinunf.fuel.core.requests.toTask9import com.github.kittinunf.result.Result10import com.github.kittinunf.result.getOrElse11import com.github.kittinunf.result.map12import com.github.kittinunf.result.mapError13import java.io.InputStream14import java.io.Reader15import kotlin.jvm.Throws16/**17 * Generic interface for [Response] deserialization.18 *19 * @note you are responsible of using the [Response] [Body] [InputStream] and closing it when you're done. Failing to do20 * so can result in hanging connections if used in conjunction with [com.github.kittinunf.fuel.toolbox.HttpClient].21 *22 * @see ResponseDeserializable23 */24interface Deserializable<out T : Any> {25 /**26 * Deserialize [response] into [T]27 *28 * @param response [Response] the incoming response29 * @return [T] the instance of [T]30 */31 fun deserialize(response: Response): T32}33interface ResponseDeserializable<out T : Any> : Deserializable<T> {34 override fun deserialize(response: Response): T {35 response.body.toStream().use { stream ->36 return deserialize(stream)37 ?: deserialize(stream.reader())38 ?: reserialize(response, stream).let {39 deserialize(response.data)40 ?: deserialize(String(response.data))41 ?: throw FuelError.wrap(IllegalStateException(42 "One of deserialize(ByteArray) or deserialize(InputStream) or deserialize(Reader) or " +43 "deserialize(String) must be implemented"44 ))45 }46 }47 }48 private fun reserialize(response: Response, stream: InputStream): Response {49 val length = response.body.length50 response.body = DefaultBody.from({ stream }, length?.let { l -> { l } })51 return response52 }53 /**54 * Deserialize into [T] from an [InputStream]55 *56 * @param inputStream [InputStream] source bytes57 * @return [T] deserialized instance of [T] or null when not applied58 */59 fun deserialize(inputStream: InputStream): T? = null60 /**61 * Deserialize into [T] from a [Reader]62 *63 * @param reader [Reader] source bytes64 * @return [T] deserialized instance of [T] or null when not applied65 */66 fun deserialize(reader: Reader): T? = null67 /**68 * Deserialize into [T] from a [ByteArray]69 *70 * @note it is more efficient to implement the [InputStream] variant.71 *72 * @param bytes [ByteArray] source bytes73 * @return [T] deserialized instance of [T] or null when not applied74 */75 fun deserialize(bytes: ByteArray): T? = null76 /**77 * Deserialize into [T] from a [String]78 *79 * @note it is more efficient to implement the [Reader] variant.80 *81 * @param content [String] source bytes82 * @return [T] deserialized instance of [T] or null when not applied83 */84 fun deserialize(content: String): T? = null85}86/**87 * Deserialize the [Response] to the [this] into a [T] using [U]88 *89 * @see ResponseResultHandler90 *91 * @param deserializable [U] the instance that performs deserialization92 * @param handler [ResponseResultHandler<T>] handler that has a [Result]93 * @return [CancellableRequest] the request that can be cancelled94 */95fun <T : Any, U : Deserializable<T>> Request.response(deserializable: U, handler: ResponseResultHandler<T>): CancellableRequest =96 response(deserializable,97 { request, response, value -> handler(request, response, Result.Success(value)) },98 { request, response, error -> handler(request, response, Result.Failure(error)) }99 )100/**101 * Deserialize the [Response] to the [this] into a [T] using [U]102 *103 * @see ResultHandler104 *105 * @param deserializable [U] the instance that performs deserialization106 * @param handler [ResultHandler<T>] handler that has a [Result]107 * @return [CancellableRequest] the request that can be cancelled108 */109fun <T : Any, U : Deserializable<T>> Request.response(deserializable: U, handler: ResultHandler<T>): CancellableRequest =110 response(deserializable,111 { _, _, value -> handler(Result.Success(value)) },112 { _, _, error -> handler(Result.Failure(error)) }113 )114/**115 * Deserialize the [Response] to the [this] into a [T] using [U]116 *117 * @see ResponseHandler118 *119 * @param deserializable [U] the instance that performs deserialization120 * @param handler [ResponseHandler<T>] handler that has dedicated paths for success and failure121 * @return [CancellableRequest] the request that can be cancelled122 */123fun <T : Any, U : Deserializable<T>> Request.response(deserializable: U, handler: ResponseHandler<T>): CancellableRequest =124 response(deserializable,125 { request, response, value -> handler.success(request, response, value) },126 { request, response, error -> handler.failure(request, response, error) }127 )128/**129 * Deserialize the [Response] to the [this] into a [T] using [U]130 *131 * @see Handler132 *133 * @param deserializable [U] the instance that performs deserialization134 * @param handler [Handler<T>] handler that has dedicated paths for success and failure135 * @return [CancellableRequest] the request that can be cancelled136 */137fun <T : Any, U : Deserializable<T>> Request.response(deserializable: U, handler: Handler<T>): CancellableRequest =138 response(deserializable,139 { _, _, value -> handler.success(value) },140 { _, _, error -> handler.failure(error) }141 )142/**143 * Deserialize the [Response] to the [this] into a [T] using [U]144 *145 * @note not async, use the variations with a handler instead.146 *147 * @throws Exception if there is an internal library error, not related to Network or Deserialization148 *149 * @param deserializable [U] the instance that performs deserialization150 * @return [ResponseResultOf<T>] the response result of151 */152fun <T : Any, U : Deserializable<T>> Request.response(deserializable: U): ResponseResultOf<T> {153 // First execute the network request and catch any issues154 val rawResponse = runCatching { toTask().call() }155 .onFailure { error ->156 FuelError.wrap(error, Response.error(url)).also {157 return Triple(this, it.response, Result.error(it))158 }159 }160 .getOrThrow()161 // By this time it should have a response, but deserialization might fail162 return runCatching { Triple(this, rawResponse, Result.Success(deserializable.deserialize(rawResponse))) }163 .recover { error -> Triple(this, rawResponse, Result.Failure(FuelError.wrap(error, rawResponse))) }164 .getOrThrow()165}166/**167 * Ignore the response result168 *169 * Use this method to avoid huge memory allocation when using [com.github.kittinunf.fuel.core.requests.download]170 * to a large download and without using the result [ByteArray]171 *172 * @see [com.github.kittinunf.fuel.core.Request.response]173 *174 * @note not async, use the variations with a handler instead.175 *176 * @throws Exception if there is an internal library error, not related to Network177 */178fun Request.responseUnit(): ResponseResultOf<Unit> = response(EmptyDeserializer)179private fun <T : Any, U : Deserializable<T>> Request.response(180 deserializable: U,181 success: (Request, Response, T) -> Unit,182 failure: (Request, Response, FuelError) -> Unit183): CancellableRequest {184 val asyncRequest = RequestTaskCallbacks(185 request = this,186 onSuccess = { response ->187 // The network succeeded but deserialization might fail188 val deliverable = Result.of<T, Exception> { deserializable.deserialize(response) }189 executionOptions.callback {190 deliverable.fold(191 { success(this, response, it) },192 { failure(this, response, FuelError.wrap(it, response).also { error ->193 Fuel.trace { "[Deserializable] unfold failure: \n\r$error" } })194 }195 )196 }197 },198 onFailure = { error, response ->...
RequestTaskCallbacks.kt
Source:RequestTaskCallbacks.kt
...13 * @param task [Callable<Response>] the task to execute (and perform callbacks on)14 * @param onSuccess [RequestSuccessCallback] the success callback, called when everything went fine15 * @param onFailure [RequestFailureCallback] the failure callback, called when an error occurred16 */17internal class RequestTaskCallbacks(18 private val request: Request,19 private val task: Callable<Response> = request.toTask(),20 private val onSuccess: RequestSuccessCallback,21 private val onFailure: RequestFailureCallback22) : Callable<Response> {23 override fun call(): Response {24 Fuel.trace { "[RequestTaskCallbacks] start request task\n\r\t$request" }25 return runCatching { task.call() }26 .mapCatching { it -> it.also { onSuccess(it) } }27 .getOrElse { error -> FuelError.wrap(error).also { onFailure(it, it.response) }.response }28 }29}...
RequestTaskCallbacks
Using AI Code Generation
1@file:Suppress("unused")2import com.github.kittinunf.fuel.core.FuelError3import com.github.kittinunf.fuel.core.Request4import com.github.kittinunf.fuel.core.Response5import com.github.kittinunf.result.Result6interface RequestTaskCallbacks {7 fun success(request: Request, response: Response) {8 }9 fun failure(request: Request, response: Response, error: FuelError) {10 }11 fun finished(request: Request, response: Response, result: Result<ByteArray, FuelError>) {12 }13}14class RequestTaskCallback(15 val success: (Request, Response) -> Unit = { _, _ -> },16 val failure: (Request, Response, FuelError) -> Unit = { _, _, _ -> },17 val finished: (Request, Response, Result<ByteArray, FuelError>) -> Unit = { _, _, _ -> }18) : RequestTaskCallbacks {19 override fun success(request: Request, response: Response) {20 success(request, response)21 }22 override fun failure(request: Request, response: Response, error: FuelError) {23 failure(request, response, error)24 }25 override fun finished(request: Request, response: Response, result: Result<ByteArray, FuelError>)
RequestTaskCallbacks
Using AI Code Generation
1FuelManager.instance.baseHeaders = mapOf("foo" to "bar")2FuelManager.instance.baseParams = listOf("foo" to "bar")3FuelManager.instance.baseHeaders = mapOf("foo" to "bar")4FuelManager.instance.baseParams = listOf("foo" to "bar")5FuelManager.instance.baseHeaders = mapOf("foo" to "bar")6FuelManager.instance.baseParams = listOf("foo" to "bar")7FuelManager.instance.baseHeaders = mapOf("foo" to "bar")8FuelManager.instance.baseParams = listOf("foo" to "bar")9FuelManager.instance.baseHeaders = mapOf("foo" to "bar")10FuelManager.instance.baseParams = listOf("foo" to "bar")11FuelManager.instance.baseHeaders = mapOf("foo" to "bar")12FuelManager.instance.baseParams = listOf("foo" to "bar")13FuelManager.instance.baseHeaders = mapOf("foo" to "bar")14FuelManager.instance.baseParams = listOf("foo" to "bar")15FuelManager.instance.baseHeaders = mapOf("foo" to "bar")16FuelManager.instance.baseParams = listOf("foo" to "bar")17FuelManager.instance.baseHeaders = mapOf("foo" to "bar")18FuelManager.instance.baseParams = listOf("foo" to "bar")19FuelManager.instance.baseHeaders = mapOf("foo" to "bar")20FuelManager.instance.baseParams = listOf("foo" to "bar")21FuelManager.instance.baseHeaders = mapOf("foo" to "bar")22FuelManager.instance.baseParams = listOf("foo" to "bar")
RequestTaskCallbacks
Using AI Code Generation
1import com.github.kittinunf.fuel.core.requests.RequestTaskCallbacks2class MainActivity : AppCompatActivity(), RequestTaskCallbacks {3 override fun onCreate(savedInstanceState: Bundle?) {4 super.onCreate(savedInstanceState)5 setContentView(R.layout.activity_main)6 val (data, error) = result7 Log.d("TAG", "request: ${request}")8 Log.d("TAG", "response: ${response}")9 Log.d("TAG", "data: ${data}")10 Log.d("TAG", "error: ${error}")11 })12 }13}14 val (data, error) = result15 Log.d("TAG", "request: ${request}")16 Log.d("TAG", "response: ${response}")17 Log.d("TAG", "data: ${data}")18 Log.d("TAG", "error: ${error}")19})20 val (data, error) = result21 Log.d("TAG", "request: ${request}")22 Log.d("TAG", "response: ${response}")23 Log.d("TAG", "data: ${data}")24 Log.d("TAG", "error: ${error}")25})26 val (data, error) = result27 Log.d("TAG", "request: ${request}")28 Log.d("TAG", "response: ${response}")29 Log.d("TAG", "data: ${data}")30 Log.d("TAG", "error: ${error}")31})32 val (data, error) = result33 Log.d("TAG", "request: ${request}")34 Log.d("TAG", "response: ${response}")35 Log.d("TAG
RequestTaskCallbacks
Using AI Code Generation
1 .responseObject(RequestTaskCallback { request, response, result ->2 })3 .responseString(RequestTaskCallback { request, response, result ->4 })5 .responseByteArray(RequestTaskCallback { request, response, result ->6 })7 .responseFile(RequestTaskCallback { request, response, result ->8 })9 .responseStream(RequestTaskCallback { request, response, result ->10 })11 .response(RequestTaskCallback { request, response, result ->12 })13 .responseJson(RequestTaskCallback { request, response, result ->14 })15 .responseXml(RequestTaskCallback { request, response, result ->
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!