request-monad 0.1.0.0 → 0.2.0.0
raw patch · 4 files changed
+23/−19 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Control.Monad.Request.Lazy: mapRequestT :: Monad m => (x -> m r) -> RequestT x r' m a -> RequestT r r' m a
+ Control.Monad.Request.Lazy: mapRequestT :: Monad m => (x -> RequestT r r' m r) -> RequestT x r' m a -> RequestT r r' m a
- Control.Monad.Request.Lazy: mapResponseT :: Monad m => (r' -> m x) -> RequestT r x m a -> RequestT r r' m a
+ Control.Monad.Request.Lazy: mapResponseT :: Monad m => (r' -> RequestT r r' m x) -> RequestT r x m a -> RequestT r r' m a
Files
- CHANGELOG.md +4/−0
- README.md +7/−6
- request-monad.cabal +2/−2
- src/Control/Monad/Request/Lazy.hs +10/−11
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.2.0.0++* Generalize `mapRequestT` and `mapResponseT` so that requests may be performed in middleware+ # 0.1.0.0 * First public release
README.md view
@@ -28,6 +28,7 @@ ```haskell import Control.Monad import Control.Monad.Request+import System.IO getNameAndAge :: Monad m => RequestT String String m (String, Int) getNameAndAge = do@@ -36,7 +37,7 @@ return (name, age) prompt :: String -> IO String-prompt str = putStr str >> getLine+prompt str = putStr str >> hFlush stdout >> getLine main :: IO () main = do@@ -71,25 +72,25 @@ import Control.Monad import Control.Monad.Request import qualified Data.Aeson as A-import qualified Data.ByteString as B+import qualified Data.ByteString.Lazy as B deserialize :: (A.FromJSON a, Monad m) => B.ByteString -> m (Maybe a) deserialize = return . A.decode -tryTwice :: Monad m => RequestT B.ByteString (Maybe A.Value) m (Maybe A.Value)+tryTwice :: Monad m => RequestT B.ByteString B.ByteString m (Maybe A.Value) tryTwice = mapResponseT deserialize $ do a <- send "request one" b <- send "request two" return $ a `mplus` b -handleRequest :: Monad m => B.ByteString -> m B.ByteString+handleRequest :: Monad m => B.ByteString -> B.ByteString handleRequest "request one" = return "not json"-handleRequest x = "15"+handleRequest x = "[15]" main :: IO () main = do let res = runRequest tryTwice handleRequest- print $ res -- Prints "Just (Number 15)"+ print $ res -- Prints "Just (Array (fromList [Number 15.0]))" ``` ## TODO
request-monad.cabal view
@@ -1,5 +1,5 @@ name: request-monad-version: 0.1.0.0+version: 0.2.0.0 synopsis: A transformer for generic requests description: An MTL-style monad that can be used to perform requests. Using RequestT@@ -27,5 +27,5 @@ exposed-modules: Control.Monad.Request, Control.Monad.Request.Class, Control.Monad.Request.Lazy- ghc-options: -O3 -Wall+ ghc-options: -Wall default-language: Haskell2010
src/Control/Monad/Request/Lazy.hs view
@@ -2,7 +2,6 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE UndecidableInstances #-} - {- | Module : Control.Monad.Request.Lazy Copyright : (c) Tom Hulihan <hulihan.tom159@gmail.com> 2014,@@ -74,14 +73,14 @@ mapRequest :: (x -> r) -- ^ The middleware function -> Request x r' a -- ^ The computation which sends @x@ -> Request r r' a -- ^ The computation which sends @r@-mapRequest f = mapRequestT (Identity . f)+mapRequest f = mapRequestT (return . f) -- | Given a mapping from @r\' -> x@, transform a computation handles responses -- of type @x@ to one that handles responses of type @r'@. mapResponse :: (r' -> x) -- ^ The middleware function -> Request r x a -- ^ The computation which handles @x@ -> Request r r' a -- ^ The computation which handles @r'@-mapResponse f = mapResponseT (Identity . f)+mapResponse f = mapResponseT (return . f) -------------------------------------------------------------------------------- -- 'RequestT' and its associated functions@@ -112,22 +111,22 @@ in go m -- | Turn a computation that requests @x@ into a computation that requests @r@.-mapRequestT :: Monad m => (x -> m r) -- ^ The middleware function- -> RequestT x r' m a -- ^ The @x@-requesting computation- -> RequestT r r' m a -- ^ The @r@-requesting computation+mapRequestT :: Monad m => (x -> RequestT r r' m r) -- ^ The middleware+ -> RequestT x r' m a -- ^ The @x@-requester+ -> RequestT r r' m a -- ^ The @r@-requester mapRequestT f = let go (Pure a) = Pure a- go (Request x g) = lift (f x) >>= flip Request (go . g)+ go (Request x g) = f x >>= flip Request (go . g) go (Lift act) = Lift (liftM go act) in go -- | Turn a computation that handles @x@ into a computation that handles @r'@.-mapResponseT :: Monad m => (r' -> m x) -- ^ The middleware function- -> RequestT r x m a -- ^ The @x@-handling computation- -> RequestT r r' m a -- ^ The @r'@-handling computation+mapResponseT :: Monad m => (r' -> RequestT r r' m x) -- ^ The middleware+ -> RequestT r x m a -- ^ The @x@-handler+ -> RequestT r r' m a -- ^ The @r'@-handler mapResponseT f = let go (Pure a) = Pure a- go (Request r g) = Request r (go . g <=< lift . f)+ go (Request r g) = Request r (go . g <=< f) go (Lift act) = Lift (liftM go act) in go