diff options
author | Robert Jaros <rjaros@finn.pl> | 2020-04-15 20:16:39 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2020-04-15 20:16:39 +0200 |
commit | 6eca49df30836633cb584e2a62301825fab6f883 (patch) | |
tree | 579a09dd43c9a33f2fa7237f993a093748c25828 /kvision-modules/kvision-server-jooby/src | |
parent | 07f2a729431ac4e36f6c0573630870b64e4c1705 (diff) | |
download | kvision-6eca49df30836633cb584e2a62301825fab6f883.tar.gz kvision-6eca49df30836633cb584e2a62301825fab6f883.tar.bz2 kvision-6eca49df30836633cb584e2a62301825fab6f883.zip |
Increase the number of parameters for server side interface methods
Diffstat (limited to 'kvision-modules/kvision-server-jooby/src')
3 files changed, 78 insertions, 1 deletions
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 @@ -104,6 +104,18 @@ expect open class KVServiceManager<T : Any>(serviceClass: KClass<T>) { ) /** + * 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 <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified PAR6, reified RET> 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<T : Any> actual constructor(serviceClass: KClass<T>): KVServiceMgr<T> { +actual open class KVServiceManager<T : Any> actual constructor(serviceClass: KClass<T>) : KVServiceMgr<T> { protected val calls: MutableMap<String, Pair<String, HttpMethod>> = mutableMapOf() var counter: Int = 0 @@ -129,6 +129,23 @@ actual open class KVServiceManager<T : Any> actual constructor(serviceClass: KCl } /** + * 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 <reified PAR1, reified PAR2, reified PAR3, + reified PAR4, reified PAR5, reified PAR6, reified RET> 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 @@ -346,6 +346,52 @@ actual open class KVServiceManager<T : Any> 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 <reified PAR1, reified PAR2, reified PAR3, + reified PAR4, reified PAR5, reified PAR6, reified RET> 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<JsonRpcRequest>() + @Suppress("MagicNumber") + if (jsonRpcRequest.params.size == 6) { + val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) + val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) + val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) + val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) + val param5 = getParameter<PAR5>(jsonRpcRequest.params[4]) + val param6 = getParameter<PAR6>(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 * @param route a route @@ -435,8 +481,10 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val param1 = getParameter<Int?>(jsonRpcRequest.params[0]) val param2 = getParameter<Int?>(jsonRpcRequest.params[1]) val param3 = getParameter<List<RemoteFilter>?>(jsonRpcRequest.params[2]) + @Suppress("MagicNumber") val param4 = getParameter<List<RemoteSorter>?>(jsonRpcRequest.params[3]) + @Suppress("MagicNumber") val param5 = getParameter<String?>(jsonRpcRequest.params[4]) val service = ctx.require(serviceClass.java) |