git.iko.soy/typeable/servant
on master help sign in
Add `NoFraming` strategy
b80a3e6Jan van Brügge
servant-client/test/Servant/StreamSpec.hs+3−2
@@ -41,7 +41,8 @@ import Test.QuickCheck
41import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
41import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
42 NetstringFraming, NewlineFraming,
42 NetstringFraming, NewlineFraming,
43 OctetStream, ResultStream (..),
43 OctetStream, ResultStream (..),
44− StreamGenerator (..), StreamGet)
44+ StreamGenerator (..), StreamGet,
45+ NoFraming)
45import Servant.Client
46import Servant.Client
46import Servant.ClientSpec (Person (..))
47import Servant.ClientSpec (Person (..))
47import qualified Servant.ClientSpec as CS
48import qualified Servant.ClientSpec as CS
@@ -55,7 +56,7 @@ spec = describe "Servant.Stream" $ do
55type StreamApi f =
56type StreamApi f =
56 "streamGetNewline" :> StreamGet NewlineFraming JSON (f Person)
57 "streamGetNewline" :> StreamGet NewlineFraming JSON (f Person)
57 :<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person)
58 :<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person)
58− :<|> "streamALot" :> StreamGet NewlineFraming OctetStream (f BS.ByteString)
59+ :<|> "streamALot" :> StreamGet NoFraming OctetStream (f BS.ByteString)
59
60
60
61
61capi :: Proxy (StreamApi ResultStream)
62capi :: Proxy (StreamApi ResultStream)
servant/src/Servant/API.hs+2−2
@@ -117,8 +117,8 @@ import Servant.API.Stream
117 (BoundaryStrategy (..), BuildFromStream (..),
117 (BoundaryStrategy (..), BuildFromStream (..),
118 ByteStringParser (..), FramingRender (..),
118 ByteStringParser (..), FramingRender (..),
119 FramingUnrender (..), NetstringFraming, NewlineFraming,
119 FramingUnrender (..), NetstringFraming, NewlineFraming,
120− ResultStream (..), Stream, StreamGenerator (..), StreamGet,
120+ NoFraming, ResultStream (..), Stream, StreamGenerator (..),
121− StreamPost, ToStreamGenerator (..))
121+ StreamGet, StreamPost, ToStreamGenerator (..))
122import Servant.API.Sub
122import Servant.API.Sub
123 ((:>))
123 ((:>))
124import Servant.API.Vault
124import Servant.API.Vault
servant/src/Servant/API/Stream.hs+12
@@ -81,6 +81,18 @@ data ByteStringParser a = ByteStringParser {
81class FramingUnrender strategy a where
81class FramingUnrender strategy a where
82 unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString))
82 unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString))
83
83
84+-- | A framing strategy that does not do any framing at all, it just passes the input data
85+-- This will be used most of the time with binary data, such as files
86+data NoFraming
87+
88+instance FramingRender NoFraming a where
89+ header _ _ = empty
90+ boundary _ _ = BoundaryStrategyGeneral id
91+ trailer _ _ = empty
92+
93+instance FramingUnrender NoFraming a where
94+ unrenderFrames _ _ = ByteStringParser (Just . (go,)) (go,)
95+ where go = ByteStringParser (Just . (, empty) . Right) ((, empty) . Right)
84
96
85-- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame.
97-- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame.
86-- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON).
98-- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON).