Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config update #115

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/main/scala/configuration/ConfigurationUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,29 @@ object ConfigurationUtils {
// We'll error later if it isn't
IO.pure(Uri.unsafeFromString(url))
case head :: tail =>
val url = Uri.unsafeFromString(head)
val maybeResponse = toArr(client)(url, apiKey, "health")

maybeResponse.flatMap {
case Right(_) =>
IO.pure(url)
case Left(_) =>
Uri.fromString(head).toOption match {
case Some(url) =>
val maybeResponse = toArr(client)(url, apiKey, "health")

maybeResponse.flatMap {
case Right(_) =>
IO.pure(url)
case Left(_) =>
findCorrectUrl(client)(tail, apiKey)
}

case None =>
findCorrectUrl(client)(tail, apiKey)
}
}

private def getSonarrConfig(configReader: ConfigurationReader, client: HttpClient): IO[(Uri, String, Int, String, Int, Set[Int])] = {
val apiKey = configReader.getConfigOption(Keys.sonarrApiKey).getOrElse(throwError("Unable to find sonarr API key"))
val ioUrl = configReader.getConfigOption(Keys.sonarrBaseUrl).flatMap(Uri.fromString(_).toOption).map(IO.pure).getOrElse {
val defaults = possibleLocalHosts.map(_ + ":8989")
logger.warn(s"Unable to fetch sonarr baseUrl, trying common hosts with port 8989")
findCorrectUrl(client)(defaults, apiKey)
}
val configuredUrl = configReader.getConfigOption(Keys.sonarrBaseUrl)
val possibleUrls: Seq[String] = configuredUrl.map("http://" + _).toSeq ++ configuredUrl.toSeq ++ possibleLocalHosts.map(_ + ":8989")

for {
url <- ioUrl
url <- findCorrectUrl(client)(possibleUrls, apiKey)
rootFolder <- toArr(client)(url, apiKey, "rootFolder").map {
case Right(res) =>
logger.info("Successfully connected to Sonarr")
Expand Down Expand Up @@ -143,14 +145,11 @@ object ConfigurationUtils {

private def getRadarrConfig(configReader: ConfigurationReader, client: HttpClient): IO[(Uri, String, Int, String, Set[Int])] = {
val apiKey = configReader.getConfigOption(Keys.radarrApiKey).getOrElse(throwError("Unable to find radarr API key"))
val ioUrl = configReader.getConfigOption(Keys.radarrBaseUrl).flatMap(Uri.fromString(_).toOption).map(IO.pure).getOrElse {
val defaults = possibleLocalHosts.map(_ + ":7878")
logger.warn(s"Unable to fetch radarr baseUrl, trying common hosts with port 7878")
findCorrectUrl(client)(defaults, apiKey)
}
val configuredUrl = configReader.getConfigOption(Keys.radarrBaseUrl)
val possibleUrls: Seq[String] = configuredUrl.map("http://" + _).toSeq ++ configuredUrl.toSeq ++ possibleLocalHosts.map(_ + ":7878")

for {
url <- ioUrl
url <- findCorrectUrl(client)(possibleUrls, apiKey)
rootFolder <- toArr(client)(url, apiKey, "rootFolder").map {
case Right(res) =>
logger.info("Successfully connected to Radarr")
Expand Down
Loading