module Captcha.Internal.Request where

import Captcha.Internal.Monad (HasCaptchaEnv (session))
import Control.Lens ((^.))
import Control.Monad.Cont (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader (ask))
import Data.ByteString.Lazy (ByteString)
import Data.Text (Text, unpack)
import Network.Wreq (Options, Response)
import qualified Network.Wreq.Session as Session
import Network.Wreq.Types (Postable)

-- | Send a POST request with the given session from 'CaptchaEnv'.
post :: (HasCaptchaEnv r, MonadReader r m, MonadIO m, Postable a) => Options -> Text -> a -> m (Response ByteString)
post :: Options -> Text -> a -> m (Response ByteString)
post Options
options Text
url a
payload =
  m r
forall r (m :: * -> *). MonadReader r m => m r
ask m r -> (r -> m (Response ByteString)) -> m (Response ByteString)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \r
env -> IO (Response ByteString) -> m (Response ByteString)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response ByteString) -> m (Response ByteString))
-> IO (Response ByteString) -> m (Response ByteString)
forall a b. (a -> b) -> a -> b
$ Options -> Session -> String -> a -> IO (Response ByteString)
forall a.
Postable a =>
Options -> Session -> String -> a -> IO (Response ByteString)
Session.postWith Options
options (r
env r -> Getting Session r Session -> Session
forall s a. s -> Getting a s a -> a
^. Getting Session r Session
forall c. HasCaptchaEnv c => Lens' c Session
session) (Text -> String
unpack Text
url) a
payload

-- | Send a GET request with the given session from 'CaptchaEnv'.
get :: (HasCaptchaEnv r, MonadReader r m, MonadIO m) => Options -> Text -> m (Response ByteString)
get :: Options -> Text -> m (Response ByteString)
get Options
options Text
url =
  m r
forall r (m :: * -> *). MonadReader r m => m r
ask m r -> (r -> m (Response ByteString)) -> m (Response ByteString)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \r
env -> IO (Response ByteString) -> m (Response ByteString)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response ByteString) -> m (Response ByteString))
-> IO (Response ByteString) -> m (Response ByteString)
forall a b. (a -> b) -> a -> b
$ Options -> Session -> String -> IO (Response ByteString)
Session.getWith Options
options (r
env r -> Getting Session r Session -> Session
forall s a. s -> Getting a s a -> a
^. Getting Session r Session
forall c. HasCaptchaEnv c => Lens' c Session
session) (Text -> String
unpack Text
url)