Commits

John Lenz committed 01ca917

use the new RequestBodyEnumChunked from http-enumerator

  • Participants
  • Parent commits 1a7dd42
  • Tags 0.3.1

Comments (0)

Files changed (1)

File Database/CouchDB/Enumerator.hs

 import           Control.Monad.Trans.Class (MonadTrans, lift)
 import           Control.Monad.Trans.Reader (ReaderT, ask)
 import qualified Data.Aeson as A
+import qualified Data.Aeson.Encode as AE
 import           Data.Attoparsec
 import           Data.Attoparsec.Enumerator (iterParser)
 import qualified Data.ByteString as B
          -> m Revision
 couchPut p q val = do v <- run_ $ couch HT.methodPut p q (iterParser A.json) body
                       either (liftIO . throw) return (valToObj v >>= objToRev)
-    where body = H.RequestBodyLBS $ A.encode $ A.toJSON val
+    where body = jsonToReqBody val
 
 -- | A version of 'couchPut' which ignores the return value.  This is slightly faster than / _ <- couchPut .../
 --   since the JSON parser is not run.
           -> a          -- ^ The object to store.
           -> m ()
 couchPut_ p q val = run_ $ couch HT.methodPut p q (yield () EOF) body
-    where body = H.RequestBodyLBS $ A.encode $ A.toJSON val
+    where body = jsonToReqBody val
 
 -- | Delete the given revision of the object.
 couchDelete :: MonadCouch m
 --- Helper Code
 -----------------------------------------------------------------------------------------
 
+-- | Converts a json object into a 'H.RequestBodyEnumChunked'
+jsonToReqBody :: (A.ToJSON a, Monad m) => a -> H.RequestBody m
+jsonToReqBody val = H.RequestBodyEnumChunked enum
+    where jbuilder = AE.fromValue $ A.toJSON val
+          enum (Continue k) = k (Chunks [jbuilder])
+          enum step         = returnI step
+
 -- | Check status codes from couch db.
 checkStatus :: Monad m => HT.Status -> Iteratee B.ByteString m b 
                        -> Iteratee B.ByteString m b