{-# LANGUAGE CPP, RecordWildCards #-}
module Network.Wreq.Wrecker
( Session
, defaultManagerSettings
, withWreq
, withWreqNoCookies
, withWreqSettings
, get
, post
, head_
, options
, put
, delete
, getWith
, postWith
, headWith
, optionsWith
, putWith
, deleteWith
) where
import qualified Data.ByteString.Lazy as L
import Data.Default (def)
import Network.Connection (ConnectionContext)
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Client.TLS as TLS
import qualified Network.Wreq.Session as Session
import qualified Network.Wreq.Types as Wreq
import Wrecker
data Session = Session
{ sSession :: Session.Session
, sRecorder :: Recorder
}
defaultManagerSettings :: ConnectionContext -> HTTP.ManagerSettings
defaultManagerSettings context =
(TLS.mkManagerSettingsContext (Just context) def Nothing)
{HTTP.managerResponseTimeout = HTTP.responseTimeoutNone}
withWreq :: (Session -> IO a) -> Environment -> IO a
withWreq f env =
withWreqSettings
(recorder env)
(Just (HTTP.createCookieJar []))
(defaultManagerSettings (context env))
f
withWreqNoCookies :: (Session -> IO a) -> Environment -> IO a
withWreqNoCookies f env =
withWreqSettings (recorder env) Nothing (defaultManagerSettings (context env)) f
withWreqSettings ::
Recorder
-> Maybe HTTP.CookieJar
-> HTTP.ManagerSettings
-> (Session -> IO a)
-> IO a
withWreqSettings recorder cookie settings f =
Session.withSessionControl cookie settings $ \session -> f (Session session recorder)
withSess :: (Session.Session -> String -> IO a) -> Session -> String -> IO a
withSess f sess key = record (sRecorder sess) key $ f (sSession sess) key
withSess1 :: (Session.Session -> String -> a -> IO b) -> Session -> String -> a -> IO b
withSess1 f sess key b = record (sRecorder sess) key $ f (sSession sess) key b
get :: Session -> String -> IO (HTTP.Response L.ByteString)
get = withSess Session.get
post :: Wreq.Postable a => Session -> String -> a -> IO (HTTP.Response L.ByteString)
post = withSess1 Session.post
head_ :: Session -> String -> IO (HTTP.Response ())
head_ = withSess Session.head_
options :: Session -> String -> IO (HTTP.Response ())
options = withSess Session.options
put :: Wreq.Putable a => Session -> String -> a -> IO (HTTP.Response L.ByteString)
put = withSess1 Session.put
delete :: Session -> String -> IO (HTTP.Response L.ByteString)
delete = withSess Session.delete
getWith :: Wreq.Options -> Session -> String -> IO (HTTP.Response L.ByteString)
getWith opts = withSess (Session.getWith opts)
postWith ::
Wreq.Postable a => Wreq.Options -> Session -> String -> a -> IO (HTTP.Response L.ByteString)
postWith opts = withSess1 (Session.postWith opts)
headWith :: Wreq.Options -> Session -> String -> IO (HTTP.Response ())
headWith opts = withSess (Session.headWith opts)
optionsWith :: Wreq.Options -> Session -> String -> IO (HTTP.Response ())
optionsWith opts = withSess (Session.optionsWith opts)
putWith ::
Wreq.Putable a => Wreq.Options -> Session -> String -> a -> IO (HTTP.Response L.ByteString)
putWith opts = withSess1 (Session.putWith opts)
deleteWith :: Wreq.Options -> Session -> String -> IO (HTTP.Response L.ByteString)
deleteWith opts = withSess (Session.deleteWith opts)