From 3f2925996743bb3dba8464f863efe124aba85a51 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 20 May 2020 22:38:08 +0200 Subject: Support for request configuration when using remote interfaces. --- .../kotlin/pl/treksoft/kvision/remote/CallAgent.kt | 4 ++- .../pl/treksoft/kvision/remote/KVRemoteAgent.kt | 36 ++++++++++++---------- 2 files changed, 23 insertions(+), 17 deletions(-) (limited to 'kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision') diff --git a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt index 3d865f04..e357c024 100644 --- a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt +++ b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt @@ -57,7 +57,8 @@ open class CallAgent { fun jsonRpcCall( url: String, data: List = listOf(), - method: HttpMethod = HttpMethod.POST + method: HttpMethod = HttpMethod.POST, + beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null ): Promise { val jsonRpcRequest = JsonRpcRequest(counter++, url, data) val jsonData = if (method == HttpMethod.GET) { @@ -103,6 +104,7 @@ open class CallAgent { this.xhrFields = obj { this.withCredentials = true } + this.beforeSend = beforeSend }) } } diff --git a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt index 8836c7f1..fe655b87 100644 --- a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt +++ b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt @@ -34,6 +34,8 @@ import kotlinx.serialization.builtins.list import kotlinx.serialization.serializer import kotlinx.serialization.stringify import org.w3c.dom.get +import pl.treksoft.jquery.JQueryAjaxSettings +import pl.treksoft.jquery.JQueryXHR import kotlin.browser.window import kotlin.reflect.KClass @@ -42,8 +44,10 @@ import kotlin.reflect.KClass */ @Suppress("LargeClass", "TooManyFunctions") @OptIn(ImplicitReflectionSerializer::class, ExperimentalCoroutinesApi::class) -open class KVRemoteAgent(val serviceManager: KVServiceMgr) : - RemoteAgent { +open class KVRemoteAgent( + val serviceManager: KVServiceMgr, + val beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null +) : RemoteAgent { val callAgent = CallAgent() @@ -54,7 +58,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, method = method).then { + return callAgent.jsonRpcCall(url, method = method, beforeSend = beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -78,7 +82,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, method = method).then { + return callAgent.jsonRpcCall(url, method = method, beforeSend = beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { @@ -102,7 +106,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data), method).then { + return callAgent.jsonRpcCall(url, listOf(data), method, beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -127,7 +131,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data), method).then { + return callAgent.jsonRpcCall(url, listOf(data), method, beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { @@ -152,7 +156,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2), method, beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -178,7 +182,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2), method, beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { @@ -204,7 +208,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method, beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -231,7 +235,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method, beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { @@ -258,7 +262,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method, beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -290,7 +294,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method, beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { @@ -325,7 +329,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method, beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -361,7 +365,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method, beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { @@ -398,7 +402,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5, data6), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5, data6), method, beforeSend).then { try { @Suppress("UNCHECKED_CAST") deserialize(it, RET::class.js.name) @@ -436,7 +440,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val (url, method) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5, data6), method).then { + return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5, data6), method, beforeSend).then { try { deserializeList(it, RET::class.js.name) } catch (t: NotStandardTypeException) { -- cgit