From 0c9ee826d545befe2c6e1c740bd5f74ed453b899 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 20 Oct 2018 03:43:02 +0200 Subject: New kvision-server-spring-boot module with support for Spring Boot server-side integration. Kotlin upgrade to 1.3.0-rc-190. --- .../pl/treksoft/kvision/remote/JoobyRemoteAgent.kt | 369 +++++++++++++++++++++ .../treksoft/kvision/remote/JoobyServiceManager.kt | 131 ++++++++ .../pl/treksoft/kvision/remote/RemoteAgent.kt | 340 +------------------ .../pl/treksoft/kvision/remote/ServiceManager.kt | 137 -------- .../treksoft/kvision/remote/SpringRemoteAgent.kt | 369 +++++++++++++++++++++ .../kvision/remote/SpringServiceManager.kt | 125 +++++++ 6 files changed, 996 insertions(+), 475 deletions(-) create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt (limited to 'src/main/kotlin/pl/treksoft') diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt new file mode 100644 index 00000000..b4cc6c21 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt @@ -0,0 +1,369 @@ +/* + * 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.Deferred +import kotlinx.coroutines.asDeferred +import kotlinx.serialization.list +import kotlinx.serialization.serializer +import pl.treksoft.kvision.utils.JSON +import kotlin.js.js +import kotlin.reflect.KClass +import kotlin.js.JSON as NativeJSON + +/** + * Client side agent for JSON-RPC remote calls with Jooby. + */ +@Suppress("LargeClass", "TooManyFunctions") +open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) : RemoteAgent { + + 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 { + @Suppress("UNCHECKED_CAST") + deserialize(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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 { + deserializeList(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, 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 call( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred>, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + JSON.nonstrict.parse(RET::class.serializer().list, it) + } + } + }.asDeferred() + } + + + /** + * @suppress + * Internal function + */ + inline fun serialize(value: PAR): String? { + return value?.let { + @Suppress("UNCHECKED_CAST") + trySerialize((PAR::class as KClass), it as Any) + } + } + +} diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt new file mode 100644 index 00000000..c9d8ba25 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt @@ -0,0 +1,131 @@ +/* + * 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.Deferred + +/** + * Multiplatform service manager for Jooby. + */ +actual open class JoobyServiceManager actual constructor(service: T) : ServiceManager { + + protected val calls: MutableMap> = mutableMapOf() + var counter: Int = 0 + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(Request?) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR, Request?) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, Request?) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, + route: String?, + method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Applies all defined routes to the given server. + * Not used on the js platform. + */ + actual fun applyRoutes(k: KVServer) { + } + + /** + * Returns the map of defined paths. + */ + override fun getCalls(): Map> = calls +} 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(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 { - @Suppress("UNCHECKED_CAST") - deserialize(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass, it) as RET - } catch (t: NotEnumTypeException) { - 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 { - deserializeList(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass, it) as List - } catch (t: NotEnumTypeException) { - 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 - ): Deferred { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass, it) as RET - } catch (t: NotEnumTypeException) { - 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 - ): Deferred> { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass, it) as List - } catch (t: NotEnumTypeException) { - 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 - ): Deferred { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass, it) as RET - } catch (t: NotEnumTypeException) { - 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 - ): Deferred> { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass, it) as List - } catch (t: NotEnumTypeException) { - 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 - ): Deferred { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass, it) as RET - } catch (t: NotEnumTypeException) { - 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 - ): Deferred> { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass, it) as List - } catch (t: NotEnumTypeException) { - 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 - ): Deferred { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass, it) as RET - } catch (t: NotEnumTypeException) { - 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 - ): Deferred> { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass, it) as List - } catch (t: NotEnumTypeException) { - 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 - ): Deferred { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass, 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 call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred>, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4, - p5: PAR5 - ): Deferred> { - 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(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass, it) as List - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred() - } - - - /** - * @suppress - * Internal function - */ - inline fun serialize(value: PAR): String? { - return value?.let { - @Suppress("UNCHECKED_CAST") - trySerialize((PAR::class as KClass), it as Any) - } - } +interface RemoteAgent { /** * @suppress diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt deleted file mode 100644 index c6487494..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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.Deferred - -/** - * Multiplatform service manager. - */ -actual open class ServiceManager actual constructor(service: T) { - protected val calls: MutableMap> = mutableMapOf() - var counter: Int = 0 - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - * @param prefix an URL address prefix - */ - protected actual inline fun bind( - noinline function: T.(Request?) -> Deferred, - route: String?, method: RpcHttpMethod, prefix: String - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("$prefix$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - * @param prefix an URL address prefix - */ - protected actual inline fun bind( - noinline function: T.(PAR, Request?) -> Deferred, - route: String?, method: RpcHttpMethod, prefix: String - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("$prefix$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - * @param prefix an URL address prefix - */ - protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred, - route: String?, method: RpcHttpMethod, prefix: String - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("$prefix$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - * @param prefix an URL address prefix - */ - protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, - route: String?, method: RpcHttpMethod, prefix: String - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("$prefix$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - * @param prefix an URL address prefix - */ - protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, - route: String?, method: RpcHttpMethod, prefix: String - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("$prefix$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - * @param prefix an URL address prefix - */ - protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, - route: String?, - method: RpcHttpMethod, - prefix: String - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("$prefix$routeDef", method) - } - - /** - * Applies all defined routes to the given server. - * Not used on the js platform. - */ - actual fun applyRoutes(k: KVServer) { - } - - /** - * Returns the map of defined paths. - */ - actual fun getCalls(): Map> = calls -} diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt b/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt new file mode 100644 index 00000000..f1f3b473 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt @@ -0,0 +1,369 @@ +/* + * 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.Deferred +import kotlinx.coroutines.asDeferred +import kotlinx.serialization.list +import kotlinx.serialization.serializer +import pl.treksoft.kvision.utils.JSON +import kotlin.js.js +import kotlin.reflect.KClass +import kotlin.js.JSON as NativeJSON + +/** + * Client side agent for JSON-RPC remote calls with Spring Boot. + */ +@Suppress("LargeClass", "TooManyFunctions") +open class SpringRemoteAgent(val serviceManager: SpringServiceManager) : RemoteAgent { + + val callAgent = CallAgent() + + /** + * Executes defined call to a remote web service. + */ + inline fun call(noinline function: T.() -> Deferred): Deferred { + val (url, method) = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return callAgent.jsonRpcCall(url, method = method).then { + try { + @Suppress("UNCHECKED_CAST") + deserialize(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + JSON.nonstrict.parse(RET::class.serializer(), it) + } + } + }.asDeferred() + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.() -> Deferred> + ): Deferred> { + val (url, method) = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return callAgent.jsonRpcCall(url, method = method).then { + try { + deserializeList(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + JSON.nonstrict.parse(RET::class.serializer().list, it) + } + } + }.asDeferred() + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR) -> Deferred, p: PAR + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + JSON.nonstrict.parse(RET::class.serializer(), it) + } + } + }.asDeferred() + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR) -> Deferred>, p: PAR + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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) -> Deferred, p1: PAR1, p2: PAR2 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + JSON.nonstrict.parse(RET::class.serializer(), it) + } + } + }.asDeferred() + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2) -> Deferred>, p1: PAR1, p2: PAR2 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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) -> Deferred>, p1: PAR1, p2: PAR2, p3: PAR3 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, it) as RET + } catch (t: NotEnumTypeException) { + 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) -> Deferred>, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + 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) -> Deferred, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5 + ): Deferred { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnum(RET::class as KClass, 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 call( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred>, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5 + ): Deferred> { + 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(it, RET::class.js.name) + } catch (t: NotStandardTypeException) { + try { + @Suppress("UNCHECKED_CAST") + tryDeserializeEnumList(RET::class as KClass, it) as List + } catch (t: NotEnumTypeException) { + JSON.nonstrict.parse(RET::class.serializer().list, it) + } + } + }.asDeferred() + } + + + /** + * @suppress + * Internal function + */ + inline fun serialize(value: PAR): String? { + return value?.let { + @Suppress("UNCHECKED_CAST") + trySerialize((PAR::class as KClass), it as Any) + } + } + +} diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt new file mode 100644 index 00000000..6b26da8e --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -0,0 +1,125 @@ +/* + * 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.Deferred +import kotlin.reflect.KClass + +/** + * Multiplatform service manager for Spring Boot. + */ +actual open class SpringServiceManager actual constructor(serviceClass: KClass) : ServiceManager { + + protected val calls: MutableMap> = mutableMapOf() + var counter: Int = 0 + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.() -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, PAR3) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred, + route: String?, method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + protected actual inline fun bind( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred, + route: String?, + method: RpcHttpMethod + ) { + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString()] = Pair("/kv/$routeDef", method) + } + + /** + * Returns the map of defined paths. + */ + override fun getCalls(): Map> = calls +} -- cgit