def getPodcastInfo()

in src/main/scala/com/gu/contentapi/services/PodcastLookup.scala [64:104]


  def getPodcastInfo(filePath: String): Option[PodcastInfo] = {
    if (cache contains filePath) {
      cacheHits += 1
      cache.get(filePath)
    } else {
      cacheMisses += 1

      //Try up to 3 times because we occasionally get capi timeouts
      val result: Try[Option[PodcastLookup.PodcastInfo]] = retry(3) {
        Await.result(
          makeCapiQuery(filePath) map {
            case SuccessfulQuery(sr) =>
              for {
                result <- sr.results.headOption
                podcastName <- result.tags.find(_.podcast.isDefined).map(_.webUrl)
              } yield {
                val podcastInfo = PodcastInfo(result.webUrl, podcastName)
                cache.put(filePath, podcastInfo)
                podcastInfo
              }

            case FailedQuery(err) =>
              logger.error(s"Failed to get podcast info from capi for file '$filePath': $err")
              None
            // Time taken to await the future should be at least the HTTP read timeout configured for requests to CAPI.
            // If everything else takes > 10s + (PodcastLookup.httpReadTimeout - actual read time)s,
            // we most likely have issues and should fail the future.
          }, httpReadTimeout + 10.seconds)
      }

      result match {
        case Success(info) => info
        case Failure(NonFatal(e)) =>
          logger.error(s"CAPI request repeatedly failed: ${e.getMessage}", e)
          None
        case Failure(err) => //if the exception was fatal, throw it and let the lambda runtime handle it. This will show up as a failure in the metrics
          logger.error(s"A fatal error occurred trying to access CAPI: ${err.getMessage}", err)
          throw err
      }
    }
  }