{-# LANGUAGE DeriveFunctor #-}

module Network.Shelob where

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

import           Network.Shelob.Connection
import           Network.Shelob.Request
import           Network.Shelob.Response
import           Network.Shelob.Types

userAgent :: Header
userAgent = Header "User-Agent" "haskell/vriska"

request :: Method -> BS.ByteString -> [Header] -> Request
request m p hs = Request m p (hs' ++ hs) ""
  where hs' = [ Header "Content-Length" "0"
              , userAgent
              ]

head :: BS.ByteString -> [Header] -> Request
head p hs = Request MHead p (hs' ++ hs) ""
  where hs' = [ Header "Content-Length" "0"
              , userAgent
              ]

get :: BS.ByteString -> [Header] -> Request
get p hs = Request MGet p (hs' ++ hs) ""
  where hs' = [ Header "Content-Length" "0"
              , userAgent
              ]

post :: BS.ByteString -> [Header] -> BS.ByteString -> Request
post p hs body = Request MPost p (hs' ++ hs) body
  where hs' = [ Header "Content-Length" (BS8.pack (show (BS.length body)))
              , userAgent
              ]

makeRequest :: Connection -> Request -> IO (Either HTTPError Response)
makeRequest conn req = do
  let pReq = pRequest req
  connWrite conn pReq
  readResponse conn req
