Best Fuel code snippet using com.github.kittinunf.fuel.core.requests.internal.writeParameter
UploadBody.kt
Source:UploadBody.kt
...95 val lazyDataparts = request.dataParts96 return outputStream.buffered().let { stream ->97 // Parameters98 val parameterLength = request.parameters.sumByDouble { (name, data) ->99 writeParameter(stream, name, data).toDouble()100 }101 // Blobs / Files102 val filesWithHeadersLength = lazyDataparts.sumByDouble { lazyDataPart ->103 writeDataPart(stream, lazyDataPart(request)).toDouble()104 }105 // Sum and Trailer106 val writtenLength = 0L +107 parameterLength +108 filesWithHeadersLength +109 stream.writeBoundary() + stream.writeString("--") +110 stream.writeNewline()111 // This is a buffered stream, so flush what's remaining112 writtenLength.toLong().also { stream.flush() }113 }114 }115 /**116 * Returns the length of the body in bytes117 * @return [Long?] the length in bytes, null if it is unknown118 */119 override val length: Long? by lazy {120 (121 // Parameters size122 request.parameters.sumByDouble { (name, data) ->123 writeParameter(ByteArrayOutputStream(), name, data).toDouble()124 } +125 // Blobs / Files size126 request.dataParts.sumByDouble { lazyDataPart ->127 val dataPart = lazyDataPart(request)128 // Allow for unknown sizes129 val length = dataPart.contentLength ?: return@lazy null130 if (length == -1L) return@lazy -1L131 0.0 + writeDataPartHeader(ByteArrayOutputStream(), dataPart) + length + CRLF.size132 } +133 // Trailer size134 "--$boundary--".toByteArray(DEFAULT_CHARSET).size + CRLF.size135 ).toLong()136 }137 private val boundary: String by lazy {138 request[Headers.CONTENT_TYPE].lastOrNull()139 ?.let { Regex("boundary=([^\\s]+)").find(it)?.groupValues?.getOrNull(1)?.trim('"') }140 ?: throw BoundaryMissing(request)141 }142 private fun writeParameter(outputStream: OutputStream, name: String, data: Any?): Long {143 outputStream.apply {144 return 0L +145 writeBoundary() +146 writeNewline() +147 writeString("${Headers.CONTENT_DISPOSITION}: form-data; name=\"$name\"") +148 writeNewline() +149 writeString("${Headers.CONTENT_TYPE}: text/plain; charset=\"${DEFAULT_CHARSET.name()}\"") +150 writeNewline() +151 writeNewline() +152 writeString(data.toString()) +153 writeNewline()154 }155 }156 private fun writeDataPart(outputStream: OutputStream, dataPart: DataPart): Long {...
writeParameter
Using AI Code Generation
1fun writeParameter(appendable: Appendable, key: String, value: Any?, charset: Charset) {2 appendable.append(URLEncoder.encode(key, charset.name()))3 if (value != null) {4 appendable.append("=")5 appendable.append(URLEncoder.encode(value.toString(), charset.name()))6 }7}8fun writeParameters(appendable: Appendable, parameters: List<Pair<String, Any?>>, charset: Charset) {9 parameters.forEachIndexed { index, pair ->10 if (index != 0) {11 appendable.append("&")12 }13 writeParameter(appendable, pair.first, pair.second, charset)14 }15}16fun writeParameters(appendable: Appendable, parameters: Map<String, Any?>, charset: Charset) {17 writeParameters(appendable, parameters.toList(), charset)18}19fun writeParameters(appendable: Appendable, parameters: Parameters, charset: Charset) {20 writeParameters(appendable, parameters.toList(), charset)21}22fun writeParameters(appendable: Appendable, parameters: Parameters, charset: Charset, encode: Boolean) {23 if (encode) {24 writeParameters(appendable, parameters.toList(), charset)25 } else {26 parameters.forEachIndexed { index, pair ->27 if (index != 0) {28 appendable.append("&")29 }30 appendable.append(pair.first)31 if (pair.second != null) {32 appendable.append("=")33 appendable.append(pair.second.toString())34 }35 }36 }37}38fun writeParameters(appendable: Appendable, parameters: List<Pair<String, Any?>>, charset: Charset, encode: Boolean) {39 if (encode) {40 writeParameters(appendable, parameters, charset)41 } else {42 parameters.forEachIndexed { index, pair ->43 if (index != 0) {44 appendable.append("&")
writeParameter
Using AI Code Generation
1fun Request.writeParameter(name: String, value: Any) {2 this.writeParameter(name, value)3}4fun Request.writeParameter(name: String, value: String) {5 this.writeParameter(name, value)6}7fun Request.writeParameter(name: String, value: Int) {8 this.writeParameter(name, value)9}10fun Request.writeParameter(name: String, value: Long) {11 this.writeParameter(name, value)12}13fun Request.writeParameter(name: String, value: Float) {14 this.writeParameter(name, value)15}16fun Request.writeParameter(name: String, value: Double) {17 this.writeParameter(name, value)18}19fun Request.writeParameter(name: String, value: Boolean) {20 this.writeParameter(name, value)21}22fun Request.writeParameter(name: String, value: ByteArray) {23 this.writeParameter(name, value)24}25fun Request.writeParameter(name: String, value: File) {26 this.writeParameter(name, value
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!!