def fetchTopFrontAsBreakingNews()

in fakebreakingnewslambda/src/main/scala/fakebreakingnews/TopUkRegularStory.scala [38:81]


  def fetchTopFrontAsBreakingNews(): Future[BreakingNewsPayload] = {

    val promise = Promise[BreakingNewsPayload]()
    val call = okHttpClient.newCall(new Request.Builder()
      .url(ukRegularStoriesUrl)
      .build())
    call.enqueue(new Callback {
      override def onFailure(call: Call, e: IOException): Unit = promise.failure(new Exception(s"Error getting $ukRegularStoriesUrl", e))

      override def onResponse(call: Call, response: Response): Unit = Try {
        val maybebreakingNewsNotification = for {
          body <- Option(response.body)
          bodyString = body.string()
          bodyJson = Json.parse(bodyString)
          firstUkRegularStory = bodyJson.as[UkRegularStories].cards(0)
          breakingNewsPayload = BreakingNewsPayload(
            id = UUID.randomUUID(),
            title = Some(firstUkRegularStory.title),
            message = Some(firstUkRegularStory.title),
            thumbnailUrl = None,
            sender = "newstester",
            link = GuardianLinkDetails(
              contentApiId = firstUkRegularStory.item.id,
              shortUrl = firstUkRegularStory.item.links.shortUrl,
              firstUkRegularStory.title,
              thumbnail = None,
              git = client.models.GITContent,
              blockId = None
            ),
            imageUrl = None,
            importance = client.models.Importance.Major,
            topic = List(client.models.Topic.BreakingNewsInternalTest),
            debug = false,
            dryRun = Some(true)
          )
        } yield breakingNewsPayload
        maybebreakingNewsNotification.get
      } match {
        case Success(value) => promise.success(value)
        case Failure(e) => promise.failure(new Exception(s"Error getting $ukRegularStoriesUrl", e))
      }
    })
    promise.future
  }