def fetchAndParse()

in src/main/scala/com/gu/elasticsearchmonitor/NodeStats.scala [16:39]


  def fetchAndParse(host: String, httpClient: OkHttpClient, mapper: ObjectMapper, logger: LambdaLogger): Either[String, NodeStats] = {
    val nodeStatsRequest = Request.Builder()
      .url(s"$host/_nodes/stats")
      .build()

    val nodeStatsResponse = Try(httpClient.newCall(nodeStatsRequest).execute())

    val result = nodeStatsResponse match {
      case Success(response) if response.code == 200 =>
        val root = mapper.readTree(response.body.string)
        logger.log("Fetched the node stats", LogLevel.INFO)
        Right(NodeStats(
          nodes = root.get("nodes").iterator().asScala.toList.map(Node.parse)))
      case Success(response) =>
        Left(s"Unable to fetch the node stats. Http code ${response.code}")
      case Failure(e) =>
        logger.log(s"Unable to fetch node stats: $e", LogLevel.ERROR)
        Left(s"Unable to fetch node stats: ${e.getMessage}")
    }

    nodeStatsResponse.foreach(_.close)

    result
  }