webapi: add getConfig request
authorDa Risk <da_risk@geekorum.com>
Mon, 02 Sep 2019 20:09:19 -0700
changeset 431 a437182c6080
parent 430 401b8736155b
child 432 26eed0175da9
webapi: add getConfig request
webapi/src/main/kotlin/TinyRssApi.kt
webapi/src/main/kotlin/model/Config.kt
webapi/src/test/kotlin/model/ConfigJsonSerializationTest.kt
--- a/webapi/src/main/kotlin/TinyRssApi.kt	Sat Aug 31 19:04:02 2019 -0700
+++ b/webapi/src/main/kotlin/TinyRssApi.kt	Mon Sep 02 20:09:19 2019 -0700
@@ -27,6 +27,8 @@
 import com.geekorum.ttrss.webapi.model.GetApiLevelResponsePayload
 import com.geekorum.ttrss.webapi.model.GetArticlesRequestPayload
 import com.geekorum.ttrss.webapi.model.GetCategoriesRequestPayload
+import com.geekorum.ttrss.webapi.model.GetConfigRequestPayload
+import com.geekorum.ttrss.webapi.model.GetConfigResponsePayload
 import com.geekorum.ttrss.webapi.model.GetFeedsRequestPayload
 import com.geekorum.ttrss.webapi.model.Headline
 import com.geekorum.ttrss.webapi.model.ListResponsePayload
@@ -59,6 +61,9 @@
     suspend fun getVersion(@Body getVersionRequestPayload: GetVersionRequestPayload): GetVersionResponsePayload
 
     @POST("api/")
+    suspend fun getConfig(@Body getConfigRequestPayload: GetConfigRequestPayload): GetConfigResponsePayload
+
+    @POST("api/")
     suspend fun getApiLevel(@Body getApiLevelRequestPayload: GetApiLevelRequestPayload): GetApiLevelResponsePayload
 
     @POST("api/")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/webapi/src/main/kotlin/model/Config.kt	Mon Sep 02 20:09:19 2019 -0700
@@ -0,0 +1,100 @@
+package com.geekorum.ttrss.webapi.model
+
+import androidx.annotation.Keep
+import kotlinx.serialization.CompositeDecoder
+import kotlinx.serialization.Decoder
+import kotlinx.serialization.Encoder
+import kotlinx.serialization.KSerializer
+import kotlinx.serialization.SerialName
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.Serializer
+import kotlinx.serialization.internal.IntSerializer
+import kotlinx.serialization.internal.makeNullable
+
+/**
+ * Request payload to get the configuration of the TtRss server.
+ */
+@Keep
+@Serializable
+class GetConfigRequestPayload : LoggedRequestPayload() {
+
+    @SerialName("op")
+    override val operation = "getConfig"
+}
+
+data class GetConfigResponsePayload(
+    @SerialName("seq")
+    override val sequence: Int? = null,
+    override val status: Int = 0,
+    override val content: Content
+) : ResponsePayload<GetConfigResponsePayload.Content>() {
+
+    val iconsDir: String?
+        get() = content.iconsDir
+
+    val iconsUrl: String?
+        get() = content.iconsUrl
+
+    val numFeeds: Int?
+        get() = content.numFeeds
+
+    val daemonIsRunning: Boolean?
+        get() = content.daemonIsRunning
+
+    @Serializable
+    data class Content(
+        @SerialName("daemon_is_running")
+        val daemonIsRunning: Boolean? = null,
+        @SerialName("icons_dir")
+        val iconsDir: String? = null,
+        @SerialName("icons_url")
+        val iconsUrl: String? = null,
+        @SerialName("num_feeds")
+        val numFeeds: Int? = null,
+        override var error: Error? = null
+
+    ) : BaseContent()
+
+    companion object {
+        fun serializer(): KSerializer<GetConfigResponsePayload> {
+            return GetConfigResponsePayload.GetConfigResponsePayloadSerializer()
+        }
+    }
+
+
+    @Serializer(GetConfigResponsePayload::class)
+    class GetConfigResponsePayloadSerializer : KSerializer<GetConfigResponsePayload> {
+        override fun serialize(encoder: Encoder, obj: GetConfigResponsePayload) {
+            TODO("not implemented")
+        }
+
+        override fun deserialize(decoder: Decoder): GetConfigResponsePayload {
+            val contentDecoder = decoder.beginStructure(descriptor)
+            lateinit var content: Content
+            var seq: Int? = null
+            var status = 0
+            loop@ while (true) {
+                when (val i = contentDecoder.decodeElementIndex(descriptor)) {
+                    CompositeDecoder.READ_DONE -> break@loop
+                    0 -> seq = contentDecoder.decodeNullableSerializableElement(descriptor, i,
+                        makeNullable(
+                            IntSerializer))
+                    1 -> status = contentDecoder.decodeIntElement(descriptor, i)
+                    2 -> {
+                        val contentSerializer = Content.serializer()
+                        content =
+                            contentDecoder.decodeSerializableElement(contentSerializer.descriptor,
+                                i,
+                                contentSerializer)
+                    }
+                }
+            }
+            contentDecoder.endStructure(descriptor)
+            return GetConfigResponsePayload(
+                content = content,
+                sequence = seq,
+                status = status
+            )
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/webapi/src/test/kotlin/model/ConfigJsonSerializationTest.kt	Mon Sep 02 20:09:19 2019 -0700
@@ -0,0 +1,92 @@
+/*
+ * Geekttrss is a RSS feed reader application on the Android Platform.
+ *
+ * Copyright (C) 2017-2019 by Frederic-Charles Barthelery.
+ *
+ * This file is part of Geekttrss.
+ *
+ * Geekttrss is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Geekttrss is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.geekorum.ttrss.webapi.model
+
+import com.google.common.truth.Truth.assertThat
+import kotlinx.serialization.UnstableDefault
+import kotlinx.serialization.json.Json
+import kotlin.test.Test
+
+@UseExperimental(UnstableDefault::class)
+class GetConfigJsonSerializationTest {
+
+    @Test
+    fun testThatGetConfigRequestPayloadDoCorrectJson() {
+        val payload = GetConfigRequestPayload().apply {
+            sessionId = "SESSION_ID"
+        }
+        val serializer = getSerializer<GetConfigRequestPayload>()
+        val result = Json.stringify(serializer, payload)
+        assertThat(result).isEqualTo("""
+            {"sid":"SESSION_ID","op":"getConfig"}
+        """.trimIndent())
+    }
+
+    @Test
+    fun testThatGetConfigResponsePayloadDoLoadCorrectly() {
+        val jsonString = """
+            {
+                "content" : {
+                    "daemon_is_running" : true,
+                    "icons_dir" : "feed-icons",
+                    "icons_url" : "feed-icons",
+                    "num_feeds" : 65
+                },
+                "seq" : 0,
+                "status" : 0
+            }
+        """.trimIndent()
+        val serializer = getSerializer<GetConfigResponsePayload>()
+        val result = Json.parse(serializer, jsonString)
+        val expected = GetConfigResponsePayload(
+            sequence = 0,
+            status = 0,
+            content = GetConfigResponsePayload.Content(daemonIsRunning = true,
+                iconsDir = "feed-icons",
+                iconsUrl = "feed-icons",
+                numFeeds = 65)
+        )
+        assertThat(result.sequence).isEqualTo(expected.sequence)
+        assertThat(result.status).isEqualTo(expected.status)
+        assertThat(result.content).isEqualTo(expected.content)
+    }
+
+    @Test
+    fun testThatGetConfigResponsePayloadWithErrorDoLoadCorrectly() {
+        val jsonString = """
+            {
+              "seq": 2,
+              "status": 1,
+              "content": {"error":"NOT_LOGGED_IN"}
+            }
+        """.trimIndent()
+        val serializer = getSerializer<ListResponsePayload<Headline>>()
+        val result = Json.parse(serializer, jsonString)
+        val expected = GetConfigResponsePayload(
+            sequence = 2,
+            status = 1,
+            content = GetConfigResponsePayload.Content(error = Error.NOT_LOGGED_IN)
+        )
+        assertThat(result.sequence).isEqualTo(expected.sequence)
+        assertThat(result.status).isEqualTo(expected.status)
+        assertThat(result.content.error).isEqualTo(expected.content.error)
+    }
+}