Is there a way to chain optionally a method in Kotlin, like for example on a FuelManager class, where I would like to have body method as an optional thing, so this would be a request with a body method:
val (_, response, data) = manager .request(Method.POST, url) .timeoutRead(2_000) .timeout(1_000) .header("Content-Type" to "application/json") .header("X-API-Key" to ctx.conf.get(ConfValues.ApiSecret)) .body(payload.toString()) .responseString() So, here I would like to check if payload exists, then I would add body method, if not I would not add body to a request. Request without body method.
val (_, response, data) = manager .request(Method.POST, url) .timeoutRead(2_000) .timeout(1_000) .header("Content-Type" to "application/json") .header("X-API-Key" to ctx.conf.get(ConfValues.ApiSecret)) .responseString()
applyinstead of chaining. This way you can put if statements wherever you like. Is each of the chained methods declared in the same type?