{-# LANGUAGE RecordWildCards #-}
module Network.HTTP.Client.Extras (
Url
, HttpResponse(..)
, readHttpResponse
, jsonResponseHeaders
, _200ok
, _400badRequest
, _404notFound
, _405methodNotAllowed
, _408requestTimeout
, _500internalServerError
) where
import qualified Data.ByteString as SB
( ByteString, unpack )
import Data.ByteString.Lazy
( ByteString, unpack )
import Data.Vector
( fromList )
import Network.HTTP.Client
( HttpException(..), CookieJar, HttpExceptionContent(StatusCodeException)
, Response, responseCookieJar, responseBody, createCookieJar
, responseHeaders, responseVersion, responseStatus )
import Network.HTTP.Types
import Data.Aeson (Value(..), object, (.=))
import qualified Data.Text as T (Text, pack)
type Url = String
data HttpResponse = HttpResponse
{ _responseStatus :: Status
, _responseVersion :: HttpVersion
, _responseHeaders :: ResponseHeaders
, _responseBody :: ByteString
, _responseCookieJar :: CookieJar
} deriving (Eq, Show)
readHttpResponse :: Response ByteString -> HttpResponse
readHttpResponse r = HttpResponse
{ _responseStatus = responseStatus r
, _responseVersion = responseVersion r
, _responseHeaders = responseHeaders r
, _responseBody = responseBody r
, _responseCookieJar = responseCookieJar r
}
jsonResponseHeaders :: ResponseHeaders -> Value
jsonResponseHeaders =
Array . fromList . map (\(k,v) -> object [ (key k) .= (val v) ])
where
key = T.pack . concatMap esc . show
val = T.pack . concatMap esc . show
esc c = case c of
'\\' -> "\\"
'"' -> "\\\""
_ -> [c]
_200ok :: ByteString -> HttpResponse
_200ok body = HttpResponse
{ _responseStatus = status200
, _responseVersion = http11
, _responseHeaders = []
, _responseBody = body
, _responseCookieJar = createCookieJar []
}
_400badRequest :: ByteString -> HttpResponse
_400badRequest body = HttpResponse
{ _responseStatus = status400
, _responseVersion = http11
, _responseHeaders = []
, _responseBody = body
, _responseCookieJar = createCookieJar []
}
_404notFound :: ByteString -> HttpResponse
_404notFound body = HttpResponse
{ _responseStatus = status404
, _responseVersion = http11
, _responseHeaders = []
, _responseBody = body
, _responseCookieJar = createCookieJar []
}
_405methodNotAllowed :: ByteString -> HttpResponse
_405methodNotAllowed body = HttpResponse
{ _responseStatus = status405
, _responseVersion = http11
, _responseHeaders = []
, _responseBody = body
, _responseCookieJar = createCookieJar []
}
_408requestTimeout :: ByteString -> HttpResponse
_408requestTimeout body = HttpResponse
{ _responseStatus = status408
, _responseVersion = http11
, _responseHeaders = []
, _responseBody = body
, _responseCookieJar = createCookieJar []
}
_500internalServerError :: ByteString -> HttpResponse
_500internalServerError body = HttpResponse
{ _responseStatus = status500
, _responseVersion = http11
, _responseHeaders = []
, _responseBody = body
, _responseCookieJar = createCookieJar []
}