module Network.HTTP.Client.Free.HttpClient (
HttpClient
, runHttp
, runTHttp
) where
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Trans (MonadTrans, lift)
import Control.Monad.Trans.Free.Church (FT, iterT, iterTM, liftF)
import Data.ByteString.Lazy (ByteString)
import Network.HTTP.Client (Manager, Request, Response,
httpLbs)
import Network.HTTP.Client.Free.Types (HttpF (HttpF), RequestType,
ResponseType)
import Network.HTTP.Client.Free.Util (setMethod)
import Network.HTTP.Types.Method (renderStdMethod)
data HttpClient
type instance RequestType HttpClient = Request
type instance ResponseType HttpClient = Response ByteString
iterTHttp :: ( Request ~ RequestType client
, Response ByteString ~ ResponseType client
, Monad m
, MonadIO m
)
=> Manager
-> HttpF client (m a)
-> m a
iterTHttp manager (HttpF m r next) =
let !req = setMethod m r in liftIO (httpLbs req manager) >>= next
iterTMHttp :: ( Request ~ RequestType client
, Response ByteString ~ ResponseType client
, Monad m
, MonadTrans t
, Monad (t m)
, MonadIO m
)
=> Manager
-> HttpF client (t m a)
-> t m a
iterTMHttp manager (HttpF m r next) =
let !req = setMethod m r in (lift . liftIO $ httpLbs req manager) >>= next
runHttp :: ( Request ~ RequestType client
, Response ByteString ~ ResponseType client
, Monad m
, MonadIO m
)
=> Manager
-> FT (HttpF client) m a
-> m a
runHttp manager = iterT (iterTHttp manager)
runTHttp :: ( Request ~ RequestType client
, Response ByteString ~ ResponseType client
, Monad m
, MonadIO m
, MonadTrans t
, Monad (t m)
)
=> Manager
-> FT (HttpF client) m a
-> t m a
runTHttp manager = iterTM (iterTMHttp manager)