From c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 11 Feb 2019 23:02:40 +0100 Subject: Refactoring of server side components. --- .../kotlin/pl/treksoft/kvision/remote/CallAgent.kt | 72 ++++++++++++---------- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 41 +++++++----- 2 files changed, 63 insertions(+), 50 deletions(-) (limited to 'kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft') diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt index 4a086e2a..e9f0dd15 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt @@ -56,37 +56,41 @@ open class CallAgent { fun jsonRpcCall( url: String, data: List = listOf(), - method: RpcHttpMethod = RpcHttpMethod.POST + method: HttpMethod = HttpMethod.POST ): Promise { val jsonRpcRequest = JsonRpcRequest(counter++, url, data) - val jsonData = JSON.plain.stringify(jsonRpcRequest) + val jsonData = if (method == HttpMethod.GET) { + obj { id = jsonRpcRequest.id } + } else { + JSON.plain.stringify(jsonRpcRequest) + } return Promise { resolve, reject -> jQuery.ajax(url, obj { this.contentType = "application/json" this.data = jsonData this.method = method.name this.success = - { data: dynamic, _: Any, _: Any -> - when { - data.id != jsonRpcRequest.id -> reject(Exception("Invalid response ID")) - data.error != null -> reject(Exception(data.error.toString())) - data.result != null -> resolve(data.result) - else -> reject(Exception("Invalid response")) - } + { data: dynamic, _: Any, _: Any -> + when { + data.id != jsonRpcRequest.id -> reject(Exception("Invalid response ID")) + data.error != null -> reject(Exception(data.error.toString())) + data.result != null -> resolve(data.result) + else -> reject(Exception("Invalid response")) } + } this.error = - { xhr: JQueryXHR, _: String, errorText: String -> - val message = if (xhr.responseJSON != null && xhr.responseJSON != undefined) { - xhr.responseJSON.toString() - } else { - errorText - } - if (xhr.status.toInt() == HTTP_UNAUTHORIZED) { - reject(SecurityException(message)) - } else { - reject(Exception(message)) - } + { xhr: JQueryXHR, _: String, errorText: String -> + val message = if (xhr.responseJSON != null && xhr.responseJSON != undefined) { + xhr.responseJSON.toString() + } else { + errorText + } + if (xhr.status.toInt() == HTTP_UNAUTHORIZED) { + reject(SecurityException(message)) + } else { + reject(Exception(message)) } + } }) } } @@ -112,22 +116,22 @@ open class CallAgent { this.data = data this.method = method.name this.success = - { data: dynamic, _: Any, _: Any -> - resolve(data) - } + { data: dynamic, _: Any, _: Any -> + resolve(data) + } this.error = - { xhr: JQueryXHR, _: String, errorText: String -> - val message = if (xhr.responseJSON != null && xhr.responseJSON != undefined) { - xhr.responseJSON.toString() - } else { - errorText - } - if (xhr.status.toInt() == HTTP_UNAUTHORIZED) { - reject(SecurityException(message)) - } else { - reject(Exception(message)) - } + { xhr: JQueryXHR, _: String, errorText: String -> + val message = if (xhr.responseJSON != null && xhr.responseJSON != undefined) { + xhr.responseJSON.toString() + } else { + errorText + } + if (xhr.status.toInt() == HTTP_UNAUTHORIZED) { + reject(SecurityException(message)) + } else { + reject(Exception(message)) } + } this.beforeSend = beforeSend }) } diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index c13c4c18..4ca6a4a5 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -28,18 +28,18 @@ import kotlin.reflect.KClass */ actual open class KVServiceManager actual constructor(serviceClass: KClass) { - protected val calls: MutableMap> = mutableMapOf() + 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 route a route */ protected actual inline fun bind( noinline function: suspend T.() -> RET, - route: String?, method: RpcHttpMethod + method: HttpMethod, route: String? ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) @@ -48,13 +48,15 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl /** * 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 route a route */ protected actual inline fun bind( noinline function: suspend T.(PAR) -> RET, - route: String?, method: RpcHttpMethod + 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) } @@ -62,13 +64,15 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl /** * 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 route a route */ protected actual inline fun bind( noinline function: suspend T.(PAR1, PAR2) -> RET, - route: String?, method: RpcHttpMethod + 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) } @@ -76,13 +80,15 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl /** * 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 route a route */ protected actual inline fun bind( noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, - route: String?, method: RpcHttpMethod + 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) } @@ -90,13 +96,15 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl /** * 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 route a route */ protected actual inline fun bind( noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, - route: String?, method: RpcHttpMethod + 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) } @@ -104,15 +112,16 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl /** * 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 route a route */ protected actual inline fun bind( noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, - route: String?, - method: RpcHttpMethod + 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) } @@ -125,12 +134,12 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl function: T.(String?, String?) -> List ) { val routeDef = "route${this::class.simpleName}${counter++}" - calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", RpcHttpMethod.POST) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", HttpMethod.POST) } /** * Returns the map of defined paths. */ - fun getCalls(): Map> = calls + fun getCalls(): Map> = calls } -- cgit