packages feed

grapesy-1.2.0: src/Network/GRPC/Util/ClientStream.hs

module Network.GRPC.Util.ClientStream (
    -- ** Client API
    clientInputStream,
    clientOutputStream,
) where

import Network.GRPC.Util.Stream

import Network.HTTP.Semantics.Client qualified as Client
import Network.HTTP.Semantics (OutBodyIface)
import Network.HTTP.Semantics qualified as HTTP

import Network.GRPC.Util.HeaderTable (fromHeaderTable)

{-------------------------------------------------------------------------------
  Client API
-------------------------------------------------------------------------------}

clientInputStream :: Client.Response -> IO InputStream
clientInputStream resp = do
    return InputStream {
        _getChunk =
           wrapServerDisconnected $
             Client.getResponseBodyChunk' resp
      , _getTrailers =
           wrapServerDisconnected $
             maybe [] fromHeaderTable <$> Client.getResponseTrailers resp
      }

-- | Construct a client 'OutputStream'
--
-- We do not wrap the members of the 'OutputStream' with
-- 'wrapStreamExceptionsWith', since we do this around the entire
-- 'sendMessageLoop'. See the comment for @outboundThread@ in
-- 'Network.GRPC.Util.Session.Client.setupRequestChannel'.
clientOutputStream :: OutBodyIface -> IO OutputStream
clientOutputStream iface =
    return OutputStream {
        _writeChunk = \c ->
          HTTP.outBodyPush iface c
      , _writeChunkFinal = \c ->
          HTTP.outBodyPushFinal iface c
      , _flush =
          HTTP.outBodyFlush iface
      }