Skip to content

Commit

Permalink
Remove nested conditionals from FeedFetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Jul 30, 2024
1 parent 42a3b45 commit b8b94b8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit b8b94b8

Please sign in to comment.