]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Utils/Servant.hs
Merge branch 'client-executable' of ssh://gitlab.iscpif.fr:20022/gargantext/haskell...
[gargantext.git] / src / Gargantext / Utils / Servant.hs
1 module Gargantext.Utils.Servant where
2
3 import qualified Data.ByteString.Lazy.Char8 as BSC
4 import Data.Csv (encodeDefaultOrderedByName, DefaultOrdered, ToNamedRecord)
5 import Network.HTTP.Media ((//), (/:))
6 import qualified Prelude as Prelude
7 import Protolude
8 import Protolude.Partial (read)
9 import Servant
10
11 data CSV = CSV
12
13 instance Accept CSV where
14 contentType _ = "text" // "csv" /: ("charset", "utf-8")
15
16 instance (DefaultOrdered a, ToNamedRecord a) => MimeRender CSV [a] where
17 mimeRender _ val = encodeDefaultOrderedByName val
18
19 instance Read a => MimeUnrender CSV a where
20 mimeUnrender _ bs = case BSC.take len bs of
21 "text/csv" -> return . read . BSC.unpack $ BSC.drop len bs
22 _ -> Left "didn't start with the magic incantation"
23 where
24 len :: Int64
25 len = fromIntegral $ length ("text/csv" :: Prelude.String)
26
27 --instance ToNamedRecord a => MimeRender CSV [a] where
28 -- mimeRender _ val = encode val