module Network.HTTP.Simple
(
httpBS
, httpLBS
, httpNoBody
, httpJSON
, httpJSONEither
, httpSink
, httpSource
, withResponse
, H.Request
, H.Response
, JSONException (..)
, H.HttpException (..)
, H.Proxy (..)
, H.defaultRequest
, H.parseRequest
, H.parseRequest_
, setRequestMethod
, setRequestSecure
, setRequestHost
, setRequestPort
, setRequestPath
, addRequestHeader
, getRequestHeader
, setRequestHeader
, setRequestHeaders
, setRequestQueryString
, getRequestQueryString
, setRequestBody
, setRequestBodyJSON
, setRequestBodyLBS
, setRequestBodySource
, setRequestBodyFile
, setRequestBodyURLEncoded
, H.setRequestIgnoreStatus
, setRequestBasicAuth
, setRequestManager
, setRequestProxy
, getResponseStatus
, getResponseStatusCode
, getResponseHeader
, getResponseHeaders
, getResponseBody
, httpLbs
) where
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Network.HTTP.Client as H
import qualified Network.HTTP.Client.Internal as HI
import qualified Network.HTTP.Client.TLS as H
import Network.HTTP.Client.Conduit (bodyReaderSource)
import qualified Network.HTTP.Client.Conduit as HC
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Aeson (FromJSON (..), Value)
import Data.Aeson.Parser (json')
import qualified Data.Aeson.Types as A
import qualified Data.Aeson as A
import qualified Data.Traversable as T
import Control.Exception (throwIO, Exception)
import Data.Typeable (Typeable)
import qualified Data.Conduit as C
import qualified Data.Conduit.Attoparsec as C
import qualified Control.Monad.Catch as Catch
import qualified Network.HTTP.Types as H
import Data.Int (Int64)
import Control.Monad.Trans.Resource (MonadResource)
httpBS :: MonadIO m => H.Request -> m (H.Response S.ByteString)
httpBS req = liftIO $ do
man <- H.getGlobalManager
fmap L.toStrict `fmap` H.httpLbs req man
httpLBS :: MonadIO m => H.Request -> m (H.Response L.ByteString)
httpLBS req = liftIO $ do
man <- H.getGlobalManager
H.httpLbs req man
httpNoBody :: MonadIO m => H.Request -> m (H.Response ())
httpNoBody req = liftIO $ do
man <- H.getGlobalManager
H.httpNoBody req man
httpJSON :: (MonadIO m, FromJSON a) => H.Request -> m (H.Response a)
httpJSON req = liftIO $ httpJSONEither req >>= T.mapM (either throwIO return)
httpJSONEither :: (MonadIO m, FromJSON a)
=> H.Request
-> m (H.Response (Either JSONException a))
httpJSONEither req = liftIO $ httpSink req' sink
where
req' = addRequestHeader H.hAccept "application/json" req
sink orig = fmap (\x -> fmap (const x) orig) $ do
eres1 <- C.sinkParserEither json'
case eres1 of
Left e -> return $ Left $ JSONParseException req' orig e
Right value ->
case A.fromJSON value of
A.Error e -> return $ Left $ JSONConversionException
req' (fmap (const value) orig) e
A.Success x -> return $ Right x
data JSONException
= JSONParseException H.Request (H.Response ()) C.ParseError
| JSONConversionException H.Request (H.Response Value) String
deriving (Show, Typeable)
instance Exception JSONException
httpSink :: (MonadIO m, Catch.MonadMask m)
=> H.Request
-> (H.Response () -> C.Sink S.ByteString m a)
-> m a
httpSink req sink = do
man <- liftIO H.getGlobalManager
Catch.bracket
(liftIO $ H.responseOpen req man)
(liftIO . H.responseClose)
(\res -> bodyReaderSource (getResponseBody res)
C.$$ sink (fmap (const ()) res))
httpSource :: (MonadResource m, MonadIO n)
=> H.Request
-> (H.Response (C.ConduitM i S.ByteString n ())
-> C.ConduitM i o m r)
-> C.ConduitM i o m r
httpSource req withRes = do
man <- liftIO H.getGlobalManager
C.bracketP (H.responseOpen req man) H.responseClose
(withRes . fmap bodyReaderSource)
withResponse :: (MonadIO m, Catch.MonadMask m, MonadIO n)
=> H.Request
-> (H.Response (C.ConduitM i S.ByteString n ()) -> m a)
-> m a
withResponse req withRes = do
man <- liftIO H.getGlobalManager
Catch.bracket
(liftIO (H.responseOpen req man))
(liftIO . H.responseClose)
(withRes . fmap bodyReaderSource)
httpLbs :: MonadIO m => H.Request -> m (H.Response L.ByteString)
httpLbs = httpLBS
setRequestMethod :: S.ByteString -> H.Request -> H.Request
setRequestMethod x req = req { H.method = x }
setRequestSecure :: Bool -> H.Request -> H.Request
setRequestSecure x req = req { H.secure = x }
setRequestHost :: S.ByteString -> H.Request -> H.Request
setRequestHost x r = r { H.host = x }
setRequestPort :: Int -> H.Request -> H.Request
setRequestPort x r = r { H.port = x }
setRequestPath :: S.ByteString -> H.Request -> H.Request
setRequestPath x r = r { H.path = x }
addRequestHeader :: H.HeaderName -> S.ByteString -> H.Request -> H.Request
addRequestHeader name val req =
req { H.requestHeaders = (name, val) : H.requestHeaders req }
getRequestHeader :: H.HeaderName -> H.Request -> [S.ByteString]
getRequestHeader name =
map snd . filter (\(x, _) -> x == name) . H.requestHeaders
setRequestHeader :: H.HeaderName -> [S.ByteString] -> H.Request -> H.Request
setRequestHeader name vals req =
req { H.requestHeaders =
filter (\(x, _) -> x /= name) (H.requestHeaders req)
++ (map (name, ) vals)
}
setRequestHeaders :: [(H.HeaderName, S.ByteString)] -> H.Request -> H.Request
setRequestHeaders x req = req { H.requestHeaders = x }
getRequestQueryString :: H.Request -> [(S.ByteString, Maybe S.ByteString)]
getRequestQueryString = H.parseQuery . H.queryString
setRequestQueryString :: [(S.ByteString, Maybe S.ByteString)] -> H.Request -> H.Request
setRequestQueryString = H.setQueryString
setRequestBody :: H.RequestBody -> H.Request -> H.Request
setRequestBody x req = req { H.requestBody = x }
setRequestBodyJSON :: A.ToJSON a => a -> H.Request -> H.Request
setRequestBodyJSON x req =
req { H.requestHeaders
= (H.hContentType, "application/json; charset=utf-8")
: filter (\(y, _) -> y /= H.hContentType) (H.requestHeaders req)
, H.requestBody = H.RequestBodyLBS $ A.encode x
}
setRequestBodyLBS :: L.ByteString -> H.Request -> H.Request
setRequestBodyLBS = setRequestBody . H.RequestBodyLBS
setRequestBodySource :: Int64
-> C.Source IO S.ByteString
-> H.Request
-> H.Request
setRequestBodySource len src req = req { H.requestBody = HC.requestBodySource len src }
setRequestBodyFile :: FilePath -> H.Request -> H.Request
setRequestBodyFile = setRequestBody . HI.RequestBodyIO . H.streamFile
setRequestBodyURLEncoded :: [(S.ByteString, S.ByteString)] -> H.Request -> H.Request
setRequestBodyURLEncoded = H.urlEncodedBody
setRequestBasicAuth :: S.ByteString
-> S.ByteString
-> H.Request
-> H.Request
setRequestBasicAuth = H.applyBasicAuth
setRequestManager :: H.Manager -> H.Request -> H.Request
setRequestManager x req = req { HI.requestManagerOverride = Just x }
setRequestProxy :: Maybe H.Proxy -> H.Request -> H.Request
setRequestProxy x req = req { H.proxy = x }
getResponseStatus :: H.Response a -> H.Status
getResponseStatus = H.responseStatus
getResponseStatusCode :: H.Response a -> Int
getResponseStatusCode = H.statusCode . getResponseStatus
getResponseHeader :: H.HeaderName -> H.Response a -> [S.ByteString]
getResponseHeader name = map snd . filter (\(x, _) -> x == name) . H.responseHeaders
getResponseHeaders :: H.Response a -> [(H.HeaderName, S.ByteString)]
getResponseHeaders = H.responseHeaders
getResponseBody :: H.Response a -> a
getResponseBody = H.responseBody