<commit_before>package blueeyes.core.service

import HttpStatusCodeImplicits._
import blueeyes.core.data.{Bijection, DataTranscoder}
import blueeyes.util.Future
import com.ning.http.client.{ 
	AsyncHttpClient, 
	AsyncCompletionHandler, 
	FluentStringsMap, 
	Response, 
	RequestBuilderBase 
}
import java.util.concurrent.{ Future => JavaFuture }

trait HttpClientNetty[T] extends HttpClient[T] with DataTranscoder[T, String] {
  def apply(request: HttpRequest[T]): Future[HttpResponse[T]] = {
    new Future[HttpResponse[T]]() {
      prepareRequest(request).get.execute(
        new AsyncCompletionHandler[Response] {
          def onCompleted(response: Response): Response = {
            val data = transcode.unapply(response.getResponseBody)
            deliver(new HttpResponse[T](status = HttpStatus(response.getStatusCode), content = Some(data)));
            response;
          }

          override def onThrowable(t: Throwable) {
            deliver(new HttpResponse[T](HttpStatus(HttpStatusCodes.InternalServerError)));
          }
        })
    }
  }

  /**
   * TODO - Figure out how set set HTTP version using Netty
   * @param request
   * @return
   */
  protected def prepareRequest(request: HttpRequest[T]): Option[AsyncHttpClient#BoundRequestBuilder] = {	
	def setBody(rb: Option[AsyncHttpClient#BoundRequestBuilder]): Option[AsyncHttpClient#BoundRequestBuilder] = {
	  request.content.foreach(c => rb.foreach(r => r.setBody(transcode(c))))
	  rb
	}
	
	/**
	 * Netty does not allow parameters to be set for non-POST/PUT requests
	 */
	def setParameters(requestBuilder: Option[AsyncHttpClient#BoundRequestBuilder]): Option[AsyncHttpClient#BoundRequestBuilder] = {
	  requestBuilder.foreach(rb => { 
	    rb.setParameters(request.parameters.foldLeft(new FluentStringsMap())((fsMap, pair) => fsMap.add(pair._1, pair._2)))
	  }) 
	  requestBuilder
	}
	
    var reqBuilder = request.method match {
      case HttpMethods.GET => Some(new AsyncHttpClient().prepareGet(request.uri))
      case HttpMethods.POST => setParameters(setBody(Some(new AsyncHttpClient().preparePost(request.uri))))
      case HttpMethods.PUT => setParameters(setBody(Some(new AsyncHttpClient().preparePut(request.uri))))
      case HttpMethods.DELETE => Some(new AsyncHttpClient().prepareDelete(request.uri))
      case HttpMethods.OPTIONS => Some(new AsyncHttpClient().prepareOptions(request.uri))
      case _ => None
    }

    request.headers.foreach((pair) => reqBuilder.foreach(r => r.setHeader(pair._1, pair._2)))
    reqBuilder
  }
}
<commit_msg>code cleanup<commit_after>package blueeyes.core.service

import HttpStatusCodeImplicits._
import blueeyes.core.data.{Bijection, DataTranscoder}
import blueeyes.util.Future
import com.ning.http.client.{ 
	AsyncHttpClient, 
	AsyncCompletionHandler, 
	FluentStringsMap, 
	Response, 
	RequestBuilderBase 
}
import java.util.concurrent.{ Future => JavaFuture }

trait HttpClientNetty[T] extends HttpClient[T] with DataTranscoder[T, String] {
  def apply(request: HttpRequest[T]): Future[HttpResponse[T]] = {
    new Future[HttpResponse[T]]() {
      prepareRequest(request).get.execute(
        new AsyncCompletionHandler[Response] {
          def onCompleted(response: Response): Response = {
            val data = transcode.unapply(response.getResponseBody)
            deliver(HttpResponse[T](status = HttpStatus(response.getStatusCode), content = Some(data)));
            response;
          }

          override def onThrowable(t: Throwable) {
            deliver(HttpResponse[T](HttpStatus(HttpStatusCodes.InternalServerError)));
          }
        })
    }
  }

  /**
   * TODO - Figure out how set set HTTP version using Netty
   * @param request
   * @return
   */
  protected def prepareRequest(request: HttpRequest[T]): Option[AsyncHttpClient#BoundRequestBuilder] = {	
    def setBody(rb: Option[AsyncHttpClient#BoundRequestBuilder]): Option[AsyncHttpClient#BoundRequestBuilder] = {
      for (content <- request.content; requestBuilder <- rb)
        yield requestBuilder.setBody(transcode(content))
    }

    /**
    * Netty does not allow parameters to be set for non-POST/PUT requests
    */
    def setParameters(requestBuilder: Option[AsyncHttpClient#BoundRequestBuilder]): Option[AsyncHttpClient#BoundRequestBuilder] = {
      requestBuilder.map(rb => { 
        rb.setParameters(request.parameters.foldLeft(new FluentStringsMap()) { (fsMap, pair) => 
          fsMap.add(pair._1, pair._2)
        })
      })
    }
	
    var reqBuilder = request.method match {
      case HttpMethods.GET      => Some(new AsyncHttpClient().prepareGet(request.uri))
      case HttpMethods.POST     => setParameters(setBody(Some(new AsyncHttpClient().preparePost(request.uri))))
      case HttpMethods.PUT      => setParameters(setBody(Some(new AsyncHttpClient().preparePut(request.uri))))
      case HttpMethods.DELETE   => Some(new AsyncHttpClient().prepareDelete(request.uri))
      case HttpMethods.OPTIONS  => Some(new AsyncHttpClient().prepareOptions(request.uri))
      case _ => None
    }

    // ("Content-Type", "text/javascript")
    val newHeaders = request.headers /*++ Map(
      ContentType((for (ContentType(contentType) <- request.headers) yield contentType).headOption.getOrElse(transcoder.mimeType._2)),
      ContentLength(): _*
    )*/
    
    //val contentType = 

    for (pair <- newHeaders; r <- reqBuilder)
      yield r.setHeader(pair._1, pair._2)   
    
    reqBuilder
  }
}

{'fraction_non_alphanumeric': 0.1183920704845815, 'fraction_numerical': 0.0016519823788546256, 'mean_word_length': 3.7382608695652175, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 25, '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': '7657745', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1549, 'n_words': 288}