hreq-conduit: Conduit streaming support for Hreq.

[ conduit, library, mit, network, web ] [ Propose Tags ] [ Report a vulnerability ]

Conduit streaming support for Hreq an HTTP client library.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.10.1 && <5), bytestring (>=0.10.8 && <0.11), conduit (>=1.3.1 && <1.4), exceptions (>=0.10.0 && <0.11), hreq-client (>=0.1.0), hreq-core (>=0.1.0), http-client (>=0.6.4 && <0.7), http-types (>=0.12.3 && <0.13), mtl (>=2.2.2 && <3.0), retry (>=0.8 && <0.9), unliftio-core (>=0.1.2 && <0.2.0) [details]
Tested with ghc ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.1
License MIT
Copyright 2019 Lukwago Allan
Author Lukwago Allan <epicallan.al@gmail>
Maintainer Lukwago Allan <epicallan.al@gmail>
Category Network, Web, Conduit
Home page https://github.com/epicallan/hreq/blob/master/hreq-conduit/README.md
Bug tracker https://github.com/epicallan/hreq.git/issues
Source repo head: git clone https://github.com/epicallan/hreq.git
Uploaded by epicallan at 2019-11-13T08:29:28Z
Distributions
Downloads 427 total (8 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for hreq-conduit-0.1.0.0

[back to package description]

Hreq

Hackage MIT license Build status

Implementation of Hreq client as an HTTP Conduit streaming client basing on hreq-core. More streaming backends can be added in the future depending on community interest.

Please look at the repository README.md file for more information.

Streaming Example

{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes        #-}
{-# LANGUAGE TypeApplications  #-}
{-# LANGUAGE TypeOperators     #-}

import Conduit
import Data.Functor (void)
import qualified Data.Text as T

import Hreq.Conduit

main' :: IO ()
main' = void $ do
  runHttpBin streamResponse
  streamFile "README.md"

runHttpBin :: Hreq IO a -> IO a
runHttpBin action = runHreq baseUrl action
  where
    baseUrl = HttpsDomain "httpbin.org"

-- | Stream data from an endpoint and write it into a temporary file
streamResponse :: RunConduitClient m => m ()
streamResponse =
  hreqWithConduit
   @("stream-bytes" :> Capture Int :> StreamGet)
    (size :. Empty)
    $ \ src -> void $ runConduitRes $ src .| sinkSystemTempFile "hreq.json"
  where
    size = 3 * 1024 * 1024 -- amount of data to stream in MBs

-- | stream data from a file and send it as a Request body stream over the network.
streamFile :: String -> IO Response
streamFile fp =
  withSourceFile fp $
     \srcFile -> do
        let src :: ReqBodySource
            src = ReqBodySource
                  $ srcFile
                  .| decodeUtf8C
                  .| mapC T.toUpper
                  .| encodeUtf8C

        runHttpBin $ streamReq src
  where
    streamReq :: RunClient m => ReqBodySource -> m Response
    streamReq src = hreq @("post" :> ConduitReqBody :> RawResponse POST) (src :. Empty)

Documentation

This README is tested by markdown-unlit to make sure the code builds. To keep that happy, we do need a main in this file, so ignore the following :)

main :: IO ()
main = pure ()