From 161264957dc1b41cd6716ee7777139c5e29589f5 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Tue, 18 Dec 2018 18:54:27 +0100 Subject: Refactor modules. --- build.gradle | 4 +- kvision-modules/kvision-common-remote/build.gradle | 10 + .../treksoft/kvision/remote/JoobyServiceManager.kt | 107 ++++++ .../kotlin/pl/treksoft/kvision/remote/JsonRpc.kt | 37 +++ .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 37 +++ .../treksoft/kvision/remote/RemoteSelectOption.kt | 36 ++ .../pl/treksoft/kvision/remote/ServiceManager.kt | 45 +++ .../kvision/remote/SpringServiceManager.kt | 102 ++++++ kvision-modules/kvision-common-types/build.gradle | 10 + .../main/kotlin/pl/treksoft/kvision/types/Date.kt | 28 ++ .../main/kotlin/pl/treksoft/kvision/types/KFile.kt | 34 ++ kvision-modules/kvision-common/build.gradle | 10 - .../treksoft/kvision/remote/JoobyServiceManager.kt | 107 ------ .../kotlin/pl/treksoft/kvision/remote/JsonRpc.kt | 37 --- .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 37 --- .../pl/treksoft/kvision/remote/ServiceManager.kt | 45 --- .../kvision/remote/SpringServiceManager.kt | 102 ------ .../main/kotlin/pl/treksoft/kvision/types/Date.kt | 28 -- .../main/kotlin/pl/treksoft/kvision/types/KFile.kt | 34 -- kvision-modules/kvision-remote/build.gradle | 5 + .../kvision-remote/package.json.d/project.info | 3 + .../kotlin/pl/treksoft/kvision/remote/CallAgent.kt | 135 ++++++++ .../pl/treksoft/kvision/remote/JoobyRemoteAgent.kt | 370 +++++++++++++++++++++ .../treksoft/kvision/remote/JoobyServiceManager.kt | 129 +++++++ .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 123 +++++++ .../pl/treksoft/kvision/remote/RemoteAgent.kt | 208 ++++++++++++ .../kotlin/pl/treksoft/kvision/remote/Security.kt | 116 +++++++ .../treksoft/kvision/remote/SpringRemoteAgent.kt | 370 +++++++++++++++++++++ .../kvision/remote/SpringServiceManager.kt | 124 +++++++ .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 99 ++++++ .../kvision/form/upload/UploadInputSpec.kt | 57 ++++ .../pl/treksoft/kvision/form/upload/UploadSpec.kt | 56 ++++ kvision-modules/kvision-server-jooby/build.gradle | 6 +- .../kvision-server-spring-boot/build.gradle | 6 +- settings.gradle | 4 +- .../kotlin/pl/treksoft/kvision/remote/CallAgent.kt | 135 -------- .../pl/treksoft/kvision/remote/JoobyRemoteAgent.kt | 370 --------------------- .../treksoft/kvision/remote/JoobyServiceManager.kt | 129 ------- .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 123 ------- .../pl/treksoft/kvision/remote/RemoteAgent.kt | 208 ------------ .../kotlin/pl/treksoft/kvision/remote/Security.kt | 116 ------- .../treksoft/kvision/remote/SpringRemoteAgent.kt | 370 --------------------- .../kvision/remote/SpringServiceManager.kt | 124 ------- 43 files changed, 2255 insertions(+), 1981 deletions(-) create mode 100644 kvision-modules/kvision-common-remote/build.gradle create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt create mode 100644 kvision-modules/kvision-common-types/build.gradle create mode 100644 kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt create mode 100644 kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt delete mode 100644 kvision-modules/kvision-common/build.gradle delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/Date.kt delete mode 100644 kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt create mode 100644 kvision-modules/kvision-remote/build.gradle create mode 100644 kvision-modules/kvision-remote/package.json.d/project.info create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt create mode 100644 kvision-modules/kvision-remote/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt create mode 100644 kvision-modules/kvision-remote/src/test/kotlin/test/pl/treksoft/kvision/form/upload/UploadInputSpec.kt create mode 100644 kvision-modules/kvision-remote/src/test/kotlin/test/pl/treksoft/kvision/form/upload/UploadSpec.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/Security.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt diff --git a/build.gradle b/build.gradle index e1728313..20ef7bb6 100644 --- a/build.gradle +++ b/build.gradle @@ -99,7 +99,7 @@ apply plugin: 'kotlinx-serialization' dependencies { if (!project.gradle.startParameter.taskNames.contains("dokka")) { - expectedBy project('kvision-modules:kvision-common') + expectedBy project('kvision-modules:kvision-common-types') compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion" compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion" compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion" @@ -160,7 +160,9 @@ dokka { 'kvision-modules/kvision-upload/src/main/kotlin', 'kvision-modules/kvision-handlebars/src/main/kotlin', 'kvision-modules/kvision-i18n/src/main/kotlin', + 'kvision-modules/kvision-remote/src/main/kotlin', 'kvision-modules/kvision-common/src/main/kotlin', + 'kvision-modules/kvision-common-types/src/main/kotlin', 'kvision-modules/kvision-server-jooby/src/main/kotlin', 'kvision-modules/kvision-server-spring-boot/src/main/kotlin') classpath = [new File("dokka/kvision-dokka-helper.jar")] diff --git a/kvision-modules/kvision-common-remote/build.gradle b/kvision-modules/kvision-common-remote/build.gradle new file mode 100644 index 00000000..af3703e6 --- /dev/null +++ b/kvision-modules/kvision-common-remote/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'kotlin-platform-common' +apply plugin: 'kotlinx-serialization' + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion" + compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion" + compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion" + testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion" + testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion" +} diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt new file mode 100644 index 00000000..cb1b44d0 --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt @@ -0,0 +1,107 @@ +/* + * 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 + +/** + * Multiplatform service manager for Jooby. + */ +expect open class JoobyServiceManager(service: T) : ServiceManager { + + /** + * 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 inline fun bind( + noinline function: suspend T.(Request?) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR, Request?) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * Applies all defined routes to the given server. + * @param k a server + */ + fun applyRoutes(k: KVServer) + +} diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt new file mode 100644 index 00000000..7953ea01 --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt @@ -0,0 +1,37 @@ +/* + * 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.serialization.Serializable + +@Serializable +data class JsonRpcRequest(val id: Int, val method: String, val params: List, val jsonrpc: String = "2.0") { + constructor() : this(0, "", listOf()) +} + +@Serializable +data class JsonRpcResponse( + val id: Int? = null, + val result: String? = null, + val error: String? = null, + val jsonrpc: String = "2.0" +) diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt new file mode 100644 index 00000000..cf822e66 --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt @@ -0,0 +1,37 @@ +/* + * 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 + +/** + * A server. + */ +expect open class KVServer + +/** + * A server request. + */ +expect interface Request + +/** + * A user profile. + */ +expect class Profile diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt new file mode 100644 index 00000000..24e68f63 --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt @@ -0,0 +1,36 @@ +/* + * 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.serialization.Serializable + +@Serializable +data class RemoteSelectOption( + val value: String? = null, + val text: String? = null, + val className: String? = null, + val subtext: String? = null, + val icon: String? = null, + val content: String? = null, + val disabled: Boolean = false, + val divider: Boolean = false +) diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt new file mode 100644 index 00000000..6f696475 --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -0,0 +1,45 @@ +/* + * 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 + +enum class RpcHttpMethod { + POST, + PUT, + DELETE, + OPTIONS +} + +enum class HttpMethod { + GET, + POST, + PUT, + DELETE, + OPTIONS +} + +interface ServiceManager { + /** + * Returns the map of defined paths. + */ + fun getCalls(): Map> = mapOf() + +} diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt new file mode 100644 index 00000000..90926656 --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -0,0 +1,102 @@ +/* + * 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 kotlin.reflect.KClass + +/** + * Multiplatform service manager for Spring Boot. + */ +expect open class SpringServiceManager(serviceClass: KClass) : ServiceManager { + + /** + * 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 inline fun bind( + noinline function: suspend T.() -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) + + /** + * 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 inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, + route: String? = null, + method: RpcHttpMethod = RpcHttpMethod.POST + ) +} diff --git a/kvision-modules/kvision-common-types/build.gradle b/kvision-modules/kvision-common-types/build.gradle new file mode 100644 index 00000000..af3703e6 --- /dev/null +++ b/kvision-modules/kvision-common-types/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'kotlin-platform-common' +apply plugin: 'kotlinx-serialization' + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion" + compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion" + compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion" + testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion" + testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion" +} diff --git a/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt new file mode 100644 index 00000000..86a72b33 --- /dev/null +++ b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -0,0 +1,28 @@ +/* + * 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.types + +expect class Date + +expect fun String.toDateF(format: String = "YYYY-MM-DD HH:mm:ss"): Date + +expect fun Date.toStringF(format: String = "YYYY-MM-DD HH:mm:ss"): String diff --git a/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt new file mode 100644 index 00000000..ce4adca4 --- /dev/null +++ b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt @@ -0,0 +1,34 @@ +/* + * 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.types + +import kotlinx.serialization.Serializable + +/** + * A serializable class for a multiplatform File type. + */ +@Serializable +data class KFile( + val name: String, + val size: Int, + val content: String? = null +) diff --git a/kvision-modules/kvision-common/build.gradle b/kvision-modules/kvision-common/build.gradle deleted file mode 100644 index af3703e6..00000000 --- a/kvision-modules/kvision-common/build.gradle +++ /dev/null @@ -1,10 +0,0 @@ -apply plugin: 'kotlin-platform-common' -apply plugin: 'kotlinx-serialization' - -dependencies { - compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion" - compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion" - compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion" - testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion" - testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion" -} diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt deleted file mode 100644 index cb1b44d0..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ /dev/null @@ -1,107 +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 - -/** - * Multiplatform service manager for Jooby. - */ -expect open class JoobyServiceManager(service: T) : ServiceManager { - - /** - * 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 inline fun bind( - noinline function: suspend T.(Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Applies all defined routes to the given server. - * @param k a server - */ - fun applyRoutes(k: KVServer) - -} diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt deleted file mode 100644 index 7953ea01..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt +++ /dev/null @@ -1,37 +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.serialization.Serializable - -@Serializable -data class JsonRpcRequest(val id: Int, val method: String, val params: List, val jsonrpc: String = "2.0") { - constructor() : this(0, "", listOf()) -} - -@Serializable -data class JsonRpcResponse( - val id: Int? = null, - val result: String? = null, - val error: String? = null, - val jsonrpc: String = "2.0" -) diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt deleted file mode 100644 index cf822e66..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ /dev/null @@ -1,37 +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 - -/** - * A server. - */ -expect open class KVServer - -/** - * A server request. - */ -expect interface Request - -/** - * A user profile. - */ -expect class Profile diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt deleted file mode 100644 index 6f696475..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt +++ /dev/null @@ -1,45 +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 - -enum class RpcHttpMethod { - POST, - PUT, - DELETE, - OPTIONS -} - -enum class HttpMethod { - GET, - POST, - PUT, - DELETE, - OPTIONS -} - -interface ServiceManager { - /** - * Returns the map of defined paths. - */ - fun getCalls(): Map> = mapOf() - -} diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt deleted file mode 100644 index 90926656..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ /dev/null @@ -1,102 +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 kotlin.reflect.KClass - -/** - * Multiplatform service manager for Spring Boot. - */ -expect open class SpringServiceManager(serviceClass: KClass) : ServiceManager { - - /** - * 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 inline fun bind( - noinline function: suspend T.() -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * 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 inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) -} diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/Date.kt deleted file mode 100644 index 86a72b33..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ /dev/null @@ -1,28 +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.types - -expect class Date - -expect fun String.toDateF(format: String = "YYYY-MM-DD HH:mm:ss"): Date - -expect fun Date.toStringF(format: String = "YYYY-MM-DD HH:mm:ss"): String diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt deleted file mode 100644 index ce4adca4..00000000 --- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt +++ /dev/null @@ -1,34 +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.types - -import kotlinx.serialization.Serializable - -/** - * A serializable class for a multiplatform File type. - */ -@Serializable -data class KFile( - val name: String, - val size: Int, - val content: String? = null -) diff --git a/kvision-modules/kvision-remote/build.gradle b/kvision-modules/kvision-remote/build.gradle new file mode 100644 index 00000000..b99fb199 --- /dev/null +++ b/kvision-modules/kvision-remote/build.gradle @@ -0,0 +1,5 @@ +apply from: "../shared.gradle" + +dependencies { + expectedBy project(":kvision-modules:kvision-common-remote") +} diff --git a/kvision-modules/kvision-remote/package.json.d/project.info b/kvision-modules/kvision-remote/package.json.d/project.info new file mode 100644 index 00000000..cfee4b2f --- /dev/null +++ b/kvision-modules/kvision-remote/package.json.d/project.info @@ -0,0 +1,3 @@ +{ + "description": "KVision Remote module" +} 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 new file mode 100644 index 00000000..4a086e2a --- /dev/null +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt @@ -0,0 +1,135 @@ +/* + * 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.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.stringify +import pl.treksoft.jquery.JQueryAjaxSettings +import pl.treksoft.jquery.JQueryXHR +import pl.treksoft.jquery.jQuery +import pl.treksoft.kvision.utils.JSON +import pl.treksoft.kvision.utils.obj +import kotlin.js.Promise +import kotlin.js.undefined +import kotlin.js.JSON as NativeJSON + +/** + * HTTP status unauthorized (401). + */ +const val HTTP_UNAUTHORIZED = 401 + +/** + * An agent responsible for remote calls. + */ +open class CallAgent { + + private var counter = 1 + + /** + * Makes an JSON-RPC call to the remote server. + * @param url an URL address + * @param method a HTTP method + * @param data data to be sent + * @return a promise of the result + */ + @UseExperimental(ImplicitReflectionSerializer::class) + @Suppress("UnsafeCastFromDynamic") + fun jsonRpcCall( + url: String, + data: List = listOf(), + method: RpcHttpMethod = RpcHttpMethod.POST + ): Promise { + val jsonRpcRequest = JsonRpcRequest(counter++, url, data) + val jsonData = 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")) + } + } + 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)) + } + } + }) + } + } + + /** + * Makes a remote call to the remote server. + * @param url an URL address + * @param method a HTTP method + * @param data data to be sent + * @return a promise of the result + */ + @Suppress("UnsafeCastFromDynamic") + fun remoteCall( + url: String, + data: dynamic = null, + method: HttpMethod = HttpMethod.GET, + contentType: String = "application/json", + beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null + ): Promise { + return Promise { resolve, reject -> + jQuery.ajax(url, obj { + this.contentType = contentType + this.data = data + this.method = method.name + this.success = + { 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)) + } + } + this.beforeSend = beforeSend + }) + } + } +} diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt new file mode 100644 index 00000000..318f77ea --- /dev/null +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt @@ -0,0 +1,370 @@ +/* + * 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.asDeferred +import kotlinx.serialization.ImplicitReflectionSerializer +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") +@UseExperimental(ImplicitReflectionSerializer::class) +open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) : RemoteAgent { + + val callAgent = CallAgent() + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call(noinline function: suspend T.(Request?) -> RET): RET { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(Request?) -> List + ): List { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(PAR, Request?) -> RET, p: PAR + ): RET { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(PAR, Request?) -> List, p: PAR + ): List { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, p1: PAR1, p2: PAR2 + ): RET { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, Request?) -> List, p1: PAR1, p2: PAR2 + ): List { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, p1: PAR1, p2: PAR2, p3: PAR3 + ): RET { + 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().await() + } + + /** + * Executes defined call to a remote web service. + */ + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> List, p1: PAR1, p2: PAR2, p3: PAR3 + ): List { + 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) + } + }