How to use Request.suspendable method of com.github.kittinunf.fuel.core.requests.SuspendableRequest class

Best Fuel code snippet using com.github.kittinunf.fuel.core.requests.SuspendableRequest.Request.suspendable

SuspendableRequest.kt

Source:SuspendableRequest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core.requests2import com.github.kittinunf.fuel.Fuel3import com.github.kittinunf.fuel.core.FuelError4import com.github.kittinunf.fuel.core.HttpException5import com.github.kittinunf.fuel.core.Request6import com.github.kittinunf.fuel.core.Response7import com.github.kittinunf.result.Result8/**9 * Coroutine version of [RequestTask]. Turns a [Request] into an executable, suspendable, coroutine.10 */11class SuspendableRequest private constructor(private val wrapped: Request) : Request by wrapped {12 private val interruptCallback by lazy { executor.interruptCallback }13 private val executor by lazy { request.executionOptions }14 private val client by lazy { executor.client }15 private fun prepareRequest(request: Request): Request = executor.requestTransformer(request)16 private suspend fun executeRequest(request: Request): Pair<Request, Response> {17 return runCatching { Pair(request, client.awaitRequest(request)) }18 .recover { error -> throw FuelError.wrap(error, Response(url)) }19 .getOrThrow()20 }21 private fun prepareResponse(result: Pair<Request, Response>): Response {22 val (request, response) = result23 return runCatching { executor.responseTransformer(request, response) }24 .mapCatching { transformedResponse ->25 val valid = executor.responseValidator(transformedResponse)26 if (valid) transformedResponse27 else throw FuelError.wrap(HttpException(transformedResponse.statusCode, transformedResponse.responseMessage), transformedResponse)28 }29 .recover { error -> throw FuelError.wrap(error, response) }30 .getOrThrow()31 }32 suspend fun awaitResult(): Result<Response, FuelError> {33 return runCatching { prepareRequest(request) }34 .mapCatching { executeRequest(it) }35 .mapCatching { pair ->36 // Nested runCatching so response can be rebound37 runCatching { prepareResponse(pair) }38 .recover { error ->39 error.also { Fuel.trace { "[RequestTask] execution error\n\r\t$error" } }40 throw FuelError.wrap(error, pair.second)41 }42 .getOrThrow()43 }44 .onFailure { error ->45 Fuel.trace { "[RequestTask] on failure ${(error as? FuelError)?.exception ?: error}" }46 if (error is FuelError && error.causedByInterruption) {47 Fuel.trace { "[RequestTask] execution error\n\r\t$error" }48 interruptCallback.invoke(request)49 }50 }51 .map { Result.Success(it) }52 .recover { Result.Failure(it as FuelError) }53 .getOrThrow()54 }55 @Throws(FuelError::class)56 suspend fun await(): Response {57 return awaitResult().get()58 }59 companion object {60 private val FEATURE = SuspendableRequest::class.java.canonicalName61 fun enableFor(request: Request): SuspendableRequest {62 // Makes sure the "newest" request is stored, although it should always be the same.63 val current = request.enabledFeatures[FEATURE] ?: SuspendableRequest(request)64 if (request !== current) {65 request.enabledFeatures[FEATURE] = current66 }67 return current as SuspendableRequest68 }69 }70}71fun Request.suspendable() = SuspendableRequest.enableFor(this)...

Full Screen

Full Screen

RequestTask.kt

Source:RequestTask.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core.requests2import com.github.kittinunf.fuel.Fuel3import com.github.kittinunf.fuel.core.FuelError4import com.github.kittinunf.fuel.core.HttpException5import com.github.kittinunf.fuel.core.Request6import com.github.kittinunf.fuel.core.Response7import java.util.concurrent.Callable8private typealias RequestTaskResult = Pair<Request, Response>9/**10 * Synchronous version of [SuspendableRequest]. Turns a [Request] into a [Callable]11 */12internal class RequestTask(internal val request: Request) : Callable<Response> {13 private val interruptCallback by lazy { executor.interruptCallback }14 private val executor by lazy { request.executionOptions }15 private val client by lazy { executor.client }16 private fun prepareRequest(request: Request): Request = executor.requestTransformer(request)17 @Throws(FuelError::class)18 private fun executeRequest(request: Request): RequestTaskResult {19 return runCatching { Pair(request, client.executeRequest(request)) }20 .recover { error -> throw FuelError.wrap(error, Response(request.url)) }21 .getOrThrow()22 }23 @Throws(FuelError::class)24 private fun prepareResponse(result: RequestTaskResult): Response {25 val (request, response) = result26 return runCatching { executor.responseTransformer(request, response) }27 .mapCatching { transformedResponse ->28 val valid = executor.responseValidator(transformedResponse)29 if (valid) transformedResponse30 else throw FuelError.wrap(HttpException(transformedResponse.statusCode, transformedResponse.responseMessage), transformedResponse)31 }32 .recover { error -> throw FuelError.wrap(error, response) }33 .getOrThrow()34 }35 @Throws(FuelError::class)36 override fun call(): Response {37 return runCatching { prepareRequest(request) }38 .mapCatching { executeRequest(it) }39 .mapCatching { pair ->40 // Nested runCatching so response can be rebound41 runCatching { prepareResponse(pair) }42 .recover { error ->43 error.also { Fuel.trace { "[RequestTask] execution error\n\r\t$error" } }44 throw FuelError.wrap(error, pair.second)45 }46 .getOrThrow()47 }48 .onFailure { error ->49 Fuel.trace { "[RequestTask] on failure (interrupted=${(error as? FuelError)?.causedByInterruption ?: error})" }50 if (error is FuelError && error.causedByInterruption) {51 Fuel.trace { "[RequestTask] execution error\n\r\t$error" }52 interruptCallback.invoke(request)53 }54 }55 .getOrThrow()56 }57}58internal fun Request.toTask(): Callable<Response> = RequestTask(this)...

Full Screen

Full Screen

Request.suspendable

Using AI Code Generation

copy

Full Screen

1suspend fun <T : Any> Request.suspendable(): T {2return suspendCoroutine { continuation ->3this.responseString { request, response, result ->4when (result) {5is Result.Failure -> {6val ex = result.getException()7continuation.resumeWithException(ex)8}9is Result.Success -> {10val data = result.get()11val type = object : TypeToken<T>() {}.type12val result = Gson().fromJson<T>(data, type)13continuation.resume(result)14}15}16}17}18}19suspend fun <T : Any> Request.suspendable(): T {20return suspendCoroutine { continuation ->21this.responseString { request, response, result ->22when (result) {23is Result.Failure -> {24val ex = result.getException()25continuation.resumeWithException(ex)26}27is Result.Success -> {28val data = result.get()29val type = object : TypeToken<T>() {}.type30val result = Gson().fromJson<T>(data, type)31continuation.resume(result)32}33}34}35}36suspend fun <T : Any> Request.suspendable(): T {37return suspendCoroutine { continuation ->38this.responseString { request, response, result ->39when (result) {40is Result.Failure -> {41val ex = result.getException()42continuation.resumeWithException(ex)43}44is Result.Success -> {45val data = result.get()46val type = object : TypeToken<T>() {}.type47val result = Gson().fromJson<T>(data, type)48continuation.resume(result)49}50}51}52}53suspend fun <T : Any> Request.suspendable(): T {54return suspendCoroutine { continuation ->55this.responseString { request, response, result ->56when (result) {57is Result.Failure -> {58val ex = result.getException()59continuation.resumeWithException(ex)60}61is Result.Success -> {62val data = result.get()63val type = object : TypeToken<T>() {}.type64val result = Gson().fromJson<T>(data, type)65continuation.resume(result)66}67}68}69}

Full Screen

Full Screen

Request.suspendable

Using AI Code Generation

copy

Full Screen

1suspend fun <T : Any> Request.suspendable(deserializer: Deserializable<T>): T {2 return suspendCoroutine { continuation ->3 request.response(deserializer) { _, _, result ->4 when (result) {5 is Result.Success -> continuation.resume(result.value)6 is Result.Failure -> continuation.resumeWithException(result.error.exception)7 }8 }9 }10}11println("Result: $result")12suspend fun <T : Any> Request.await(deserializer: Deserializable<T>): T {13 return suspendCoroutine { continuation ->14 request.response(deserializer) { _, _, result ->15 when (result) {16 is Result.Success -> continuation.resume(result.value)17 is Result.Failure -> continuation.resumeWithException(result.error.exception)18 }19 }20 }21}22println("Result: $result")23suspend fun <T : Any> Request.await(deserializer: Deserializable<T>): T {24 return suspendCoroutine { continuation ->25 request.response(deserializer) { _, _, result ->26 when (result) {27 is Result.Success -> continuation.resume(result.value)28 is Result.Failure -> continuation.resumeWithException(result.error.exception)29 }30 }31 }32}33println("Result: $result")34suspend fun <T : Any> Request.await(deserializer: Deserializable<T>): T {35 return suspendCoroutine { continuation ->36 request.response(deserializer) { _, _, result ->37 when (result) {38 is Result.Success -> continuation.resume(result.value)39 is Result.Failure -> continuation.resumeWithException(result.error.exception)40 }41 }42 }

Full Screen

Full Screen

Request.suspendable

Using AI Code Generation

copy

Full Screen

1suspend fun get(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.GET)2suspend fun post(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.POST)3suspend fun put(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.PUT)4suspend fun delete(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.DELETE)5suspend fun head(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.HEAD)6suspend fun patch(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.PATCH)7suspend fun options(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.OPTIONS)8suspend fun trace(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.TRACE)9suspend fun download(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.DOWNLOAD)10suspend fun upload(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.UPLOAD)11suspend fun Request.suspendable(url: String, method: Method): Response {12 return suspendCoroutine { continuation ->13 request.url(url).method(method).response { request, response, result ->14 when (result) {15 is Result.Failure -> continuation.resumeWithException(result.getException())16 is Result.Success -> continuation.resume(response)17 }18 }19 }20}21suspend fun get(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.GET)22suspend fun post(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.POST)23suspend fun put(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.PUT)24suspend fun delete(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.DELETE)25suspend fun head(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.HEAD)26suspend fun patch(url: String, parameters: Parameters = listOf()): Response = Request.suspendable(url, Method.P

Full Screen

Full Screen

Request.suspendable

Using AI Code Generation

copy

Full Screen

1response(): Response<ByteArray, FuelError>2responseString(): Response<String, FuelError>3responseString(charset: Charset): Response<String, FuelError>4responseString(charset: String): Response<String, FuelError>5responseString(charset: Charset, handler: (String) -> String): Response<String, FuelError>6responseString(charset: String, handler: (String) -> String): Response<String, FuelError>7responseString(handler: (String) -> String): Response<String, FuelError>8responseObject(deserializer: ResponseDeserializable<T>): Response<T, FuelError>9responseObject(deserializer: (String) -> T): Response<T, FuelError>10responseObject(deserializer: ResponseDeserializable<T>, handler: (String) -> String): Response<T, FuelError>11responseObject(deserializer: (String) -> T, handler: (String) -> String): Response<T, FuelError>12responseObject(deserializer: ResponseDeserializable<T>, charset: Charset): Response<T, FuelError>13responseObject(deserializer: (String) -> T, charset: Charset): Response<T, FuelError>14responseObject(deserializer: ResponseDeserializable<T>, charset: String): Response<T, FuelError>15responseObject(deserializer: (String) -> T, charset: String): Response<T, FuelError>16responseObject(deserializer: ResponseDeserializable<T>, charset: Charset, handler: (String) -> String): Response<T, FuelError>17responseObject(deserializer: (String) -> T, charset: Charset, handler: (String) -> String): Response<T, FuelError>18responseObject(deserializer: ResponseDeserializable<T

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Fuel automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in SuspendableRequest

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful