<commit_before>package de.is24.play.orientdb.client

import akka.actor.ActorSystem
import akka.event.slf4j.SLF4JLogging
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import akka.http.scaladsl.unmarshalling.Unmarshaller
import akka.stream.ActorMaterializer
import de.is24.play.orientdb.{BatchOperation, OrientDbQuery}
import play.api.libs.json._
import OrientProtocol._
import akka.http.scaladsl.unmarshalling.Unmarshal
import scala.concurrent.{ExecutionContext, Future}
import scala.collection.immutable._

class OrientDbHttpClient(config: OrientClientConfig)(implicit actorSystem: ActorSystem) extends SLF4JLogging {

  private implicit val ec = actorSystem.dispatcher

  private implicit val materializer = ActorMaterializer()

  private val JsonContentType = MediaTypes.`application/json`.withCharset(HttpCharsets.`UTF-8`)

  private val orientDbCommandUrl = s"${config.url}/command/${config.database}/"

  private val orientDbBatchUrl = s"${config.url}/batch/${config.database}"

  private def orientDbFunctionUrl(name: String) = s"${config.url}/function/${config.database}/$name"

  private val authorization = headers.Authorization(BasicHttpCredentials(config.userName, config.password))

  private val http = Http()


  def select[T: Reads](orientDbQuery: OrientDbQuery): Future[Seq[T]] = {
    command(orientDbQuery)
      .flatMap { responseJson =>
      log.debug("Received orient body {}", responseJson)
      (responseJson \ "result").validate[Seq[T]] match {
        case JsSuccess(result, _) =>
          Future.successful(result)
        case JsError(e) =>
          Future.failed(new RuntimeException(s"Orient db call result has invalid body: $e"))
      }
    }
  }

  def selectJson(orientDbQuery: OrientDbQuery): Future[Seq[JsValue]] = {
    select[JsValue](orientDbQuery)
  }

  def command(orientDbQuery: OrientDbQuery): Future[JsValue] = {
    val entity = HttpEntity(orientDbQuery.query)
    log.info("Execute command {}", orientDbQuery)
    val request: HttpRequest = HttpRequest(
      uri = orientDbCommandUrl + orientDbQuery.language,
      entity = entity,
      method = HttpMethods.POST,
      headers = Seq[HttpHeader](authorization))
    http
      .singleRequest(request)
      .flatMap(handleErrorResponse(request))
      .flatMap(unmarshalResponse)
  }

  def executeBatch(batchOperation: BatchOperation): Future[JsValue] = {
    val entity = HttpEntity(JsonContentType, Json.stringify(Json.toJson(batchOperation)))
    val request: HttpRequest = HttpRequest(uri = orientDbBatchUrl, entity = entity, method = HttpMethods.POST, headers = Seq[HttpHeader](authorization))
    log.info("Execute batch {}", batchOperation)
    http<commit_msg>adjust loglevel<commit_after>package de.is24.play.orientdb.client

import akka.actor.ActorSystem
import akka.event.slf4j.SLF4JLogging
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import akka.http.scaladsl.unmarshalling.Unmarshaller
import akka.stream.ActorMaterializer
import de.is24.play.orientdb.{BatchOperation, OrientDbQuery}
import play.api.libs.json._
import OrientProtocol._
import akka.http.scaladsl.unmarshalling.Unmarshal
import scala.concurrent.{ExecutionContext, Future}
import scala.collection.immutable._

class OrientDbHttpClient(config: OrientClientConfig)(implicit actorSystem: ActorSystem) extends SLF4JLogging {

  private implicit val ec = actorSystem.dispatcher

  private implicit val materializer = ActorMaterializer()

  private val JsonContentType = MediaTypes.`application/json`.withCharset(HttpCharsets.`UTF-8`)

  private val orientDbCommandUrl = s"${config.url}/command/${config.database}/"

  private val orientDbBatchUrl = s"${config.url}/batch/${config.database}"

  private def orientDbFunctionUrl(name: String) = s"${config.url}/function/${config.database}/$name"

  private val authorization = headers.Authorization(BasicHttpCredentials(config.userName, config.password))

  private val http = Http()


  def select[T: Reads](orientDbQuery: OrientDbQuery): Future[Seq[T]] = {
    command(orientDbQuery)
      .flatMap { responseJson =>
      log.debug("Received orient body {}", responseJson)
      (responseJson \ "result").validate[Seq[T]] match {
        case JsSuccess(result, _) =>
          Future.successful(result)
        case JsError(e) =>
          Future.failed(new RuntimeException(s"Orient db call result has invalid body: $e"))
      }
    }
  }

  def selectJson(orientDbQuery: OrientDbQuery): Future[Seq[JsValue]] = {
    select[JsValue](orientDbQuery)
  }

  def command(orientDbQuery: OrientDbQuery): Future[JsValue] = {
    val entity = HttpEntity(orientDbQuery.query)
    log.debug("Execute command {}", orientDbQuery)
    val request: HttpRequest = HttpRequest(
      uri = orientDbCommandUrl + orientDbQuery.language,
      entity = entity,
      method = HttpMethods.POST,
      headers = Seq[HttpHeader](authorization))
    http
      .singleRequest(request)
      .flatMap(handleErrorResponse(request))
      .flatMap(unmarshalResponse)
  }

  def executeBatch(batchOperation: BatchOperation): Future[JsValue] = {
    val entity = HttpEntity(JsonContentType, Json.stringify(Json.toJson(batchOperation)))
    val request: HttpRequest = HttpRequest(uri = orientDbBatchUrl, entity = entity, method = HttpMethods.POST, headers = Seq[HttpHeader](authorization))
    log.debug("Execute batch {}", batchOperation)
    http
{'fraction_non_alphanumeric': 0.1103448275862069, 'fraction_numerical': 0.0029038112522686023, 'mean_word_length': 4.764644351464435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4901346', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1612, 'n_words': 314}