Best Fuel code snippet using com.github.kittinunf.fuel.core.FuelManager.head
FuelManager.kt
Source:FuelManager.kt
...161 responseInterceptors.clear()162 return this163 }164 private fun applyOptions(request: Request): Request {165 // Sets base headers ONLY if they are not set166 val unsetBaseHeaders = request.headers.keys.fold(Headers.from(baseHeaders.orEmpty())) {167 result, it -> result.remove(it); result168 }169 return request.header(unsetBaseHeaders).apply {170 executionOptions = RequestExecutionOptions(171 client = client,172 socketFactory = socketFactory,173 hostnameVerifier = hostnameVerifier,174 callbackExecutor = callbackExecutor,175 requestTransformer = requestInterceptors.foldRight({ r: Request -> r }) { f, acc -> f(acc) },176 responseTransformer = responseInterceptors.foldRight({ _: Request, res: Response -> res }) { f, acc -> f(acc) },177 executorService = executorService178 ).also { executor ->179 executor.timeoutInMillisecond = timeoutInMillisecond180 executor.timeoutReadInMillisecond = timeoutReadInMillisecond181 executor.forceMethods = forceMethods182 }183 }184 }185 companion object {186 // manager187 var instance by readWriteLazy { FuelManager() }188 val progressBufferSize: Int get() = instance.progressBufferSize189 }190 /**191 * Create a [Method.GET] [Request] to [path] with [parameters]192 *193 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path194 * @param parameters [Parameters] the optional parameters195 * @return [Request] the request196 */197 override fun get(path: String, parameters: Parameters?): Request =198 request(Method.GET, path, parameters)199 /**200 * Create a [Method.GET] [Request] to [PathStringConvertible.path] with [parameters]201 *202 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path203 * @param parameters [Parameters] the optional parameters204 * @return [Request] the request205 */206 override fun get(convertible: PathStringConvertible, parameters: Parameters?): Request =207 request(Method.GET, convertible, parameters)208 /**209 * Create a [Method.POST] [Request] to [path] with [parameters]210 *211 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path212 * @param parameters [Parameters] the optional parameters213 * @return [Request] the request214 */215 override fun post(path: String, parameters: Parameters?): Request =216 request(Method.POST, path, parameters)217 /**218 * Create a [Method.POST] [Request] to [PathStringConvertible.path] with [parameters]219 *220 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path221 * @param parameters [Parameters] the optional parameters222 * @return [Request] the request223 */224 override fun post(convertible: PathStringConvertible, parameters: Parameters?): Request =225 request(Method.POST, convertible, parameters)226 /**227 * Create a [Method.PUT] [Request] to [path] with [parameters]228 *229 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path230 * @param parameters [Parameters] the optional parameters231 * @return [Request] the request232 */233 override fun put(path: String, parameters: Parameters?): Request =234 request(Method.PUT, path, parameters)235 /**236 * Create a [Method.PUT] [Request] to [PathStringConvertible.path] with [parameters]237 *238 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path239 * @param parameters [Parameters] the optional parameters240 * @return [Request] the request241 */242 override fun put(convertible: PathStringConvertible, parameters: Parameters?): Request =243 request(Method.PUT, convertible, parameters)244 /**245 * Create a [Method.PATCH] [Request] to [path] with [parameters]246 *247 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path248 * @param parameters [Parameters] the optional parameters249 * @return [Request] the request250 */251 override fun patch(path: String, parameters: Parameters?): Request =252 request(Method.PATCH, path, parameters)253 /**254 * Create a [Method.PATCH] [Request] to [PathStringConvertible.path] with [parameters]255 *256 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path257 * @param parameters [Parameters] the optional parameters258 * @return [Request] the request259 */260 override fun patch(convertible: PathStringConvertible, parameters: Parameters?): Request =261 request(Method.PATCH, convertible, parameters)262 /**263 * Create a [Method.DELETE] [Request] to [path] with [parameters]264 *265 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path266 * @param parameters [Parameters] the optional parameters267 * @return [Request] the request268 */269 override fun delete(path: String, parameters: Parameters?): Request =270 request(Method.DELETE, path, parameters)271 /**272 * Create a [Method.DELETE] [Request] to [PathStringConvertible.path] with [parameters]273 *274 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path275 * @param parameters [Parameters] the optional parameters276 * @return [Request] the request277 */278 override fun delete(convertible: PathStringConvertible, parameters: Parameters?): Request =279 request(Method.DELETE, convertible, parameters)280 /**281 * Create a [method] [Request] to [PathStringConvertible.path] with [parameters], which can download to a file282 *283 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path284 * @param method [Method] the method to download with, defaults to [Method.GET]285 * @param parameters [Parameters] the optional parameters286 * @return [DownloadRequest] the request (extended for download)287 */288 override fun download(convertible: PathStringConvertible, method: Method, parameters: Parameters?): DownloadRequest =289 download(convertible.path, method, parameters)290 /**291 * Create a [method] [Request] to [PathStringConvertible.path] with [parameters], which can upload blobs and292 * Data Parts293 *294 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path295 * @param method [Method] the method to upload with, defaults to [Method.POST]296 * @param parameters [Parameters] the optional parameters297 * @return [UploadRequest] the request (extended for upload)298 */299 override fun upload(convertible: PathStringConvertible, method: Method, parameters: Parameters?): UploadRequest =300 upload(convertible.path, method, parameters)301 /**302 * Create a [Method.HEAD] [Request] to [path] with [parameters]303 *304 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path305 * @param parameters [Parameters] the optional parameters306 * @return [Request] the request307 */308 override fun head(path: String, parameters: Parameters?): Request =309 request(Method.HEAD, path, parameters)310 /**311 * Create a [Method.HEAD] [Request] to [PathStringConvertible.path] with [parameters]312 *313 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path314 * @param parameters [Parameters] the optional parameters315 * @return [Request] the request316 */317 override fun head(convertible: PathStringConvertible, parameters: Parameters?): Request =318 request(Method.HEAD, convertible, parameters)319 /**320 * Resets this FuelManager to a clean instance321 */322 fun reset(): FuelManager {323 val clean = FuelManager()324 client = clean.client325 proxy = clean.proxy326 basePath = clean.basePath327 timeoutInMillisecond = clean.timeoutInMillisecond328 timeoutReadInMillisecond = clean.timeoutReadInMillisecond329 baseHeaders = clean.baseHeaders330 baseParams = clean.baseParams331 keystore = clean.keystore...
RequestFactory.kt
Source:RequestFactory.kt
...172 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path173 * @param parameters [Parameters] the optional parameters174 * @return [Request] the request175 */176 fun head(path: String, parameters: Parameters? = null): Request177 /**178 * Create a [Method.HEAD] [Request] to [PathStringConvertible.path] with [parameters]179 *180 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path181 * @param parameters [Parameters] the optional parameters182 * @return [Request] the request183 */184 fun head(convertible: PathStringConvertible, parameters: Parameters? = null): Request185 }186}...
FuelHttpClient.kt
Source:FuelHttpClient.kt
...34 response.url.toExternalForm(),35 result.value,36 response.statusCode,37 response.responseMessage,38 response.headers,39 response.headers["Set-Cookie"].flatMap { HttpCookie.parse(it) },40 response.contentLength,41 time42 )43 }44 }45 }46 override fun dispatch(request: Request, handler: (result: Result<Response>) -> Unit) {47 val start = System.currentTimeMillis()48 val (id, req) = prepare(request)49 req.responseString(request.charset ?: charset) { _, response, result ->50 when (result) {51 is com.github.kittinunf.result.Result.Failure -> handler(52 Result.failure(53 result.getException()54 )55 )56 is com.github.kittinunf.result.Result.Success -> {57 val time = System.currentTimeMillis() - start58 logger.debug("[Response][$id] status code: ${response.statusCode} content length: ${response.contentLength} time: $time ms")59 val res = Response(60 response.url.toExternalForm(),61 result.value,62 response.statusCode,63 response.responseMessage,64 response.headers,65 response.headers["Set-Cookie"].flatMap { HttpCookie.parse(it) },66 response.contentLength,67 time68 )69 handler(Result.success(res))70 }71 }72 }73 }74 private fun prepare(request: Request): Pair<String, com.github.kittinunf.fuel.core.Request> {75 val id = generateId(6)76 logger.debug("[Request][$id] ${request.method} ${request.url}")77 if (proxyProvider.hasAny()) {78 val httpProxy = proxyProvider.select()79 FuelManager.instance.proxy = httpProxy.toProxy() /// TODO: not work80 logger.debug("[Proxy][$id] use $httpProxy")81 } else {82 FuelManager.instance.proxy = null83 }84 val req = when (request.method) {85 HttpMethod.GET -> request.url.httpGet()86 HttpMethod.POST -> request.url.httpPost()87 HttpMethod.PUT -> request.url.httpPut()88 HttpMethod.PATCH -> request.url.httpPatch()89 HttpMethod.DELETE -> request.url.httpDelete()90 HttpMethod.HEAD -> request.url.httpHead()91 }92 req.allowRedirects(request.allowRedirects)93 if (request.cookies.isNotEmpty()) {94 req.header("Cookie", request.cookies.joinToString("&"))95 }96 if (userAgentProvider.hasAny()) {97 req.header("User-Agent", userAgentProvider.select())98 }99 req.header(request.headers.toMap())100 req.timeout(timeout)101 req.timeoutRead(timeoutRead)102 request.body?.let {103 when (it) {104 is TextRequestBody -> req.body(it.text)105 is JsonRequestBody -> req.jsonBody(it.json)106 is BytesRequestBody -> req.body(it.bytes)107 is FileRequestBody -> req.body(it.file)108 is FormRequestBody -> {109 req.header("Content-Type", "application/x-www-form-urlencoded")110 req.parameters = it.form.toList()111 }112 }113 }114 return Pair(id, req)115 }116}...
GithubApi.kt
Source:GithubApi.kt
...8sealed class GithubApi(repo: Repo) : FuelRouting {9 data class Repo(val owner: String, val name: String)10 private var authToken: String = ""11 override val basePath: String = "https://api.github.com/repos/${repo.owner}/${repo.name}"12 override val headers: Map<String, HeaderValues>?13 get() = mapOf("Authorization" to listOf("token $authToken"))14 override val params: Parameters? = null15 override val bytes: ByteArray? = null16 fun withToken(token: String): GithubApi {17 authToken = token18 return this19 }20 abstract class Issue(repo: Repo) : GithubApi(repo) {21 override val path: String = "/issues"22 class Update(repo: Repo, number: Int, user: String) : Issue(repo) {23 override val path: String = "/issues/$number"24 override val body: String? = """25 {26 "labels" : [],27 "assignees" : [ "$user" ]28 }29 """.trimIndent()30 override val method: Method = PATCH31 override fun call(): Request = FuelManager.instance.request(this).appendHeader(Headers.CONTENT_TYPE, "application/json")32 }33 }34 abstract class PR(repo: Repo) : GithubApi(repo) {35 enum class State(val value: String) {36 OPEN("open"),37 CLOSED("closed")38 }39 override val path: String = "/pulls"40 class Create(repo: Repo, title: String, body: String, branch: String, base: String = "master") : PR(repo) {41 override val body: String? = """42 {43 "title" : "$title",44 "head" : "$branch",45 "base" : "$base",46 "body" : "$body"47 }48 """.trimIndent()49 override val method: Method = POST50 }51 class Read(repo: Repo, head: String) : PR(repo) {52 override val body: String? = null53 override val method: Method = GET54 override val params: Parameters? = listOf("head" to "kouzoh:$head")55 }56 class Update(57 repo: Repo,58 number: Int,59 title: String,60 body: String,61 state: State = State.OPEN,62 base: String = "master"63 ) : PR(repo) {64 override val body: String? = """65 {66 "title" : "$title",67 "state" : "${state.value}",68 "base" : "$base",...
FuelWebService.kt
Source:FuelWebService.kt
...35 inline fun <reified T: Any> get(url: String,36 crossinline success: (Request, Response, T) -> Unit,37 noinline failure: ((Request, Response, Exception) -> Unit)? = null38 ): Request = request<T>(Fuel.get(url), success, failure)39 inline fun <reified T: Any> head(url: String,40 crossinline success: (Request, Response, T) -> Unit,41 noinline failure: ((Request, Response, Exception) -> Unit)? = null42 ): Request = request<T>(Fuel.head(url), success, failure)43 inline fun <reified T: Any> post(url: String,44 crossinline success: (Request, Response, T) -> Unit,45 noinline failure: ((Request, Response, Exception) -> Unit)? = null46 ): Request = request<T>(Fuel.post(url), success, failure)47 inline fun <reified T: Any> put(url: String,48 crossinline success: (Request, Response, T) -> Unit,49 noinline failure: ((Request, Response, Exception) -> Unit)? = null50 ): Request = request<T>(Fuel.put(url), success, failure)51 inline fun <reified T: Any> delete(url: String,52 crossinline success: (Request, Response, T) -> Unit,53 noinline failure: ((Request, Response, Exception) -> Unit)? = null54 ): Request = request<T>(Fuel.delete(url), success, failure)55 @Deprecated("@hide")56 inline fun <reified T: Any> request(request: Request,...
Fuel.kt
Source:Fuel.kt
...42 Fuel.upload(this, method, parameters)43fun RequestFactory.PathStringConvertible.httpUpload(parameters: Parameters? = null, method: Method = Method.POST): UploadRequest =44 this.path.httpUpload(parameters, method)45fun String.httpHead(parameters: Parameters? = null): Request =46 Fuel.head(this, parameters)47fun RequestFactory.PathStringConvertible.httpHead(parameters: Parameters? = null): Request =48 this.path.httpHead(parameters)...
NetUtils.kt
Source:NetUtils.kt
...9 fun sendGet(10 url: String,11 withTor: Boolean = false,12 method: String? = null,13 header: Map<String, Any>? = null,14 args: List<Pair<String, String>>? = null15 ): String? {16 if (withTor){17 val torAddress = InetSocketAddress("127.0.0.1", 9050)18 FuelManager.instance.proxy = Proxy(Proxy.Type.SOCKS, torAddress)19 FuelManager.instance.timeoutInMillisecond = 30_00020 }21 var head = mapOf<String, Any>()22 if (header != null) {23 head = header24 }25 val sb = StringBuilder(url)26 if (method != null) sb.append(method)27 val (_, _, result) = sb.toString()28 .httpGet(args)29 .header(head)30 .responseString()31 return when (result) {32 is Result.Failure -> {33 val ex = result.getException()34 println(result.error)35 ex.printStackTrace()36 null37 }38 is Result.Success -> {39 result.get()40 }41 }42 }43 fun sendPost(url: String, method: String, header: Map<String, Any>? = null, requestJson: String): String? {44 var head = mapOf<String, Any>()45 if (header != null) {46 head = header47 }48 val (_, _, result) = "$url$method"49 .httpPost()50 .body(requestJson)51 .header(head)52 .responseString()53 return when (result) {54 is Result.Failure -> {55 val ex = result.getException()56 ex.printStackTrace()57 null58 }59 is Result.Success -> {60 result.get()61 }62 }63 }64}...
Client.kt
Source:Client.kt
...30 else -> throw Exception(method.name + " is not supported by JVM")31 }32 }33 if (request.body != null) req.body(request.body.toString())34 req.header(request.headers)35 req.response { r, response, result ->36 if (result.component2() != null) {37 println(r.url.toString())38 c.resumeWithException(result.component2()!!)39 return@response40 }41 c.resume(response.toResponse())42 }43 }44 fun com.github.kittinunf.fuel.core.Response.toResponse(): Response {45 return Response(46 status = this.statusCode,47 responseText = String(data),48 headers = this.headers.map { entry -> entry.key to entry.value.first() }.toMap()49 )50 }51}...
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!!