From 4b999e9fd65aa4bf39059af1663dcfa068791f33 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Wed, 16 Sep 2020 13:57:45 +0200 Subject: [PATCH] Add jsonfeed parsing in LocalRSSDataSource --- .../api/localfeed/LocalRSSDataSourceTest.kt | 14 +++++++++++ .../api/localfeed/LocalRSSDataSource.kt | 23 +++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt b/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt index 60c76b80..6960c9f5 100644 --- a/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt +++ b/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt @@ -81,6 +81,20 @@ class LocalRSSDataSourceTest { assertEquals(request.headers[LibUtils.LAST_MODIFIED_HEADER], "Last-Modified") } + @Test + fun jsonFeedTest() { + val stream = context.resources.assets.open("localfeed/json/json_feed.json") + + mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) + .addHeader(LibUtils.CONTENT_TYPE_HEADER, "application/feed+json") + .setBody(Buffer().readFrom(stream))) + + val pair = localRSSDataSource.queryRSSResource(url.toString(), null, true)!! + + assertEquals(pair.first.name, "News from Flying Meat") + assertEquals(pair.second.size, 10) + } + @Test fun response304Test() { mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)) diff --git a/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt b/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt index 817bc126..3541ff1d 100644 --- a/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt +++ b/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt @@ -2,15 +2,20 @@ package com.readrops.api.localfeed import android.accounts.NetworkErrorException import androidx.annotation.WorkerThread +import com.readrops.api.localfeed.json.JSONFeedAdapter +import com.readrops.api.localfeed.json.JSONItemsAdapter import com.readrops.api.utils.LibUtils import com.readrops.api.utils.ParseException import com.readrops.api.utils.UnknownFormatException import com.readrops.db.entities.Feed import com.readrops.db.entities.Item +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import okhttp3.Headers import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response +import okio.Buffer import java.io.ByteArrayInputStream import java.io.IOException import java.io.InputStream @@ -89,7 +94,12 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) { adapter.fromXml(stream) } else { - Feed() + val adapter = Moshi.Builder() + .add(JSONFeedAdapter()) + .build() + .adapter(Feed::class.java) + + adapter.fromJson(Buffer().readFrom(stream))!! } feed.etag = response.header(LibUtils.ETAG_HEADER) @@ -98,13 +108,18 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) { return feed } - private fun parseItems(inputStream: InputStream, type: LocalRSSHelper.RSSType): List { + private fun parseItems(stream: InputStream, type: LocalRSSHelper.RSSType): List { return if (type != LocalRSSHelper.RSSType.JSONFEED) { val adapter = XmlAdapter.xmlItemsAdapterFactory(type) - adapter.fromXml(inputStream) + adapter.fromXml(stream) } else { - listOf() + val adapter = Moshi.Builder() + .add(Types.newParameterizedType(MutableList::class.java, Item::class.java), JSONItemsAdapter()) + .build() + .adapter>(Types.newParameterizedType(MutableList::class.java, Item::class.java)) + + adapter.fromJson(Buffer().readFrom(stream))!! } } } \ No newline at end of file