<commit_before>module OutWatch.Http ( createHttpHandler
  , get
  , getWithBody
  , post
  , put
  , delete
  , options
  , head
  ) where

import Control.Monad.Eff.Unsafe (unsafePerformEff)
import Data.HTTP.Method (Method(..))
import Network.HTTP.Affjax (AJAX)
import OutWatch.Sink (Handler, createHandlerImpl)
import Prelude (show, ($), (#))
import RxJS.Observable (Observable, Request, Response, ajax, ajaxWithBody, switchMap, share)


createHttpHandler :: forall a e. Array a -> Handler (ajax :: AJAX | e) a
createHttpHandler = createHandlerImpl


get :: forall e. Handler (ajax :: AJAX | e) String -> Observable Response
get = requestWithUrl

getWithBody :: forall e. Handler (ajax :: AJAX | e) Request -> Observable Response
getWithBody = request GET

post :: forall e. Handler (ajax :: AJAX | e) Request -> Observable Response
post = request POST

put :: forall e. Handler (ajax :: AJAX | e) Request -> Observable Response
put = request PUT

delete :: forall e. Handler (ajax :: AJAX | e) Request -> Observable Response
delete = request DELETE

options :: forall e. Handler (ajax :: AJAX | e) Request -> Observable Response
options = request OPTIONS

head :: forall e. Handler (ajax :: AJAX | e) Request -> Observable Response
head = request HEAD



request :: forall e. Method -> Handler (ajax :: AJAX | e) Request -> Observable Response
request method handler =
  switchMap handler.src
    (\req -> unsafePerformEff $ ajaxWithBody (setResponseType req method))
    # share

requestWithUrl :: forall e. Handler (ajax :: AJAX | e) String -> Observable Response
requestWithUrl handler =
  switchMap handler.src (\url -> unsafePerformEff $ ajax url) # share

setResponseType :: Request -> Method -> Request
setResponseType req method =
  req { responseType = show method }<commit_msg>Improve HTTP api<commit_after>module OutWatch.Http ( createHttpHandler
  , get
  , getWithBody
  , post
  , put
  , delete
  , options
  , head
  , requestWithBody
  , HttpHandler
  ) where

import Control.Monad.Eff.Unsafe (unsafePerformEff)
import Data.HTTP.Method (Method(..))
import Data.StrMap (empty)
import Network.HTTP.Affjax (AJAX)
import OutWatch.Sink (Handler, Observer, createHandlerImpl)
import Prelude (show, ($))
import RxJS.Observable (Observable, Request, Response, ajax, ajaxWithBody, switchMap)


createHttpHandler :: forall a e. Array a -> Handler (ajax :: AJAX | e) a
createHttpHandler = createHandlerImpl


get :: forall eff a. (Observable a -> Observable String) -> HttpHandler (ajax :: AJAX | eff)  a
get = requestWithUrl

getWithBody :: forall e a. (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
getWithBody = request GET

post :: forall e a. (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
post = request POST

put :: forall e a. (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
put = request PUT

delete :: forall e a. (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
delete = request DELETE

options :: forall e a. (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
options = request OPTIONS

head :: forall e a. (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
head = request HEAD

requestWithBody :: String -> String -> Request
requestWithBody url body =
  { url : url
  , body : body
  , timeout : 0
  , headers : empty
  , crossDomain : false
  , responseType : ""
  , method : show GET
  }

type HttpHandler eff input = { responses :: Observable Response, sink :: Observer eff input }


requestWithUrl :: forall e a. (Observable a -> Observable String) -> HttpHandler (ajax :: AJAX | e) a
requestWithUrl transform =
  let handler = createHandlerImpl[]
      transformed = transform handler.src
      responses = switchMap transformed (\url -> unsafePerformEff $ ajax url)
  in {responses, sink : handler.sink}


request :: forall e a. Method -> (Observable a -> Observable Request) -> HttpHandler (ajax :: AJAX | e) a
request method transform =
  let handler = createHandlerImpl[]
      transformed = transform handler.src
      responses = switchMap transformed
        (\req -> unsafePerformEff $ ajaxWithBody (setMethod req method))
  in {responses, sink : handler.sink}


setMethod :: Request -> Method -> Request
setMethod req method =
  req { method = show method }
{'fraction_non_alphanumeric': 0.10906156329259857, 'fraction_numerical': 0.00023057412958266084, 'mean_word_length': 3.82, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, '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': '7347585', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1248, 'n_words': 470}