<commit_before>package threesixty.engine

import threesixty.visualizer.Visualization

import spray.http.{HttpMethods, HttpResponse, HttpHeaders, HttpRequest, AllOrigins, StatusCodes, ContentTypes, ContentType, MediaTypes, HttpEntity, StatusCode}
import ContentTypes.{`text/plain(UTF-8)`, `application/json`}
import MediaTypes.`image/svg+xml`
import HttpMethods.{GET, POST}
import HttpHeaders.`Access-Control-Allow-Origin`

import spray.json.{JsString, JsValue, JsObject}


trait UsageInfo {
    def usage: String
}


trait EngineResponse {
    def toHttpResponse: HttpResponse
}

object SuccessResponse {
    def apply(json: JsValue): SuccessResponse = SuccessResponse(json.prettyPrint, `application/json`)
}
case class SuccessResponse(msg: String, contentType: ContentType = `text/plain(UTF-8)`) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = StatusCodes.OK,
            entity = HttpEntity(contentType, msg),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}


case class VisualizationResponse(visualization: Visualization) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = StatusCodes.OK,
            entity = HttpEntity(`image/svg+xml`, visualization.toString),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}


object ErrorResponse {
    def apply(json: JsValue): ErrorResponse = ErrorResponse(json.toString, `application/json`)
}
case class ErrorResponse(msg: String, contentType: ContentType = `text/plain(UTF-8)`) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = StatusCodes.BadRequest,
            entity = HttpEntity(contentType, msg),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}

object HelpResponse {
    def apply(json: JsValue): HelpResponse = HelpResponse(msg = json.toString, contentType = `application/json`)
    def apply(json: JsValue, status: StatusCode): HelpResponse = HelpResponse(json.toString, status, `application/json`)
}
case class HelpResponse(msg: String, status: StatusCode = StatusCodes.OK, contentType: ContentType = `text/plain(UTF-8)`) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = status,
            entity = HttpEntity(contentType, msg),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}


object Engine {
    def toErrorJson(errorMsg: String): JsValue = {
        JsObject(Map[String, JsValue]("error" -> JsString(errorMsg)))
    }
}<commit_msg>Implicit conversion for EngineResponses<commit_after>package threesixty.engine

import threesixty.visualizer.Visualization

import spray.http.{HttpMethods, HttpResponse, HttpHeaders, HttpRequest, AllOrigins, StatusCodes, ContentTypes, ContentType, MediaTypes, HttpEntity, StatusCode}
import ContentTypes.{`text/plain(UTF-8)`, `application/json`}
import MediaTypes.`image/svg+xml`
import HttpMethods.{GET, POST}
import HttpHeaders.`Access-Control-Allow-Origin`

import spray.json.{JsString, JsValue, JsObject}


trait UsageInfo {
    def usage: String
}


trait EngineResponse {
    def toHttpResponse: HttpResponse
}

object SuccessResponse {
    def apply(json: JsValue): SuccessResponse = SuccessResponse(json.prettyPrint, `application/json`)
    implicit def toHttpResponse(s: SuccessResponse): HttpResponse = s.toHttpResponse
}
case class SuccessResponse(msg: String, contentType: ContentType = `text/plain(UTF-8)`) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = StatusCodes.OK,
            entity = HttpEntity(contentType, msg),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}

object VisualizationResponse {
    implicit def toHttpResponse(v: VisualizationResponse): HttpResponse = v.toHttpResponse
}
case class VisualizationResponse(visualization: Visualization) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = StatusCodes.OK,
            entity = HttpEntity(`image/svg+xml`, visualization.toString),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}


object ErrorResponse {
    def apply(json: JsValue): ErrorResponse = ErrorResponse(json.toString, `application/json`)
    implicit def toHttpResponse(e: ErrorResponse): HttpResponse = e.toHttpResponse
}
case class ErrorResponse(msg: String, contentType: ContentType = `text/plain(UTF-8)`) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = StatusCodes.BadRequest,
            entity = HttpEntity(contentType, msg),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}

object HelpResponse {
    def apply(json: JsValue): HelpResponse = HelpResponse(msg = json.toString, contentType = `application/json`)
    def apply(json: JsValue, status: StatusCode): HelpResponse = HelpResponse(json.toString, status, `application/json`)
    implicit def toHttpResponse(h: HelpResponse): HttpResponse = h.toHttpResponse
}
case class HelpResponse(msg: String, status: StatusCode = StatusCodes.OK, contentType: ContentType = `text/plain(UTF-8)`) extends EngineResponse {
    def toHttpResponse: HttpResponse = HttpResponse(
            status = status,
            entity = HttpEntity(contentType, msg),
            headers = List(`Access-Control-Allow-Origin`(AllOrigins))
        )
}


object Engine {
    def toErrorJson(errorMsg: String): JsValue = {
        JsObject(Map[String, JsValue]("error" -> JsString(errorMsg)))
    }
}
{'fraction_non_alphanumeric': 0.10518859571453869, 'fraction_numerical': 0.0014166814237648309, 'mean_word_length': 4.2055299539170505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3129188', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1573, 'n_words': 357}