-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSources.hs
44 lines (34 loc) · 1.32 KB
/
Sources.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# LANGUAGE BangPatterns #-}
module Sources where
import qualified Data.ByteString.Streaming.HTTP as H
import Data.ByteString.Streaming.HTTP (MonadResource(..))
import Streaming
import qualified Streaming.Zip as Zip
import qualified Streaming.ByteString as BS
-- * Unprocessed sources
-- | Name-standardized alias for 'BS.stdin'
getStdin :: MonadIO m => BS.ByteStream m ()
getStdin = BS.stdin
{-# INLINE getStdin #-}
-- | Monadic computation to procure a 'BS.ByteString' over http(s)
getHttp :: (MonadIO m, MonadResource m)
=> String -- ^ URL to read data from
-> m (BS.ByteStream m ())
getHttp !url = do
req <- liftIO $ H.parseRequest url
man <- liftIO $ H.newManager H.tlsManagerSettings
resp <- H.http req man
return $ H.responseBody resp
{-# INLINE getHttp #-}
-- * Unzipping
-- | Alias for 'Zip.gunzip'
unzip :: MonadIO m => BS.ByteStream m () -> BS.ByteStream m ()
unzip = Zip.gunzip
{-# INLINE unzip #-}
-- | Performs conditional decompression of a monadic 'BS.ByteString'
condUnzip :: MonadIO m
=> Bool -- ^ Indicates whether input is compressed (performs decompression if @True@)
-> BS.ByteStream m () -- ^ Possibly compressed bytestring
-> BS.ByteStream m () -- ^ Non-compressed bytestring
condUnzip !b = if b then Zip.gunzip else id
{-# INLINE condUnzip #-}