<commit_before>module Requests.Requests exposing (request, report)

import Core.Config exposing (Config)
import Result as Result exposing (Result(..))
import Http
import Driver.Http.Http as HttpDriver
import Driver.Websocket.Channels as WebsocketDriver
import Driver.Websocket.Websocket as WebsocketDriver
import Requests.Types exposing (..)
import Requests.Topics exposing (..)
import Json.Encode as Encode


report : Result String a -> a
report result =
    case result of
        Ok response ->
            response

        Err _ ->
            Debug.crash "Failed to decode response from server"


request :
    Topic
    -> (ResponseType -> msg)
    -> Context
    -> Encode.Value
    -> Config
    -> Cmd msg
request topic msg context data config =
    case getDriver topic of
        HttpDriver ->
            requestHttp config.apiHttpUrl
                topic
                msg
                context
                data

        WebsocketDriver ->
            requestWebsocket config.apiWsUrl
                topic
                msg
                context
                data



-- internals


requestHttp :
    String
    -> Topic
    -> (ResponseType -> msg)
    -> Context
    -> Encode.Value
    -> Cmd msg
requestHttp url topic msg context data =
    data
        |> Encode.encode 0
        |> HttpDriver.send
            (genericHttp msg)
            url
            (getHttpPath topic)


requestWebsocket :
    String
    -> Topic
    -> (ResponseType -> msg)
    -> Context
    -> Encode.Value
    -> Cmd msg
requestWebsocket url topic msg context data =
    let
        channelAddress =
            WebsocketDriver.getAddress (getChannel topic) context
    in
        data
            |> WebsocketDriver.send
                (genericWs msg)
                url
                channelAddress
                (getWebsocketMsg topic)


genericHttp : (ResponseType -> msg) -> Result Http.Error String -> msg
genericHttp msg result =
    case result of
        Ok data ->
            msg ( OkCode, data )

        Err (Http.BadStatus response) ->
            msg ( getCode response.status.code, "" )

        _ ->
            Debug.crash "Http Driver failure"


genericWs : (ResponseType -> msg) -> Encode.Value -> msg
genericWs msg value =
    -- TODO: handle error code
    msg ( OkCode, toString value )<commit_msg>Pass down Request body from errored HTTP requests.<commit_after>module Requests.Requests exposing (request, report)

import Core.Config exposing (Config)
import Result as Result exposing (Result(..))
import Http
import Driver.Http.Http as HttpDriver
import Driver.Websocket.Channels as WebsocketDriver
import Driver.Websocket.Websocket as WebsocketDriver
import Requests.Types exposing (..)
import Requests.Topics exposing (..)
import Json.Encode as Encode


report : Result String a -> a
report result =
    case result of
        Ok response ->
            response

        Err _ ->
            Debug.crash "Failed to decode response from server"


request :
    Topic
    -> (ResponseType -> msg)
    -> Context
    -> Encode.Value
    -> Config
    -> Cmd msg
request topic msg context data config =
    case getDriver topic of
        HttpDriver ->
            requestHttp config.apiHttpUrl
                topic
                msg
                context
                data

        WebsocketDriver ->
            requestWebsocket config.apiWsUrl
                topic
                msg
                context
                data



-- internals


requestHttp :
    String
    -> Topic
    -> (ResponseType -> msg)
    -> Context
    -> Encode.Value
    -> Cmd msg
requestHttp url topic msg context data =
    data
        |> Encode.encode 0
        |> HttpDriver.send
            (genericHttp msg)
            url
            (getHttpPath topic)


requestWebsocket :
    String
    -> Topic
    -> (ResponseType -> msg)
    -> Context
    -> Encode.Value
    -> Cmd msg
requestWebsocket url topic msg context data =
    let
        channelAddress =
            WebsocketDriver.getAddress (getChannel topic) context
    in
        data
            |> WebsocketDriver.send
                (genericWs msg)
                url
                channelAddress
                (getWebsocketMsg topic)


genericHttp : (ResponseType -> msg) -> Result Http.Error String -> msg
genericHttp msg result =
    case result of
        Ok data ->
            msg ( OkCode, data )

        Err (Http.BadStatus response) ->
            msg ( getCode response.status.code, response.body )

        _ ->
            Debug.crash "Http Driver failure"


genericWs : (ResponseType -> msg) -> Encode.Value -> msg
genericWs msg value =
    -- TODO: handle error code
    msg ( OkCode, toString value )
{'fraction_non_alphanumeric': 0.073541842772612, 'fraction_numerical': 0.00042265426880811494, 'mean_word_length': 1.7076659038901603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, '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': '891610', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1204, 'n_words': 392}