Best Fuel code snippet using com.github.kittinunf.fuel.core.requests.SuspendableRequest
SuspendableRequest.kt
Source:SuspendableRequest.kt
...7import 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)...
RequestTask.kt
Source:RequestTask.kt
...6import 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 {...
SuspendableRequest
Using AI Code Generation
1SuspendableResult< String > result = request . responseString ()2val ( data , error ) = result . await ()3SuspendableResult< String > result = request . responseString ()4val ( data , error ) = result . await ()5SuspendableResult< String > result = request . responseString ()6val ( data , error ) = result . await ()7SuspendableResult< String > result = request . responseString ()8val ( data , error ) = result . await ()9SuspendableResult< String > result = request . responseString ()10val ( data , error ) = result . await ()
SuspendableRequest
Using AI Code Generation
1fun main ( args : Array < String > ) {2 request . responseString { request , response , result ->3 when (result) {4 is Result . Success -> {5 println ( response )6 println ( result . get ())7 }8 is Result . Failure -> {9 println ( response )10 println ( result . error )11 }12 }13 }14}15fun main ( args : Array < String > ) {16 val ( request , response , result ) = request . responseString ()17 when (result) {18 is Result . Success -> {19 println ( response )20 println ( result . get ())21 }22 is Result . Failure -> {23 println ( response )24 println ( result . error )25 }26 }27}
SuspendableRequest
Using AI Code Generation
1suspend fun getWithSuspendableRequest(): String {2val request = SuspendableRequest()3.responseString(request)4return result.get()5}6suspend fun postWithSuspendableRequest(): String {7val request = SuspendableRequest()8.responseString(request)9return result.get()10}11suspend fun putWithSuspendableRequest(): String {12val request = SuspendableRequest()13.responseString(request)14return result.get()15}16suspend fun deleteWithSuspendableRequest(): String {17val request = SuspendableRequest()18.responseString(request)19return result.get()20}21suspend fun patchWithSuspendableRequest(): String {22val request = SuspendableRequest()23.responseString(request)24return result.get()25}26suspend fun headWithSuspendableRequest(): String {27val request = SuspendableRequest()28.responseString(request)29return result.get()30}31suspend fun optionsWithSuspendableRequest(): String {32val request = SuspendableRequest()33.responseString(request)34return result.get()35}36suspend fun traceWithSuspendableRequest(): String {37val request = SuspendableRequest()38val (_, _, result) = Fuel.trace("https
SuspendableRequest
Using AI Code Generation
1suspend fun get(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {2 return SuspendableRequest(Method.GET, url, parameters, headers, timeout)3}4suspend fun post(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {5 return SuspendableRequest(Method.POST, url, parameters, headers, timeout)6}7suspend fun put(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {8 return SuspendableRequest(Method.PUT, url, parameters, headers, timeout)9}10suspend fun delete(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {11 return SuspendableRequest(Method.DELETE, url, parameters, headers, timeout)12}13suspend fun head(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {14 return SuspendableRequest(Method.HEAD, url, parameters, headers, timeout)15}16suspend fun patch(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {17 return SuspendableRequest(Method.PATCH, url, parameters, headers, timeout)18}19suspend fun options(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {20 return SuspendableRequest(Method.OPTIONS, url, parameters, headers, timeout)21}22suspend fun trace(url: String, parameters: List<Pair<String, String>>? = null, headers: Map<String, String>? = null, timeout: Int? = null): SuspendableRequest {
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!!