Add NoFraming strategy

This commit is contained in:
Jan van Brügge 2018-05-23 11:28:12 +02:00
parent 0ba09c999b
commit b80a3e6279
3 changed files with 17 additions and 4 deletions

View file

@ -41,7 +41,8 @@ import Test.QuickCheck
import Servant.API ((:<|>) ((:<|>)), (:>), JSON, import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
NetstringFraming, NewlineFraming, NetstringFraming, NewlineFraming,
OctetStream, ResultStream (..), OctetStream, ResultStream (..),
StreamGenerator (..), StreamGet) StreamGenerator (..), StreamGet,
NoFraming)
import Servant.Client import Servant.Client
import Servant.ClientSpec (Person (..)) import Servant.ClientSpec (Person (..))
import qualified Servant.ClientSpec as CS import qualified Servant.ClientSpec as CS
@ -55,7 +56,7 @@ spec = describe "Servant.Stream" $ do
type StreamApi f = type StreamApi f =
"streamGetNewline" :> StreamGet NewlineFraming JSON (f Person) "streamGetNewline" :> StreamGet NewlineFraming JSON (f Person)
:<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person) :<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person)
:<|> "streamALot" :> StreamGet NewlineFraming OctetStream (f BS.ByteString) :<|> "streamALot" :> StreamGet NoFraming OctetStream (f BS.ByteString)
capi :: Proxy (StreamApi ResultStream) capi :: Proxy (StreamApi ResultStream)

View file

@ -117,8 +117,8 @@ import Servant.API.Stream
(BoundaryStrategy (..), BuildFromStream (..), (BoundaryStrategy (..), BuildFromStream (..),
ByteStringParser (..), FramingRender (..), ByteStringParser (..), FramingRender (..),
FramingUnrender (..), NetstringFraming, NewlineFraming, FramingUnrender (..), NetstringFraming, NewlineFraming,
ResultStream (..), Stream, StreamGenerator (..), StreamGet, NoFraming, ResultStream (..), Stream, StreamGenerator (..),
StreamPost, ToStreamGenerator (..)) StreamGet, StreamPost, ToStreamGenerator (..))
import Servant.API.Sub import Servant.API.Sub
((:>)) ((:>))
import Servant.API.Vault import Servant.API.Vault

View file

@ -81,6 +81,18 @@ data ByteStringParser a = ByteStringParser {
class FramingUnrender strategy a where class FramingUnrender strategy a where
unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString)) unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString))
-- | A framing strategy that does not do any framing at all, it just passes the input data
-- This will be used most of the time with binary data, such as files
data NoFraming
instance FramingRender NoFraming a where
header _ _ = empty
boundary _ _ = BoundaryStrategyGeneral id
trailer _ _ = empty
instance FramingUnrender NoFraming a where
unrenderFrames _ _ = ByteStringParser (Just . (go,)) (go,)
where go = ByteStringParser (Just . (, empty) . Right) ((, empty) . Right)
-- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame. -- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame.
-- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON). -- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON).