Best Fuel code snippet using com.github.kittinunf.fuel.core.RequestFactory.download
Request.kt
Source:Request.kt
1package com.github.kittinunf.fuel.core2import com.github.kittinunf.fuel.core.requests.CancellableRequest3import java.io.File4import java.io.InputStream5import java.net.URL6import java.nio.charset.Charset7import kotlin.reflect.KClass8typealias Parameters = List<Pair<String, Any?>>9typealias RequestFeatures = MutableMap<String, Request>10typealias Tags = MutableMap<KClass<*>, Any>11interface Request : RequestFactory.RequestConvertible {12 val method: Method13 var url: URL14 val headers: Headers15 var parameters: Parameters16 var executionOptions: RequestExecutionOptions17 val body: Body18 val enabledFeatures: RequestFeatures19 /**20 * Add a [ProgressCallback] tracking the [Body] of the [Request]21 *22 * @see body23 * @see com.github.kittinunf.fuel.core.requests.UploadRequest.progress24 *25 * @return self26 */27 fun requestProgress(handler: ProgressCallback): Request28 /**29 * Add a [ProgressCallback] tracking the [Body] of the [Response]30 *31 * @see com.github.kittinunf.fuel.core.requests.DownloadRequest.progress32 *33 * @return self34 */35 fun responseProgress(handler: ProgressCallback): Request36 /**37 * Overwrite the [Request] [timeout] in milliseconds38 *39 * @note [Client] must implement this behaviour40 * @note the default client sets [java.net.HttpURLConnection.setConnectTimeout]41 *42 * @param timeout [Int] timeout in milliseconds43 * @return self44 */45 fun timeout(timeout: Int): Request46 /**47 * Overwrite the [Request] [timeout] in milliseconds48 *49 * @note [Client] must implement this behaviour50 * @note the default client sets [java.net.HttpURLConnection.setReadTimeout]51 *52 * @param timeout [Int] timeout in milliseconds53 * @return self54 */55 fun timeoutRead(timeout: Int): Request56 /**57 * Overwrite [RequestExecutionOptions] http cache usage flag58 *59 * @note [Client] must implement this behaviour60 * @note The default client sends `Cache-Control: none` if this flag is false, defaults to true61 *62 * @see java.net.HttpURLConnection.setUseCaches63 * @param useHttpCache [Boolean] true if suggest client to allow cached responses, false otherwise64 */65 fun useHttpCache(useHttpCache: Boolean): Request66 /**67 * Follow redirects as handled by instances of RedirectInterceptors68 * i.e. [com.github.kittinunf.fuel.core.interceptors.redirectResponseInterceptor]69 *70 * @param allowRedirects [Boolean] true if allowing, false if not71 * @return self72 */73 fun allowRedirects(allowRedirects: Boolean): Request74 /**75 * Returns a string representation of the request.76 *77 * @see com.github.kittinunf.fuel.core.extensions.httpString78 * @see com.github.kittinunf.fuel.core.extensions.cUrlString79 *80 * @return [String] the string representation81 */82 override fun toString(): String83 /**84 * Get the current values of the header, after normalisation of the header85 * @param header [String] the header name86 * @return the current values (or empty if none)87 */88 operator fun get(header: String): HeaderValues89 /**90 * Set the values of the header, overriding what's there, after normalisation of the header91 *92 * @param header [String] the header name93 * @param values [Collection<*>] the values to be transformed through #toString94 * @return self95 */96 operator fun set(header: String, values: Collection<*>): Request97 /**98 * Set the value of the header, overriding what's there, after normalisation of the header99 *100 * @param header [String] the header name101 * @param value [Any] the value to be transformed through #toString102 */103 operator fun set(header: String, value: Any): Request104 /**105 * Get the current values106 *107 * @see get(header: String)108 * @return [HeaderValues] the current values109 */110 fun header(header: String): HeaderValues111 /**112 * Replace the headers with the map provided113 *114 * @note In earlier versions the mapOf variant of this function worked differently than the vararg pairs variant,115 * which has been changed to make any call to header(...) always overwrite the values and any call to116 * appendHeader(...) will try to append the value.117 *118 * @see set(header: String, values: Collection<*>)119 * @see set(header: String, value: Any)120 *121 * @param map [Map<String, Any>] map of headers to replace. Value can be a list or single value122 * @return [Request] the modified request123 */124 fun header(map: Map<String, Any>): Request125 /**126 * Replace the headers with the pairs provided127 *128 * @note In earlier versions the mapOf variant of this function worked differently than the vararg pairs variant,129 * which has been changed to make any call to header(...) always overwrite the values and any call to130 * appendHeader(...) will try to append the value.131 *132 * @see set(header: String, values: Collection<*>)133 * @see set(header: String, value: Any)134 *135 * @param pairs [Pair<String, Any>] map of headers to replace. Value can be a list or single value136 * @return [Request] the modified request137 */138 fun header(vararg pairs: Pair<String, Any>): Request139 /**140 * Replace the header with the provided values141 *142 * @see set(header: String, values: Collection<*>)143 *144 * @param header [String] the header to set145 * @param values [List<Any>] the values to set the header to146 * @return [Request] the modified request147 */148 fun header(header: String, values: Collection<*>): Request149 /**150 * Replace the header with the provided value151 *152 * @see set(header: String, values: List<Any>)153 *154 * @param header [String] the header to set155 * @param value [Any] the value to set the header to156 * @return [Request] the modified request157 */158 fun header(header: String, value: Any): Request159 /**160 * Replace the header with the provided values161 *162 * @see set(header: String, values: List<Any>)163 *164 * @param header [String] the header to set165 * @param values [Any] the values to set the header to166 * @return [Request] the modified request167 */168 fun header(header: String, vararg values: Any): Request169 /**170 * Appends the value to the header or sets it if there was none yet171 *172 * @param header [String] the header name to append to173 * @param value [Any] the value to be transformed through #toString174 */175 fun appendHeader(header: String, value: Any): Request176 /**177 * Appends the value to the header or sets it if there was none yet178 *179 * @param header [String] the header name to append to180 * @param values [Any] the value to be transformed through #toString181 */182 fun appendHeader(header: String, vararg values: Any): Request183 /**184 * Append each pair, using the key as header name and value as header content185 *186 * @param pairs [Pair<String, Any>]187 */188 fun appendHeader(vararg pairs: Pair<String, Any>): Request189 /**190 * Execute the [Request] asynchronously, using the [handler], into a [ByteArray]191 *192 * @param handler [ResponseResultHandler] the handler to report the [Request], [Response] and Result of [ByteArray]193 * @return [CancellableRequest] the request in flight194 */195 fun response(handler: ResponseResultHandler<ByteArray>): CancellableRequest196 /**197 * Execute the [Request] asynchronously, using the [handler], into a [ByteArray]198 *199 * @param handler [ResultHandler] the handler to report the Result of [ByteArray]200 * @return [CancellableRequest] the request in flight201 */202 fun response(handler: ResultHandler<ByteArray>): CancellableRequest203 /**204 * Execute the [Request] asynchronously, using the [handler], into a [ByteArray]205 *206 * @param handler [ResponseHandler] the handler to report the [Request], [Response], and [ByteArray] or [FuelError]207 * @return [CancellableRequest] the request in flight208 */209 fun response(handler: ResponseHandler<ByteArray>): CancellableRequest210 /**211 * Execute the [Request] asynchronously, using the [handler], into a [ByteArray]212 *213 * @param handler [Handler] the handler to report the [ByteArray] or [FuelError]214 * @return [CancellableRequest] the request in flight215 */216 fun response(handler: Handler<ByteArray>): CancellableRequest217 /**218 * Execute the [Request] asynchronously, using the [handler], into a [Charsets.UTF_8] [String]219 *220 * @param handler [ResponseResultHandler] the handler to report the [Request], [Response] and Result of [String]221 * @return [CancellableRequest] the request in flight222 */223 fun responseString(handler: ResponseResultHandler<String>): CancellableRequest224 /**225 * Execute the [Request] asynchronously, using the [handler], into a [String]226 *227 * @param charset [Charset] the charset to use for the [String]228 * @param handler [ResponseResultHandler] the handler to report the [Request], [Response] and Result of [String]229 * @return [CancellableRequest] the request in flight230 */231 fun responseString(charset: Charset = Charsets.UTF_8, handler: ResponseResultHandler<String>): CancellableRequest232 /**233 * Execute the [Request] asynchronously, using the [handler], into a [Charsets.UTF_8] [String]234 *235 * @param handler [ResultHandler] the handler to report the Result of [String]236 * @return [CancellableRequest] the request in flight237 */238 fun responseString(handler: ResultHandler<String>): CancellableRequest239 /**240 * Execute the [Request] asynchronously, using the [handler], into a [String]241 *242 * @param charset [Charset] the charset to use for the [String]243 * @param handler [ResultHandler] the handler to report the Result of [String]244 * @return [CancellableRequest] the request in flight245 */246 fun responseString(charset: Charset = Charsets.UTF_8, handler: ResultHandler<String>): CancellableRequest247 /**248 * Execute the [Request] asynchronously, using the [handler], into a [Charsets.UTF_8] [String]249 *250 * @param handler [ResponseHandler] the handler to report the [Request], [Response], and [String] or [FuelError]251 * @return [CancellableRequest] the request in flight252 */253 fun responseString(handler: ResponseHandler<String>): CancellableRequest254 /**255 * Execute the [Request] asynchronously, using the [handler], into a [String]256 *257 * @param charset [Charset] the charset to use for the [String]258 * @param handler [ResponseHandler] the handler to report the [Request], [Response], and [String] or [FuelError]259 * @return [CancellableRequest] the request in flight260 */261 fun responseString(charset: Charset = Charsets.UTF_8, handler: ResponseHandler<String>): CancellableRequest262 /**263 * Execute the [Request] asynchronously, using the [handler], into a [Charsets.UTF_8] [String]264 *265 * @param handler [Handler] the handler to report the [String] or [FuelError]266 * @return [CancellableRequest] the request in flight267 */268 fun responseString(handler: Handler<String>): CancellableRequest269 /**270 * Execute the [Request] asynchronously, using the [handler], into a [String]271 * @param charset [Charset] the charset to use for the [String]272 * @param handler [Handler] the handler to report the [String] or [FuelError]273 * @return [CancellableRequest] the request in flight274 */275 fun responseString(charset: Charset = Charsets.UTF_8, handler: Handler<String>): CancellableRequest276 /**277 * Execute the [Request] asynchronously, using the [handler], into a [T]278 *279 * @param deserializer [ResponseDeserializable] instance that can turn [Response] into [T]280 * @param handler [ResponseResultHandler] the handler to report the [Request], [Response] and Result of [T]281 * @return [CancellableRequest] the request in flight282 */283 fun <T : Any> responseObject(deserializer: ResponseDeserializable<T>, handler: ResponseResultHandler<T>): CancellableRequest284 /**285 * Execute the [Request] asynchronously, using the [handler], into a [T]286 *287 * @param deserializer [ResponseDeserializable] instance that can turn [Response] into [T]288 * @param handler [ResponseHandler] the handler to report the [Request], [Response], and [T] or [FuelError]289 * @return [CancellableRequest] the request in flight290 */291 fun <T : Any> responseObject(deserializer: ResponseDeserializable<T>, handler: ResponseHandler<T>): CancellableRequest292 /**293 * Execute the [Request] asynchronously, using the [handler], into a [T]294 *295 * @param deserializer [ResponseDeserializable] instance that can turn [Response] into [T]296 * @param handler [ResultHandler] the handler to report the Result of [T]297 * @return [CancellableRequest] the request in flight298 */299 fun <T : Any> responseObject(deserializer: ResponseDeserializable<T>, handler: ResultHandler<T>): CancellableRequest300 /**301 * Execute the [Request] asynchronously, using the [handler], into a [T]302 *303 * @param deserializer [ResponseDeserializable] instance that can turn [Response] into [T]304 * @param handler [Handler] the handler to report the [T] or [FuelError]305 * @return [CancellableRequest] the request in flight306 */307 fun <T : Any> responseObject(deserializer: ResponseDeserializable<T>, handler: Handler<T>): CancellableRequest308 /**309 * Execute the [Request] synchronously, into a [ByteArray]310 *311 * @note this is a synchronous execution and can not be cancelled312 *313 * @return [ResponseResultOf] the response result of [ByteArray]314 */315 fun response(): ResponseResultOf<ByteArray>316 /**317 * Execute the [Request] synchronously, into a [String]318 *319 * @note this is a synchronous execution and can not be cancelled320 *321 * @param charset [Charset] the character set to use for the string322 * @return [ResponseResultOf] the response result of [String]323 */324 fun responseString(charset: Charset = Charsets.UTF_8): ResponseResultOf<String>325 /**326 * Execute the [Request] synchronously, into a [Charsets.UTF_8] [String]327 *328 * @note this is a synchronous execution and can not be cancelled329 *330 * @return [ResponseResultOf] the response result of [String]331 */332 fun responseString(): ResponseResultOf<String>333 /**334 * Execute the [Request] synchronously, into a [T]335 *336 * @note this is a synchronous execution and can not be cancelled337 *338 * @param deserializer [ResponseDeserializable] instance that can turn the [Response] into a [T]339 * @return [ResponseResultOf] the response result of [T]340 */341 fun <T : Any> responseObject(deserializer: ResponseDeserializable<T>): ResponseResultOf<T>342 /**343 * Sets the body to be read from a generic body source.344 *345 * @note in earlier versions the body callback would be called multiple times in order to maybe get the size. But346 * that would lead to closed streams being unable to be read. If the size is known, set it before anything else.347 *348 * @param openStream [BodySource] a function that yields a stream349 * @param calculateLength [Number?] size in +bytes+ if it is known350 * @param charset [Charset] the charset to write with351 * @param repeatable [Boolean] loads the body into memory upon reading352 *353 * @return [Request] the request354 */355 fun body(openStream: BodySource, calculateLength: BodyLength? = null, charset: Charset = Charsets.UTF_8, repeatable: Boolean = false): Request356 /**357 * Sets the body from a generic stream358 *359 * @note the stream will be read from the position it's at. Make sure you rewind it if you want it to be read from360 * the start.361 *362 * @param stream [InputStream] a stream to read from363 * @param calculateLength [Number?] size in bytes if it is known364 * @param charset [Charset] the charset to write with365 * @param repeatable [Boolean] loads the body into memory upon reading366 *367 * @return [Request] the request368 */369 fun body(stream: InputStream, calculateLength: BodyLength? = null, charset: Charset = Charsets.UTF_8, repeatable: Boolean = false): Request370 /**371 * Sets the body from a byte array372 *373 * @param bytes [ByteArray] the bytes to write374 * @param charset [Charset] the charset to write with375 * @return [Request] the request376 */377 fun body(bytes: ByteArray, charset: Charset = Charsets.UTF_8): Request378 /**379 * Sets the body from a string380 *381 * @param body [String] the string to write382 * @param charset [Charset] the charset to write with383 * @return [Request] the request384 */385 fun body(body: String, charset: Charset = Charsets.UTF_8): Request386 /**387 * Sets the body to the contents of a file.388 *389 * @note this does *NOT* make this a multipart upload. For that you can use the upload request. This function can be390 * used if you want to upload the single contents of a text based file as an inline body.391 *392 * @note when charset is not UTF-8, this forces the client to use chunked encoding, because file.length() gives the393 * length of the file in bytes without considering the charset. If the charset is to be considered, the file needs394 * to be read in its entirety which defeats the purpose of using a file.395 *396 * @param file [File] the file to write to the body397 * @param charset [Charset] the charset to write with398 * @return [Request] the request399 */400 fun body(file: File, charset: Charset = Charsets.UTF_8): Request401 /**402 * Sets the body to a defined [Body]403 *404 * @param body [Body] the body to assign405 * @return [Request] the request406 */407 fun body(body: Body): Request408 /**409 * Add a [InterruptCallback] to the [RequestExecutionOptions]410 *411 * @see RequestExecutionOptions.interruptCallbacks412 *413 * @return self414 */415 fun interrupt(interrupt: InterruptCallback): Request416 /**417 * Override a default [ResponseValidator] to the [RequestExecutionOptions]418 *419 * @see RequestExecutionOptions.responseValidator420 *421 * @return self422 */423 fun validate(validator: ResponseValidator): Request424 /**425 * Attach tag to the request426 *427 * @note tag is a generic purpose tagging for Request. This can be used to attach arbitrarily object to the Request instance.428 * @note Tags internally is represented as hashMap that uses class as a key.429 *430 * @param t [Any]431 * @return [Request] the modified request432 */433 fun tag(t: Any): Request434 /**435 * Return corresponding tag from the request436 *437 * @note tag is a generic purpose tagging for Request. This can be used to attach arbitrarily object to the Request instance.438 * @note Tags internally is represented as hashMap that uses class as a key.439 *440 * @param clazz [KClass]441 * @return [Any] previously attached tag if any, null otherwise442 */443 fun <T : Any> getTag(clazz: KClass<T>): T?444}...
FuelManager.kt
Source:FuelManager.kt
...5import com.github.kittinunf.fuel.core.interceptors.ParameterEncoder6import com.github.kittinunf.fuel.core.interceptors.redirectResponseInterceptor7import com.github.kittinunf.fuel.core.requests.DownloadRequest8import com.github.kittinunf.fuel.core.requests.UploadRequest9import com.github.kittinunf.fuel.core.requests.download10import com.github.kittinunf.fuel.core.requests.upload11import com.github.kittinunf.fuel.toolbox.HttpClient12import com.github.kittinunf.fuel.util.readWriteLazy13import java.net.Proxy14import java.security.KeyStore15import java.util.concurrent.Executor16import java.util.concurrent.ExecutorService17import java.util.concurrent.Executors18import javax.net.ssl.HostnameVerifier19import javax.net.ssl.HttpsURLConnection20import javax.net.ssl.SSLContext21import javax.net.ssl.SSLSocketFactory22import javax.net.ssl.TrustManagerFactory23typealias FoldableRequestInterceptor = (RequestTransformer) -> RequestTransformer24typealias FoldableResponseInterceptor = (ResponseTransformer) -> ResponseTransformer25class FuelManager : RequestFactory, RequestFactory.Convenience {26 var client: Client by readWriteLazy { HttpClient(proxy, hook = hook) }27 var proxy: Proxy? = null28 var basePath: String? = null29 var timeoutInMillisecond: Int = 15_00030 var timeoutReadInMillisecond: Int = timeoutInMillisecond31 var progressBufferSize: Int = DEFAULT_BUFFER_SIZE32 var hook: Hook = DefaultHook()33 var baseHeaders: Map<String, String>? = null34 var baseParams: Parameters = emptyList()35 var keystore: KeyStore? = null36 var socketFactory: SSLSocketFactory by readWriteLazy {37 keystore?.let {38 val trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())39 trustFactory.init(it)40 val sslContext = SSLContext.getInstance("SSL")41 sslContext.init(null, trustFactory.trustManagers, null)42 sslContext.socketFactory43 } ?: HttpsURLConnection.getDefaultSSLSocketFactory()44 }45 var hostnameVerifier: HostnameVerifier by readWriteLazy {46 HttpsURLConnection.getDefaultHostnameVerifier()47 }48 // background executionOptions49 var executorService: ExecutorService by readWriteLazy {50 Executors.newCachedThreadPool { command ->51 Thread(command).also { thread ->52 thread.priority = Thread.NORM_PRIORITY53 thread.isDaemon = true54 }55 }56 }57 private val requestInterceptors: MutableList<FoldableRequestInterceptor> =58 mutableListOf(ParameterEncoder)59 private val responseInterceptors: MutableList<FoldableResponseInterceptor> =60 mutableListOf(redirectResponseInterceptor(this))61 // callback executionOptions62 var callbackExecutor: Executor by readWriteLazy { createEnvironment().callbackExecutor }63 var forceMethods: Boolean = false64 /**65 * Make a request using [method] to [path] with [parameters]66 *67 * @see FuelManager.instance68 * @see FuelManager.applyOptions69 *70 * @param method [Method] the HTTP method to make the request with71 * @param path [String] the absolute url or relative to [FuelManager.instance] basePath72 * @param parameters [Parameters?] list of parameters73 *74 * @return [Request] the request75 */76 override fun request(method: Method, path: String, parameters: Parameters?): Request {77 val request = request(Encoding(78 httpMethod = method,79 urlString = path,80 baseUrlString = basePath,81 parameters = if (parameters == null) baseParams else baseParams + parameters82 ).request)83 return applyOptions(request)84 }85 /**86 * Make a request using [method] to [convertible]'s path with [parameters]87 *88 * @see FuelManager.instance89 * @see RequestFactory(Method, String, Parameters?)90 *91 * @param method [Method] the HTTP method to make the request with92 * @param convertible [PathStringConvertible]93 * @param parameters [Parameters?] list of parameters94 *95 * @return [Request] the request96 */97 override fun request(method: Method, convertible: PathStringConvertible, parameters: Parameters?): Request =98 request(method, convertible.path, parameters)99 /**100 * Make a request using from [convertible]101 *102 * @param convertible [RequestConvertible] the instance that can be turned into a [Request]103 * @return [Request] the request104 */105 override fun request(convertible: RequestConvertible): Request = applyOptions(convertible.request)106 /**107 * Create a [method] [Request] to [path] with [parameters], which can download to a file108 *109 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path110 * @param method [Method] the method to download with, defaults to [Method.GET]111 * @param parameters [Parameters] the optional parameters112 * @return [DownloadRequest] the request (extended for download)113 */114 override fun download(path: String, method: Method, parameters: Parameters?): DownloadRequest {115 val request = Encoding(116 httpMethod = method,117 urlString = path,118 baseUrlString = basePath,119 parameters = if (parameters == null) baseParams else baseParams + parameters120 ).request121 return applyOptions(request).download()122 }123 /**124 * Create a [method] [Request] to [path] with [parameters], which can upload blobs and Data Parts125 *126 * @param path [String] the absolute or relative to [FuelManager.instance]' base-path path127 * @param method [Method] the method to upload with, defaults to [Method.POST]128 * @param parameters [Parameters] the optional parameters129 * @return [UploadRequest] the request (extended for upload)130 */131 override fun upload(path: String, method: Method, parameters: Parameters?): UploadRequest {132 val request = Encoding(133 httpMethod = method,134 urlString = path,135 baseUrlString = basePath,136 parameters = if (parameters == null) baseParams else baseParams + parameters137 ).request138 return applyOptions(request).upload()139 }140 fun addRequestInterceptor(interceptor: FoldableRequestInterceptor): FuelManager {141 requestInterceptors += interceptor142 return this143 }144 fun addResponseInterceptor(interceptor: FoldableResponseInterceptor): FuelManager {145 responseInterceptors += interceptor146 return this147 }148 fun removeRequestInterceptor(interceptor: FoldableRequestInterceptor): FuelManager {149 requestInterceptors -= interceptor150 return this151 }152 fun removeResponseInterceptor(interceptor: FoldableResponseInterceptor): FuelManager {153 responseInterceptors -= interceptor154 return this155 }156 fun removeAllRequestInterceptors(): FuelManager {157 requestInterceptors.clear()158 return this159 }160 fun removeAllResponseInterceptors(): FuelManager {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 *...
RequestSharedInstanceTest.kt
Source:RequestSharedInstanceTest.kt
...290 }291 @Test292 fun httpDownloadWithProgressValidCase() {293 mock.chain(294 request = mock.request().withMethod(Method.GET.value).withPath("/Fuel/download"),295 response = mock.reflect()296 )297 var read = -1L298 var total = -1L299 val (request, response, result) = Fuel.download(mock.path("Fuel/download"))300 .fileDestination { _, _ -> File.createTempFile("download.dl", null) }301 .progress { readBytes, totalBytes ->302 read = readBytes303 total = totalBytes304 }305 .responseString()306 val (data, error) = result307 assertThat(request, notNullValue())308 assertThat(response, notNullValue())309 assertThat(error, nullValue())310 assertThat(data, notNullValue())311 val statusCode = HttpURLConnection.HTTP_OK312 assertThat(response.statusCode, equalTo(statusCode))313 assertThat(read, equalTo(total))314 }...
RequestFactory.kt
Source:RequestFactory.kt
...49 val request: Request50 }51 interface Convenience : RequestFactory {52 /**53 * Create a [method] [Request] to [path] with [parameters], which can download to a file54 *55 * @parameters path [String] the absolute or relative to [FuelManager.instance]' base-path path56 * @parameters method [Method] the method to download with, defaults to [Method.GET]57 * @parameters parameters [Parameters] the optional parameters58 * @return [DownloadRequest] the request (extended for download)59 */60 fun download(path: String, method: Method = Method.GET, parameters: Parameters? = null): DownloadRequest61 /**62 * Create a [method] [Request] to [PathStringConvertible.path] with [parameters], which can download to a file63 *64 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path65 * @param method [Method] the method to download with, defaults to [Method.GET]66 * @param parameters [Parameters] the optional parameters67 * @return [DownloadRequest] the request (extended for download)68 */69 fun download(convertible: PathStringConvertible, method: Method = Method.GET, parameters: Parameters? = null): DownloadRequest70 /**71 * Create a [method] [Request] to [PathStringConvertible.path] with [parameters], which can upload blobs and72 * Data Parts73 *74 * @param convertible [PathStringConvertible] the absolute or relative to [FuelManager.instance]' base-path path75 * @param method [Method] the method to upload with, defaults to [Method.POST]76 * @param parameters [Parameters] the optional parameters77 * @return [UploadRequest] the request (extended for upload)78 */79 fun upload(convertible: PathStringConvertible, method: Method = Method.POST, parameters: Parameters? = null): UploadRequest80 /**81 * Create a [method] [Request] to [path] with [parameters], which can upload blobs and Data Parts82 *83 * @parameters path [String] the absolute or relative to [FuelManager.instance]' base-path path...
RequestPathStringConvertibleExtensionTest.kt
Source:RequestPathStringConvertibleExtensionTest.kt
...131 }132 @Test133 fun httpDownloadRequestWithSharedInstance() {134 mock.chain(135 request = mock.request().withMethod(Method.GET.value).withPath("/http-download"),136 response = mock.reflect()137 )138 val (request, response, result) = PathStringConvertibleImpl(mock.path("http-download"))139 .httpDownload()140 .fileDestination { _, _ -> File.createTempFile("123456", null) }141 .responseString()142 val (data, error) = result143 assertThat(request, notNullValue())144 assertThat(response, notNullValue())145 assertThat(error, nullValue())146 assertThat(data, notNullValue())147 val statusCode = HttpURLConnection.HTTP_OK148 assertThat(response.statusCode, equalTo(statusCode))149 }150}...
Fuel.kt
Source:Fuel.kt
...34 Fuel.delete(this, parameters)35fun RequestFactory.PathStringConvertible.httpDelete(parameter: Parameters? = null): Request =36 this.path.httpDelete(parameter)37fun String.httpDownload(parameter: Parameters? = null, method: Method = Method.GET): DownloadRequest =38 Fuel.download(this, method, parameter)39fun RequestFactory.PathStringConvertible.httpDownload(parameters: Parameters? = null, method: Method = Method.GET): DownloadRequest =40 this.path.httpDownload(parameters, method)41fun String.httpUpload(parameters: Parameters? = null, method: Method = Method.POST): UploadRequest =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)...
download
Using AI Code Generation
1 println("Progress: $readBytes/$totalBytes")2}3 println("Progress: $readBytes/$totalBytes")4} destination { response, url ->5 File.createTempFile("download", ".tmp")6}7 println("Progress: $readBytes/$totalBytes")8} destination { response, url ->9 File.createTempFile("download", ".tmp")10} progress { readBytes, totalBytes ->11 println("Progress: $readBytes/$totalBytes")12}13 println("Progress: $readBytes/$totalBytes")14} destination { response, url ->15 File.createTempFile("download", ".tmp")16} progress { readBytes, totalBytes ->17 println("Progress: $readBytes/$totalBytes")18} file { response ->19 File.createTempFile("download", ".tmp")20}21 println("Progress: $readBytes/$totalBytes")22} destination { response, url ->
download
Using AI Code Generation
1.download()2.destination { response, url -> File.createTempFile("download", "tmp") }3.progress { readBytes, totalBytes -> println("Progress: $readBytes / $totalBytes") }4.response()5println(response)6println(result)7.destination { response, url -> File.createTempFile("download", "tmp") }8.progress { readBytes, totalBytes -> println("Progress: $readBytes / $totalBytes") }9.response()10println(response)11println(result)12.destination { response, url -> File.createTempFile("download", "tmp") }13.progress { readBytes, totalBytes -> println("Progress: $readBytes / $totalBytes") }14.response()15println(response)16println(result)17.destination { response, url -> File.createTempFile("download", "tmp") }18.progress { readBytes, totalBytes -> println("Progress: $readBytes / $totalBytes") }19.response()20println(response)21println(result)22.destination { response, url -> File.createTempFile("download", "tmp") }23.progress { readBytes, totalBytes -> println("Progress: $readBytes / $totalBytes") }24.response()25println(response)26println(result)27.destination { response, url -> File.createTempFile("download", "tmp") }28.progress { readBytes, totalBytes -> println("Progress: $readBytes / $totalBytes") }29.response()30println(response)31println(result)
download
Using AI Code Generation
1}2}3}4}5}6}7}8}9}
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!!