packages feed

http-client-conduit (empty) → 0.2.0.0

raw patch · 4 files changed

+135/−0 lines, 4 filesdep +basedep +bytestringdep +conduitsetup-changed

Dependencies added: base, bytestring, conduit, http-client, resourcet, transformers

Files

+ LICENSE view
@@ -0,0 +1,20 @@+The MIT License (MIT)++Copyright (c) 2013 Michael Snoyman++Permission is hereby granted, free of charge, to any person obtaining a copy of+this software and associated documentation files (the "Software"), to deal in+the Software without restriction, including without limitation the rights to+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+the Software, and to permit persons to whom the Software is furnished to do so,+subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Network/HTTP/Client/Conduit.hs view
@@ -0,0 +1,91 @@+{-# LANGUAGE RankNTypes #-}+-- | Frontend support for using http-client with conduit. Intended for use with+-- higher-level libraries like http-conduit.+module Network.HTTP.Client.Conduit+    ( requestBodySource+    , requestBodySourceChunked+    , requestBodySourceIO+    , requestBodySourceChunkedIO+    , bodyReaderSource+    , http+    ) where++import Data.Conduit+import qualified Data.Conduit.Internal as CI+import Control.Monad.Trans.Resource+import Network.HTTP.Client+import Network.HTTP.Client.Internal+import Data.Int (Int64)+import qualified Data.ByteString as S+import Data.ByteString (ByteString)+import Data.IORef+import Control.Monad.IO.Class (MonadIO, liftIO)+import Control.Monad (unless)++bodyReaderSource :: MonadIO m+                 => BodyReader+                 -> Producer m ByteString+bodyReaderSource br =+    loop+  where+    loop = do+        bs <- liftIO $ brRead br+        unless (S.null bs) $ do+            yield bs+            loop++http :: MonadResource m+     => Request+     -> Manager+     -> m (Response (ResumableSource m ByteString))+http req man = do+    (key, res) <- allocate (responseOpen req man) responseClose+    let rsrc = CI.ResumableSource+            (bodyReaderSource $ responseBody res)+            (release key)+    return res { responseBody = rsrc }++requestBodySource :: Int64 -> Source (ResourceT IO) ByteString -> RequestBody+requestBodySource size = RequestBodyStream size . srcToPopper++requestBodySourceChunked :: Source (ResourceT IO) ByteString -> RequestBody+requestBodySourceChunked = RequestBodyStreamChunked . srcToPopper++srcToPopper :: Source (ResourceT IO) ByteString -> GivesPopper ()+srcToPopper src f = runResourceT $ do+    (rsrc0, ()) <- src $$+ return ()+    irsrc <- liftIO $ newIORef rsrc0+    is <- getInternalState+    let popper :: IO ByteString+        popper = do+            rsrc <- readIORef irsrc+            (rsrc', mres) <- runInternalState (rsrc $$++ await) is+            writeIORef irsrc rsrc'+            case mres of+                Nothing -> return S.empty+                Just bs+                    | S.null bs -> popper+                    | otherwise -> return bs+    liftIO $ f popper++requestBodySourceIO :: Int64 -> Source IO ByteString -> RequestBody+requestBodySourceIO size = RequestBodyStream size . srcToPopperIO++requestBodySourceChunkedIO :: Source IO ByteString -> RequestBody+requestBodySourceChunkedIO = RequestBodyStreamChunked . srcToPopperIO++srcToPopperIO :: Source IO ByteString -> GivesPopper ()+srcToPopperIO src f = do+    (rsrc0, ()) <- src $$+ return ()+    irsrc <- newIORef rsrc0+    let popper :: IO ByteString+        popper = do+            rsrc <- readIORef irsrc+            (rsrc', mres) <- rsrc $$++ await+            writeIORef irsrc rsrc'+            case mres of+                Nothing -> return S.empty+                Just bs+                    | S.null bs -> popper+                    | otherwise -> return bs+    f popper
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ http-client-conduit.cabal view
@@ -0,0 +1,22 @@+name:                http-client-conduit+version:             0.2.0.0+synopsis:            Frontend support for using http-client with conduit+description:         Intended for use by higher-level libraries, such as http-conduit.+homepage:            https://github.com/snoyberg/http-client+license:             MIT+license-file:        LICENSE+author:              Michael Snoyman+maintainer:          michael@snoyman.com+category:            Network+build-type:          Simple+cabal-version:       >=1.10++library+  exposed-modules:     Network.HTTP.Client.Conduit+  build-depends:       base >= 4 && < 5+                     , http-client >= 0.2+                     , conduit+                     , resourcet+                     , bytestring+                     , transformers+  default-language:    Haskell2010