diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt | 340 |
1 files changed, 2 insertions, 338 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt b/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt index b757398a..b1ee4fde 100644 --- a/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt +++ b/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt @@ -21,8 +21,6 @@ */ package pl.treksoft.kvision.remote -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.asDeferred import kotlinx.serialization.KSerializer import kotlinx.serialization.internal.BooleanSerializer import kotlinx.serialization.internal.ByteSerializer @@ -39,350 +37,16 @@ import pl.treksoft.kvision.types.DateSerializer import pl.treksoft.kvision.types.toStringF import pl.treksoft.kvision.utils.JSON import kotlin.js.Date -import kotlin.js.asDynamic -import kotlin.js.js import kotlin.reflect.KClass -import kotlin.js.JSON as NativeJSON internal class NotStandardTypeException(type: String) : Exception("Not a standard type: $type!") internal class NotEnumTypeException : Exception("Not the Enum type!") /** - * Client side agent for JSON-RPC remote calls. + * Interface for client side agent for JSON-RPC remote calls. */ -@Suppress("LargeClass", "TooManyFunctions") -open class RemoteAgent<out T>(val serviceManager: ServiceManager<T>) { - - val callAgent = CallAgent() - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified RET : Any, T> call(noinline function: T.(Request?) -> Deferred<RET>): Deferred<RET> { - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, method = method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified RET : Any, T> call( - noinline function: T.(Request?) -> Deferred<List<RET>> - ): Deferred<List<RET>> { - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, method = method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR, reified RET : Any, T> call( - noinline function: T.(PAR, Request?) -> Deferred<RET>, p: PAR - ): Deferred<RET> { - val data = serialize(p) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR, reified RET : Any, T> call( - noinline function: T.(PAR, Request?) -> Deferred<List<RET>>, p: PAR - ): Deferred<List<RET>> { - val data = serialize(p) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR1, reified PAR2, reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>, p1: PAR1, p2: PAR2 - ): Deferred<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR1, reified PAR2, reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred<List<RET>>, p1: PAR1, p2: PAR2 - ): Deferred<List<RET>> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>, p1: PAR1, p2: PAR2, p3: PAR3 - ): Deferred<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<List<RET>>, p1: PAR1, p2: PAR2, p3: PAR3 - ): Deferred<List<RET>> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 - ): Deferred<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<List<RET>>, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4 - ): Deferred<List<RET>> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - @Suppress("LongParameterList") - inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, - reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4, - p5: PAR5 - ): Deferred<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val data5 = serialize(p5) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred() - } - - /** - * Executes defined call to a remote web service. - */ - @Suppress("LongParameterList") - inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, - reified RET : Any, T> call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<List<RET>>, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4, - p5: PAR5 - ): Deferred<List<RET>> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val data5 = serialize(p5) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - - /** - * @suppress - * Internal function - */ - inline fun <reified PAR> serialize(value: PAR): String? { - return value?.let { - @Suppress("UNCHECKED_CAST") - trySerialize((PAR::class as KClass<Any>), it as Any) - } - } +interface RemoteAgent { /** * @suppress |