<commit_before>package com.socrata.tileserver
package util

import java.io.InputStream
import java.nio.charset.StandardCharsets.UTF_8
import javax.servlet.http.HttpServletResponse.{SC_OK => ScOk}
import scala.util.{Failure, Try, Success}

import com.rojoma.json.v3.interpolation._
import com.rojoma.json.v3.io.JValueEventIterator
import com.rojoma.simplearm.v2.ResourceScope
import com.socrata.http.client.{HttpClient, JsonHttpRequest, RequestBuilder, Response}
import org.apache.commons.io.IOUtils
import org.slf4j.{Logger, LoggerFactory}

import CartoRenderer._
import exceptions.FailedRenderException

case class CartoRenderer(http: HttpClient, baseUrl: RequestBuilder) {
  def mapnikXml(cartoCss: String)(implicit rs: ResourceScope): Try[String] = {
    val content = JValueEventIterator(json"{ style: ${cartoCss} }") // scalastyle:ignore
    val req = new JsonHttpRequest(baseUrl.addPath("style"), content)

    Try(http.execute(req, rs)).map { resp: Response =>
      IOUtils.toString(resp.inputStream(), UTF_8)
    }
  }

  def renderPng(pbf: String,
                zoom: Int,
                cartoCss: String)(implicit rs: ResourceScope): Try[Array[Byte]]  = {
    val contentObj = json"{ bpbf: ${pbf}, zoom: ${zoom}, style: ${cartoCss} }"
    val content = JValueEventIterator(contentObj) // scalastyle:ignore
    val req = new JsonHttpRequest(baseUrl.addPath("render").method("POST"), content)

    logger.debug("content: {}", contentObj)

    Try(http.execute(req, rs)).map { resp: Response =>
      IOUtils.toByteArray(resp.inputStream())
    }
  }
}

object CartoRenderer {
  private val logger: Logger = LoggerFactory.getLogger(getClass)

  private[util] def handleResponse(response: Try[Response]): Try[InputStream] = {
    response.flatMap { resp =>
      if (resp.resultCode == ScOk) {
        Success(resp.inputStream())
      } else {
        Failure(
          FailedRenderException(IOUtils.toString(resp.inputStream(), UTF_8)))
      }
    }
  }
}<commit_msg>Clean up json requests<commit_after>package com.socrata.tileserver
package util

import java.io.InputStream
import java.nio.charset.StandardCharsets.UTF_8
import javax.servlet.http.HttpServletResponse.{SC_OK => ScOk}
import scala.util.{Failure, Try, Success}

import com.rojoma.json.v3.interpolation._
import com.rojoma.simplearm.v2.ResourceScope
import com.socrata.http.client.{HttpClient, RequestBuilder, Response}
import org.apache.commons.io.IOUtils
import org.slf4j.{Logger, LoggerFactory}

import CartoRenderer._
import exceptions.FailedRenderException

case class CartoRenderer(http: HttpClient, baseUrl: RequestBuilder) {
  def mapnikXml(cartoCss: String)(implicit rs: ResourceScope): Try[String] = {
    val content = json"{ style: ${cartoCss} }" // scalastyle:ignore
    val req = baseUrl.addPath("style").jsonBody(content)

    Try(http.execute(req, rs)).map { resp: Response =>
      IOUtils.toString(resp.inputStream(), UTF_8)
    }
  }

  def renderPng(pbf: String,
                zoom: Int,
                cartoCss: String)(implicit rs: ResourceScope): Try[Array[Byte]]  = {
    val content = json"{ bpbf: ${pbf}, zoom: ${zoom}, style: ${cartoCss} }" // scalastyle:ignore
    val req = baseUrl.addPath("render").jsonBody(content)

    logger.debug("content: {}", content)

    Try(http.execute(req, rs)).map { resp: Response =>
      IOUtils.toByteArray(resp.inputStream())
    }
  }
}

object CartoRenderer {
  private val logger: Logger = LoggerFactory.getLogger(getClass)

  private[util] def handleResponse(response: Try[Response]): Try[InputStream] = {
    response.flatMap { resp =>
      if (resp.resultCode == ScOk) {
        Success(resp.inputStream())
      } else {
        Failure(
          FailedRenderException(IOUtils.toString(resp.inputStream(), UTF_8)))
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.12984956452889945, 'fraction_numerical': 0.003430984428609132, 'mean_word_length': 4.060080106809079, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1791225', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1207, 'n_words': 235}