module Network.AWS.Free where
import Control.Applicative
import Control.Monad.Reader
import Control.Monad.Trans.Free.Church
import Data.Conduit (Source, yield)
import Network.AWS.EC2.Metadata (Dynamic, Metadata)
import Network.AWS.Pager
import Network.AWS.Prelude
import Network.AWS.Request (requestURL)
import Network.AWS.Types
import Network.AWS.Waiter
#if MIN_VERSION_free(4,12,0)
#else
import Control.Monad.Catch
import Control.Monad.Trans.Free (FreeT (..))
#endif
import Prelude
data Command r where
CheckF :: (Bool -> r) -> Command r
DynF :: Dynamic -> (ByteString -> r) -> Command r
MetaF :: Metadata -> (ByteString -> r) -> Command r
UserF :: (Maybe ByteString -> r) -> Command r
SignF :: (AWSPresigner (Sg s), AWSRequest a)
=> Service s
-> UTCTime
-> Seconds
-> a
-> (ClientRequest -> r)
-> Command r
SendF :: (AWSSigner (Sg s), AWSRequest a)
=> Service s
-> a
-> (Rs a -> r)
-> Command r
AwaitF :: (AWSSigner (Sg s), AWSRequest a)
=> Service s
-> Wait a
-> a
-> (Rs a -> r)
-> Command r
instance Functor Command where
fmap f = \case
CheckF k -> CheckF (fmap f k)
DynF x k -> DynF x (fmap f k)
MetaF x k -> MetaF x (fmap f k)
UserF k -> UserF (fmap f k)
SignF s t e x k -> SignF s t e x (fmap f k)
SendF s x k -> SendF s x (fmap f k)
AwaitF s w x k -> AwaitF s w x (fmap f k)
#if MIN_VERSION_free(4,12,0)
#else
instance MonadThrow m => MonadThrow (FreeT Command m) where
throwM = lift . throwM
instance MonadCatch m => MonadCatch (FreeT Command m) where
catch (FreeT m) f = FreeT $
liftM (fmap (`catch` f)) m `catch` (runFreeT . f)
#endif
send :: (MonadFree Command m, AWSRequest a)
=> a
-> m (Rs a)
send = sendWith id
sendWith :: (MonadFree Command m, AWSSigner (Sg s), AWSRequest a)
=> (Service (Sv a) -> Service s)
-> a
-> m (Rs a)
sendWith f x = liftF (SendF (f (serviceOf x)) x id)
paginate :: (MonadFree Command m, AWSPager a)
=> a
-> Source m (Rs a)
paginate = paginateWith id
paginateWith :: (MonadFree Command m, AWSSigner (Sg s), AWSPager a)
=> (Service (Sv a) -> Service s)
-> a
-> Source m (Rs a)
paginateWith f rq = go rq
where
go !x = do
!y <- lift $ liftF (SendF s x id)
yield y
maybe (pure ())
go
(page x y)
!s = f (serviceOf rq)
await :: (MonadFree Command m, AWSRequest a)
=> Wait a
-> a
-> m (Rs a)
await = awaitWith id
awaitWith :: (MonadFree Command m, AWSSigner (Sg s), AWSRequest a)
=> (Service (Sv a) -> Service s)
-> Wait a
-> a
-> m (Rs a)
awaitWith f w x = liftF (AwaitF (f (serviceOf x)) w x id)
presignURL :: (MonadFree Command m, AWSPresigner (Sg (Sv a)), AWSRequest a)
=> UTCTime
-> Seconds
-> a
-> m ByteString
presignURL ts ex = liftM requestURL . presign ts ex
presign :: (MonadFree Command m, AWSPresigner (Sg (Sv a)), AWSRequest a)
=> UTCTime
-> Seconds
-> a
-> m ClientRequest
presign = presignWith id
presignWith :: (MonadFree Command m, AWSPresigner (Sg s), AWSRequest a)
=> (Service (Sv a) -> Service s)
-> UTCTime
-> Seconds
-> a
-> m ClientRequest
presignWith f ts ex x = liftF (SignF (f (serviceOf x)) ts ex x id)
isEC2 :: MonadFree Command m => m Bool
isEC2 = liftF (CheckF id)
dynamic :: MonadFree Command m => Dynamic -> m ByteString
dynamic d = liftF (DynF d id)
metadata :: MonadFree Command m => Metadata -> m ByteString
metadata m = liftF (MetaF m id)
userdata :: MonadFree Command m => m (Maybe ByteString)
userdata = liftF (UserF id)