<commit_before>package rest

import dispatch._
import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext}

object AsyncRest {
  private implicit val ec = ExecutionContext.global
  private implicit lazy val formats = DefaultFormats
  private val jokeUrl = "http://api.icndb.com/jokes/random/"

  def joke: String = {
    val request = Http(url(jokeUrl).GET)
    val response = Await.result(request, 10 seconds)
    response.getStatusCode match {
      case 200 => parseJson(response.getResponseBody)
      case _ => s"${response.getStatusCode} : ${response.getStatusText}"
    }
  }

  private def parseJson(json: String): String = {
    val ast = parse(json)
    (ast \ "value" \ "joke").extract[String]
  }
}

/*
{ "type": "success",
  "value": { "id": 111,
             "joke": "<NAME> has a deep and abiding respect for human life... unless it gets in his way.",
             "categories": []
           }
}

JObject(List((type, JString(success)),
             (value, JObject(List((id, JInt(174)),
                                 (joke, JString(Chuck Norris cannot love, he can only not kill.)),
                                 (categories, JArray(List)))))))
 */<commit_msg>configure dispatch http object for pooling and timeout<commit_after>package rest

import dispatch._
import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext}

object AsyncRest {
  private implicit val ec = ExecutionContext.global
  private implicit lazy val formats = DefaultFormats
  private val jokeUrl = url("http://api.icndb.com/jokes/random/")
  Http.configure(_.setAllowPoolingConnections(true).setConnectTimeout(30000))

  def joke: String = {
    val request = Http(jokeUrl.GET)
    val response = Await.result(request, 10 seconds)
    response.getStatusCode match {
      case 200 => parseJson(response.getResponseBody)
      case _ => s"${response.getStatusCode} : ${response.getStatusText}"
    }
  }

  private def parseJson(json: String): String = {
    val ast = parse(json)
    (ast \ "value" \ "joke").extract[String]
  }
}

/*
{ "type": "success",
  "value": { "id": 111,
             "joke": "<NAME> has a deep and abiding respect for human life... unless it gets in his way.",
             "categories": []
           }
}

JObject(List((type, JString(success)),
             (value, JObject(List((id, JInt(174)),
                                 (joke, JString(<NAME> cannot love, he can only not kill.)),
                                 (categories, JArray(List)))))))
 */
{'fraction_non_alphanumeric': 0.1274766355140187, 'fraction_numerical': 0.011588785046728972, 'mean_word_length': 2.8614718614718613, 'pattern_counts': {'":': 10, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '956233', 'n_tokens_mistral': 814, 'n_tokens_neox': 785, 'n_words': 209}