From 6eca49df30836633cb584e2a62301825fab6f883 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 15 Apr 2020 20:16:39 +0200 Subject: Increase the number of parameters for server side interface methods --- .../pl/treksoft/kvision/remote/KVRemoteAgent.kt | 75 ++++++++++++++++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 12 ++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 19 +++++- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 50 +++++++++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 12 ++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 19 +++++- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 48 ++++++++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 12 ++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 19 +++++- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 55 ++++++++++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 12 ++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 19 +++++- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 62 ++++++++++++++++++ 13 files changed, 410 insertions(+), 4 deletions(-) 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 232e0607..24cea176 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 @@ -373,6 +373,81 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : }.asDeferred().await() } + /** + * Executes defined call to a remote web service. + */ + @Suppress("LongParameterList") + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5, + p6: PAR6 + ): RET { + val data1 = serialize(p1) + val data2 = serialize(p2) + val data3 = serialize(p3) + val data4 = serialize(p4) + val data5 = serialize(p5) + val data6 = serialize(p6) + 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 { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + @Suppress("LongParameterList") + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> List, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5, + p6: PAR6 + ): List { + val data1 = serialize(p1) + val data2 = serialize(p2) + val data3 = serialize(p3) + val data4 = serialize(p4) + val data5 = serialize(p5) + val data6 = serialize(p6) + 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 { + 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().await() + } + /** * Executes defined web socket connection */ diff --git a/kvision-modules/kvision-server-javalin/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-javalin/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 19da13c0..3ba4998d 100644 --- a/kvision-modules/kvision-server-javalin/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-javalin/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -103,6 +103,18 @@ expect open class KVServiceManager(serviceClass: KClass) { route: String? = null ) + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod = HttpMethod.POST, + route: String? = null + ) + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-javalin/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-javalin/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 23da6ac5..c7a35150 100644 --- a/kvision-modules/kvision-server-javalin/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-javalin/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -28,7 +28,7 @@ import kotlin.reflect.KClass /** * Multiplatform service manager. */ -actual open class KVServiceManager actual constructor(serviceClass: KClass): KVServiceMgr { +actual open class KVServiceManager actual constructor(serviceClass: KClass) : KVServiceMgr { protected val calls: MutableMap> = mutableMapOf() var counter: Int = 0 @@ -128,6 +128,23 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) + } + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index e3189a74..33c858f4 100644 --- a/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -383,6 +383,56 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + addRoute(method, "/kv/$routeDef") { ctx -> + val jsonRpcRequest = ctx.body() + @Suppress("MagicNumber") + if (jsonRpcRequest.params.size == 6) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + val param4 = getParameter(jsonRpcRequest.params[3]) + val param5 = getParameter(jsonRpcRequest.params[4]) + val param6 = getParameter(jsonRpcRequest.params[5]) + val injector = ctx.attribute(KV_INJECTOR_KEY)!! + val service = injector.getInstance(serviceClass.java) + initializeService(service, ctx) + val future = GlobalScope.future { + try { + val result = function.invoke(service, param1, param2, param3, param4, param5, param6) + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + } catch (e: Exception) { + if (e !is ServiceException) LOG.error(e.message, e) + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + } + } + ctx.json(future) + } else { + ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) + } + } + } + /** * Binds a given web socket connection with a function of the receiver. * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-jooby/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 19da13c0..3ba4998d 100644 --- a/kvision-modules/kvision-server-jooby/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -103,6 +103,18 @@ expect open class KVServiceManager(serviceClass: KClass) { route: String? = null ) + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod = HttpMethod.POST, + route: String? = null + ) + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-jooby/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 23da6ac5..c7a35150 100644 --- a/kvision-modules/kvision-server-jooby/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -28,7 +28,7 @@ import kotlin.reflect.KClass /** * Multiplatform service manager. */ -actual open class KVServiceManager actual constructor(serviceClass: KClass): KVServiceMgr { +actual open class KVServiceManager actual constructor(serviceClass: KClass) : KVServiceMgr { protected val calls: MutableMap> = mutableMapOf() var counter: Int = 0 @@ -128,6 +128,23 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) + } + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index befeca22..ea37c477 100644 --- a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -345,6 +345,52 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + addRoute(method, "/kv/$routeDef") { + val jsonRpcRequest = ctx.body() + @Suppress("MagicNumber") + if (jsonRpcRequest.params.size == 6) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + val param4 = getParameter(jsonRpcRequest.params[3]) + val param5 = getParameter(jsonRpcRequest.params[4]) + val param6 = getParameter(jsonRpcRequest.params[5]) + val service = ctx.require(serviceClass.java) + initializeService(service, ctx) + try { + val result = function.invoke(service, param1, param2, param3, param4, param5, param6) + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + } catch (e: Exception) { + if (e !is ServiceException) LOG.error(e.message, e) + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + } + } else { + JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters") + } + } + } + /** * Binds a given web socket connection with a function of the receiver. * @param function a function of the receiver @@ -435,8 +481,10 @@ actual open class KVServiceManager actual constructor(val serviceClass: val param1 = getParameter(jsonRpcRequest.params[0]) val param2 = getParameter(jsonRpcRequest.params[1]) val param3 = getParameter?>(jsonRpcRequest.params[2]) + @Suppress("MagicNumber") val param4 = getParameter?>(jsonRpcRequest.params[3]) + @Suppress("MagicNumber") val param5 = getParameter(jsonRpcRequest.params[4]) val service = ctx.require(serviceClass.java) diff --git a/kvision-modules/kvision-server-ktor/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 19da13c0..3ba4998d 100644 --- a/kvision-modules/kvision-server-ktor/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -103,6 +103,18 @@ expect open class KVServiceManager(serviceClass: KClass) { route: String? = null ) + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod = HttpMethod.POST, + route: String? = null + ) + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-ktor/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 23da6ac5..c7a35150 100644 --- a/kvision-modules/kvision-server-ktor/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -28,7 +28,7 @@ import kotlin.reflect.KClass /** * Multiplatform service manager. */ -actual open class KVServiceManager actual constructor(serviceClass: KClass): KVServiceMgr { +actual open class KVServiceManager actual constructor(serviceClass: KClass) : KVServiceMgr { protected val calls: MutableMap> = mutableMapOf() var counter: Int = 0 @@ -128,6 +128,23 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) + } + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-ktor/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 25771091..aee92d31 100644 --- a/kvision-modules/kvision-server-ktor/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -391,6 +391,61 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + addRoute(method, "/kv/$routeDef") { + val service = call.injector.createChildInjector(DummyWsSessionModule()).getInstance(serviceClass.java) + val jsonRpcRequest = call.receive() + @Suppress("MagicNumber") + if (jsonRpcRequest.params.size == 6) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + val param4 = getParameter(jsonRpcRequest.params[3]) + val param5 = getParameter(jsonRpcRequest.params[4]) + val param6 = getParameter(jsonRpcRequest.params[5]) + try { + val result = function.invoke(service, param1, param2, param3, param4, param5, param6) + call.respond( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + } catch (e: Exception) { + if (e !is ServiceException) LOG.error(e.message, e) + call.respond( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) + } + } else { + call.respond( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + } + } + } + /** * Binds a given web socket connetion with a function of the receiver. * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-spring-boot/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 19da13c0..3ba4998d 100644 --- a/kvision-modules/kvision-server-spring-boot/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/commonMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -103,6 +103,18 @@ expect open class KVServiceManager(serviceClass: KClass) { route: String? = null ) + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod = HttpMethod.POST, + route: String? = null + ) + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-spring-boot/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 23da6ac5..c7a35150 100644 --- a/kvision-modules/kvision-server-spring-boot/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -28,7 +28,7 @@ import kotlin.reflect.KClass /** * Multiplatform service manager. */ -actual open class KVServiceManager actual constructor(serviceClass: KClass): KVServiceMgr { +actual open class KVServiceManager actual constructor(serviceClass: KClass) : KVServiceMgr { protected val calls: MutableMap> = mutableMapOf() var counter: Int = 0 @@ -128,6 +128,23 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) + } + /** * Binds a given function of the receiver as a tabulator component source * @param function a function of the receiver diff --git a/kvision-modules/kvision-server-spring-boot/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index c4855466..1e9bb949 100644 --- a/kvision-modules/kvision-server-spring-boot/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -443,6 +443,68 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given route with a function of the receiver. + * @param function a function of the receiver + * @param method a HTTP method + * @param route a route + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET, + method: HttpMethod, route: String? + ) { + if (method == HttpMethod.GET) + throw UnsupportedOperationException("GET method is only supported for methods without parameters") + val routeDef = route ?: "route${this::class.simpleName}${counter++}" + addRoute(method, "/kv/$routeDef") { req, ctx -> + val service = ctx.getBean(serviceClass.java) + initializeService(service, req) + val jsonRpcRequest = req.awaitBody() + @Suppress("MagicNumber") + if (jsonRpcRequest.params.size == 6) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + val param4 = getParameter(jsonRpcRequest.params[3]) + val param5 = getParameter(jsonRpcRequest.params[4]) + val param6 = getParameter(jsonRpcRequest.params[5]) + try { + val result = function.invoke(service, param1, param2, param3, param4, param5, param6) + ServerResponse.ok().json().bodyValueAndAwait( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + if (e !is ServiceException) LOG.error(e.message, e) + ServerResponse.ok().json().bodyValueAndAwait( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) + ) + } + } else { + ServerResponse.ok().json().bodyValueAndAwait( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + /** * Binds a given web socket connetion with a function of the receiver. * @param function a function of the receiver -- cgit