/* * Copyright (c) 2017-present Robert Jaros * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package pl.treksoft.kvision.remote import kotlinx.coroutines.experimental.Deferred import kotlinx.coroutines.experimental.asDeferred import kotlinx.serialization.KSerializer import kotlinx.serialization.internal.ArrayListSerializer import kotlinx.serialization.internal.BooleanSerializer import kotlinx.serialization.internal.CharSerializer import kotlinx.serialization.internal.DoubleSerializer import kotlinx.serialization.internal.LongSerializer import kotlinx.serialization.internal.StringSerializer import kotlinx.serialization.json.JSON import kotlinx.serialization.list import kotlinx.serialization.serializer import kotlin.js.js import kotlin.reflect.KClass import kotlin.js.JSON as NativeJSON internal class NonStandardTypeException(type: String) : Exception("Non standard type: $type!") /** * Client side agent for JSON-RPC remote calls. */ @Suppress("EXPERIMENTAL_FEATURE_WARNING", "LargeClass", "TooManyFunctions") open class RemoteAgent(val serviceManager: ServiceManager) { val callAgent = CallAgent() /** * Executes defined call to a remote web service. */ inline fun call(noinline function: T.(Request?) -> Deferred): Deferred { val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { try { deserialize(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer(), it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(Request?) -> Deferred> ): Deferred> { val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { try { deserializeLists(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer().list, it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR, Request?) -> Deferred, p: PAR, serializer: KSerializer? = null ): Deferred { val data = serialize(p, serializer) 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(it, (RET::class as KClass).js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer(), it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR, Request?) -> Deferred>, p: PAR, serializer: KSerializer? = null ): Deferred> { val data = serialize(p, serializer) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data), method).then { try { deserializeLists(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer().list, it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR1, PAR2, Request?) -> Deferred, p1: PAR1, p2: PAR2, serializer1: KSerializer? = null, serializer2: KSerializer? = null ): Deferred { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { try { deserialize(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer(), it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR1, PAR2, Request?) -> Deferred>, p1: PAR1, p2: PAR2, serializer1: KSerializer? = null, serializer2: KSerializer? = null ): Deferred> { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { try { deserializeLists(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer().list, it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3, serializer1: KSerializer? = null, serializer2: KSerializer? = null, serializer3: KSerializer? = null ): Deferred { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val data3 = serialize(p3, serializer3) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { try { deserialize(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer(), it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred>, p1: PAR1, p2: PAR2, p3: PAR3, serializer1: KSerializer? = null, serializer2: KSerializer? = null, serializer3: KSerializer? = null ): Deferred> { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val data3 = serialize(p3, serializer3) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { try { deserializeLists(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer().list, it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, serializer1: KSerializer? = null, serializer2: KSerializer? = null, serializer3: KSerializer? = null, serializer4: KSerializer? = null ): Deferred { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val data3 = serialize(p3, serializer3) val data4 = serialize(p4, serializer4) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { try { deserialize(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer(), it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ inline fun call( noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred>, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, serializer1: KSerializer? = null, serializer2: KSerializer? = null, serializer3: KSerializer? = null, serializer4: KSerializer? = null ): Deferred> { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val data3 = serialize(p3, serializer3) val data4 = serialize(p4, serializer4) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { try { deserializeLists(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer().list, it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ @Suppress("LongParameterList") inline fun call( noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, p5: PAR5, serializer1: KSerializer? = null, serializer2: KSerializer? = null, serializer3: KSerializer? = null, serializer4: KSerializer? = null, serializer5: KSerializer? = null ): Deferred { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val data3 = serialize(p3, serializer3) val data4 = serialize(p4, serializer4) val data5 = serialize(p5, serializer5) 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 { deserialize(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer(), it) } }.asDeferred() } /** * Executes defined call to a remote web service. */ @Suppress("LongParameterList") inline fun call( noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred>, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, p5: PAR5, serializer1: KSerializer? = null, serializer2: KSerializer? = null, serializer3: KSerializer? = null, serializer4: KSerializer? = null, serializer5: KSerializer? = null ): Deferred> { val data1 = serialize(p1, serializer1) val data2 = serialize(p2, serializer2) val data3 = serialize(p3, serializer3) val data4 = serialize(p4, serializer4) val data5 = serialize(p5, serializer5) 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 { deserializeLists(it, RET::class.js.name) } catch (t: NonStandardTypeException) { JSON.nonstrict.parse(RET::class.serializer().list, it) } }.asDeferred() } /** * @suppress * Internal function */ @Suppress("TooGenericExceptionCaught", "NestedBlockDepth") inline fun serialize(value: PAR, serializer: KSerializer?): String? { return value?.let { if (serializer != null) { JSON.stringify(serializer, it) } else { if (it is Enum<*>) { "\"$it\"" } else { try { @Suppress("UNCHECKED_CAST") JSON.stringify((PAR::class as KClass).serializer(), it as Any) } catch (e: Throwable) { it.toString() } } } } } /** * @suppress * Internal function */ @Suppress("UNCHECKED_CAST") fun deserialize(value: String, type: String): RET { return when (type) { "String" -> JSON.parse(StringSerializer, value) as RET "Number" -> JSON.parse(DoubleSerializer, value) as RET "Long" -> JSON.parse(LongSerializer, value) as RET "Boolean" -> JSON.parse(BooleanSerializer, value) as RET "Char" -> JSON.parse(CharSerializer, value) as RET else -> throw NonStandardTypeException(type) } } /** * @suppress * Internal function */ @Suppress("UNCHECKED_CAST") fun deserializeLists(value: String, type: String): List { return when (type) { "String" -> JSON.parse(ArrayListSerializer(StringSerializer), value) as List "Number" -> JSON.parse(ArrayListSerializer(DoubleSerializer), value) as List "Long" -> JSON.parse(ArrayListSerializer(LongSerializer), value) as List "Boolean" -> JSON.parse(ArrayListSerializer(BooleanSerializer), value) as List "Char" -> JSON.parse(ArrayListSerializer(CharSerializer), value) as List else -> throw NonStandardTypeException(type) } } }