module Network.HTTP.Dispatch.Types where
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LBSC
data HTTPRequestMethod =
GET
| PUT
| POST
| PATCH
| DELETE deriving ( Eq, Show )
type Header = (String, String)
class Packable a where
pack :: a -> LBS.ByteString
instance Packable String where
pack = LBSC.pack
data HTTPRequest = HTTPRequest {
reqMethod :: HTTPRequestMethod
, reqUrl :: String
, reqHeaders :: [Header]
, reqBody :: Maybe LBS.ByteString
} deriving ( Eq, Show )
data HTTPResponse = HTTPResponse {
respStatus :: Int
, respHeaders :: [Header]
, respBody :: LBS.ByteString
} deriving ( Eq, Show )
withHeader :: HTTPRequest -> Header -> HTTPRequest
withHeader req header = req { reqHeaders = header : (reqHeaders req) }
withHeaders :: HTTPRequest -> [Header] -> HTTPRequest
withHeaders req headers = req { reqHeaders = headers }
withBody :: HTTPRequest -> LBS.ByteString -> HTTPRequest
withBody req body = req { reqBody = pure body }
withMethod :: HTTPRequest -> HTTPRequestMethod -> HTTPRequest
withMethod req method = req { reqMethod = method }
dropHeaderWithKey :: HTTPRequest -> String -> HTTPRequest
dropHeaderWithKey req@(HTTPRequest _ _ hdrs _) headerKey =
let filteredHeaders = filter (\(k,v) -> k /= headerKey) hdrs in
withHeaders req filteredHeaders