How to use establishConnection method of com.github.kittinunf.fuel.toolbox.HttpClient class

Best Fuel code snippet using com.github.kittinunf.fuel.toolbox.HttpClient.establishConnection

CustomHttpClient.kt

Source:CustomHttpClient.kt Github

copy

Full Screen

...49 init {50 allowMethods("PATCH")51 }52 override fun executeRequest(request: Request): Response {53 val connection = establishConnection(request) as? HttpURLConnection54 ?: throw IllegalStateException("Connection invalid.")55 try {56 connection.apply {57 connectTimeout = request.timeoutInMillisecond58 readTimeout = request.timeoutReadInMillisecond59 doInput = true60 useCaches = false61 requestMethod = request.method.value62 instanceFollowRedirects = false63 for ((key, value) in request.headers) {64 setRequestProperty(key, value)65 }66 setDoOutput(connection, request.method)67 setBodyIfDoOutput(connection, request)68 }69 val contentEncoding = connection.contentEncoding ?: ""70 return Response(71 url = request.url,72 headers = connection.headerFields.filterKeys { it != null },73 contentLength = connection.contentLength.toLong(),74 statusCode = connection.responseCode,75 responseMessage = connection.responseMessage.orEmpty(),76 dataStream = try {77 val stream = connection.errorStream ?: connection.inputStream78 if (contentEncoding.compareTo("gzip", true) == 0) GZIPInputStream(stream) else stream79 } catch (exception: IOException) {80 connection.errorStream ?: connection.inputStream?.close()81 ByteArrayInputStream(ByteArray(0))82 }83 )84 } catch (exception: Exception) {85 throw FuelError(exception, ByteArray(0), Response(request.url))86 } finally {87 // As per Android documentation, a connection that is not explicitly disconnected88 // will be pooled and reused! So, don't close it as we need inputStream later!89 // connection.disconnect()90 }91 }92 private fun establishConnection(request: Request): URLConnection =93 if (proxy != null) request.url.openConnection(proxy) else request.url.openConnection()94 private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {95 val bodyCallback = request.bodyCallback96 if (bodyCallback != null && connection.doOutput) {97 val contentLength = bodyCallback(request, null, 0)98 if (request.type == Request.Type.UPLOAD)99 connection.setFixedLengthStreamingMode(contentLength.toInt())100 BufferedOutputStream(connection.outputStream).use {101 bodyCallback(request, it, contentLength)102 }103 }104 }105 private fun setDoOutput(connection: HttpURLConnection, method: Method) = when (method) {106 Method.GET, Method.DELETE, Method.HEAD -> connection.doOutput = false...

Full Screen

Full Screen

HttpClient.kt

Source:HttpClient.kt Github

copy

Full Screen

...16class HttpClient(val proxy: Proxy? = null) : Client {17 override fun executeRequest(request: Request): Response {18 val response = Response()19 response.url = request.url20 val connection = establishConnection(request) as HttpURLConnection21 try {22 connection.apply {23 val timeout = Fuel.testConfiguration.timeout?.let { if (it == -1) Int.MAX_VALUE else it } ?: request.timeoutInMillisecond24 val timeoutRead = Fuel.testConfiguration.timeoutRead?.let { if (it == -1) Int.MAX_VALUE else it } ?: request.timeoutReadInMillisecond25 connectTimeout = timeout26 readTimeout = timeoutRead27 doInput = true28 useCaches = false29 requestMethod = if (request.httpMethod == Method.PATCH) Method.POST.value else request.httpMethod.value30 instanceFollowRedirects = false31 for ((key, value) in request.httpHeaders) {32 setRequestProperty(key, value)33 }34 if (request.httpMethod == Method.PATCH) setRequestProperty("X-HTTP-Method-Override", "PATCH")35 setDoOutput(connection, request.httpMethod)36 setBodyIfDoOutput(connection, request)37 }38 return response.apply {39 httpResponseHeaders = connection.headerFields ?: emptyMap()40 httpContentLength = connection.contentLength.toLong()41 val contentEncoding = connection.contentEncoding ?: ""42 dataStream = try {43 val stream = connection.errorStream ?: connection.inputStream44 if (contentEncoding.compareTo("gzip", true) == 0) GZIPInputStream(stream) else stream45 } catch (exception: IOException) {46 try {47 connection.errorStream ?: connection.inputStream ?. close()48 } catch (exception: IOException) { }49 ByteArrayInputStream(kotlin.ByteArray(0))50 }51 //try - catch just in case both methods throw52 try {53 httpStatusCode = connection.responseCode54 httpResponseMessage = connection.responseMessage.orEmpty()55 } catch(exception: IOException) {56 throw exception57 }58 }59 } catch(exception: Exception) {60 throw FuelError().apply {61 this.exception = exception62 this.errorData = response.data63 this.response = response64 }65 } finally {66 //As per Android documentation, a connection that is not explicitly disconnected67 //will be pooled and reused! So, don't close it as we need inputStream later!68 //connection.disconnect()69 }70 }71 private fun establishConnection(request: Request): URLConnection {72 val urlConnection = if (proxy != null) request.url.openConnection(proxy) else request.url.openConnection()73 return if (request.url.protocol == "https") {74 val conn = urlConnection as HttpsURLConnection75 conn.apply {76 sslSocketFactory = request.socketFactory77 hostnameVerifier = request.hostnameVerifier78 }79 } else {80 urlConnection as HttpURLConnection81 }82 }83 private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {84 val bodyCallback = request.bodyCallback85 if (bodyCallback != null && connection.doOutput) {...

Full Screen

Full Screen

establishConnection

Using AI Code Generation

copy

Full Screen

1httpClient . establishConnection ()2httpClient . establishConnection ()3httpClient . establishConnection ()4httpClient . establishConnection ()5httpClient . establishConnection ()6httpClient . establishConnection ()7httpClient . establishConnection ()8httpClient . establishConnection ()9httpClient . establishConnection ()10httpClient . establishConnection ()11httpClient . establishConnection ()

Full Screen

Full Screen

establishConnection

Using AI Code Generation

copy

Full Screen

1val client = HttpClient ( )2val response = request . responseString ( )3println ( response . third )4val client = HttpClient ( )5val response = request . responseString ( )6println ( response . third )7val client = HttpClient ( )8val response = request . responseString ( )9println ( response . third )10val client = HttpClient ( )11val response = request . responseString ( )12println ( response . third )13val client = HttpClient ( )14val response = request . responseString ( )15println ( response . third )16val client = HttpClient ( )17val response = request . responseString ( )18println ( response . third )19val client = HttpClient ( )20val response = request . responseString ( )21println ( response . third )22val client = HttpClient ( )23val response = request . responseString ( )

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful