aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-02-11 23:02:40 +0100
committerRobert Jaros <rjaros@finn.pl>2019-02-11 23:02:40 +0100
commitc0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a (patch)
treeec0ea1304f242de3ecc123e55979b03325ab41be /kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote
parent0d32732cfa8e0b359e1fd13523a6eb4356c97441 (diff)
downloadkvision-c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a.tar.gz
kvision-c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a.tar.bz2
kvision-c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a.zip
Refactoring of server side components.
Diffstat (limited to 'kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote')
-rw-r--r--kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt72
-rw-r--r--kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt41
2 files changed, 63 insertions, 50 deletions
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<String?> = listOf(),
- method: RpcHttpMethod = RpcHttpMethod.POST
+ method: HttpMethod = HttpMethod.POST
): Promise<String> {
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<T : Any> actual constructor(serviceClass: KClass<T>) {
- protected val calls: MutableMap<String, Pair<String, RpcHttpMethod>> = mutableMapOf()
+ protected val calls: MutableMap<String, Pair<String, HttpMethod>> = 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 <reified RET> 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<T : Any> 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 <reified PAR, reified RET> 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<T : Any> 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 <reified PAR1, reified PAR2, reified RET> 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<T : Any> 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 <reified PAR1, reified PAR2, reified PAR3, reified RET> 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<T : Any> 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 <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> 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<T : Any> 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 <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified RET> 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<T : Any> actual constructor(serviceClass: KCl
function: T.(String?, String?) -> List<RemoteSelectOption>
) {
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<String, Pair<String, RpcHttpMethod>> = calls
+ fun getCalls(): Map<String, Pair<String, HttpMethod>> = calls
}