diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt index b484d478c..8e9caf19c 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt @@ -53,34 +53,34 @@ class FeedFetcher(private val httpClient: HttpClient, private val feedParser: Fe transformUrl: Boolean, redirectCount: Int, ): FeedFetchResult { - return if (redirectCount < MAX_REDIRECTS_ALLOWED) { - try { - // We are mainly doing this check to avoid creating duplicates while refreshing feeds - // after the app update - val transformedUrl = transformUrl(url, transformUrl) - val response = httpClient.get(transformedUrl.toString()) - - when (response.status) { - HttpStatusCode.OK -> { - parseContent(response, transformedUrl.toString(), redirectCount) - } - HttpStatusCode.MultipleChoices, - HttpStatusCode.MovedPermanently, - HttpStatusCode.Found, - HttpStatusCode.SeeOther, - HttpStatusCode.TemporaryRedirect, - HttpStatusCode.PermanentRedirect -> { - handleHttpRedirect(response, transformedUrl.toString(), redirectCount) - } - else -> { - FeedFetchResult.HttpStatusError(statusCode = response.status) - } + if (redirectCount >= MAX_REDIRECTS_ALLOWED) { + return FeedFetchResult.TooManyRedirects + } + + return try { + // We are mainly doing this to avoid creating duplicates while refreshing feeds + // after the app update + val transformedUrl = transformUrl(url, transformUrl) + val response = httpClient.get(transformedUrl.toString()) + + when (response.status) { + HttpStatusCode.OK -> { + parseContent(response, transformedUrl.toString(), redirectCount) + } + HttpStatusCode.MultipleChoices, + HttpStatusCode.MovedPermanently, + HttpStatusCode.Found, + HttpStatusCode.SeeOther, + HttpStatusCode.TemporaryRedirect, + HttpStatusCode.PermanentRedirect -> { + handleHttpRedirect(response, transformedUrl.toString(), redirectCount) + } + else -> { + FeedFetchResult.HttpStatusError(statusCode = response.status) } - } catch (e: Exception) { - FeedFetchResult.Error(e) } - } else { - FeedFetchResult.TooManyRedirects + } catch (e: Exception) { + FeedFetchResult.Error(e) } } @@ -89,23 +89,23 @@ class FeedFetcher(private val httpClient: HttpClient, private val feedParser: Fe url: String, redirectCount: Int ): FeedFetchResult { - return if (response.contentType()?.withoutParameters() == ContentType.Text.Html) { - val feedUrl = fetchFeedLinkFromHtmlIfExists(response.bodyAsText(), url) - - if (feedUrl != url && !feedUrl.isNullOrBlank()) { - fetch(url = feedUrl, transformUrl = false, redirectCount = redirectCount + 1) - } else { - throw UnsupportedOperationException() - } - } else { + if (response.contentType()?.withoutParameters() != ContentType.Text.Html) { val content = response.bodyAsChannel() val responseCharset = response.contentType()?.parameter("charset") val charset = Charset.forName(responseCharset ?: Charsets.UTF8.name) val feedPayload = feedParser.parse(feedUrl = url, content = content, charset = charset) - FeedFetchResult.Success(feedPayload) + return FeedFetchResult.Success(feedPayload) } + + val feedUrl = fetchFeedLinkFromHtmlIfExists(response.bodyAsText(), url) + + if (feedUrl != url && !feedUrl.isNullOrBlank()) { + return fetch(url = feedUrl, transformUrl = false, redirectCount = redirectCount + 1) + } + + throw UnsupportedOperationException() } private suspend fun handleHttpRedirect( diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt index 852648864..ffda49fc6 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt @@ -17,7 +17,6 @@ package dev.sasikanth.rss.reader.core.network.parser import co.touchlab.kermit.Logger import dev.sasikanth.rss.reader.core.model.remote.FeedPayload -import dev.sasikanth.rss.reader.di.scopes.AppScope import dev.sasikanth.rss.reader.exceptions.XmlParsingError import dev.sasikanth.rss.reader.util.DispatchersProvider import io.ktor.http.URLBuilder @@ -35,7 +34,6 @@ import org.kobjects.ktxml.api.XmlPullParserException import org.kobjects.ktxml.mini.MiniXmlPullParser @Inject -@AppScope class FeedParser(private val dispatchersProvider: DispatchersProvider) { suspend fun parse(