WIP: nice but doomed approach to making multipart response
This commit is contained in:
+31
-5
@@ -6,7 +6,7 @@ import Control.Arrow ((***), second)
|
|||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
|
|
||||||
import Data.Text hiding (map)
|
import Data.Text hiding (map)
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe, mapMaybe)
|
||||||
import Text.Regex.TDFA ((=~))
|
import Text.Regex.TDFA ((=~))
|
||||||
import Data.Ord (comparing)
|
import Data.Ord (comparing)
|
||||||
import Data.Ranged.Ranges (emptyRange)
|
import Data.Ranged.Ranges (emptyRange)
|
||||||
@@ -16,6 +16,7 @@ import Data.List (sortBy)
|
|||||||
import Data.Functor.Identity
|
import Data.Functor.Identity
|
||||||
import qualified Data.Set as S
|
import qualified Data.Set as S
|
||||||
import qualified Data.ByteString.Lazy as BL
|
import qualified Data.ByteString.Lazy as BL
|
||||||
|
import qualified Data.ByteString.Builder as BB
|
||||||
import qualified Data.Csv as CSV
|
import qualified Data.Csv as CSV
|
||||||
|
|
||||||
import Network.HTTP.Types.Status
|
import Network.HTTP.Types.Status
|
||||||
@@ -101,7 +102,7 @@ app v1schema reqBody req =
|
|||||||
|
|
||||||
([table], "POST") -> do
|
([table], "POST") -> do
|
||||||
let qt = QualifiedTable schema (cs table)
|
let qt = QualifiedTable schema (cs table)
|
||||||
--echoRequested = lookup "Prefer" hdrs == Just "return=representation"
|
echoRequested = lookup "Prefer" hdrs == Just "return=representation"
|
||||||
parsed :: Either String (V.Vector Text, V.Vector (V.Vector Text))
|
parsed :: Either String (V.Vector Text, V.Vector (V.Vector Text))
|
||||||
parsed = if lookup "Content-Type" hdrs == Just "text/csv"
|
parsed = if lookup "Content-Type" hdrs == Just "text/csv"
|
||||||
then do
|
then do
|
||||||
@@ -115,9 +116,23 @@ app v1schema reqBody req =
|
|||||||
_ -> Left "Expecting single JSON object or CSV rows"
|
_ -> Left "Expecting single JSON object or CSV rows"
|
||||||
case parsed of
|
case parsed of
|
||||||
Left err -> return $ responseLBS status400 [] (cs err)
|
Left err -> return $ responseLBS status400 [] (cs err)
|
||||||
Right records -> do
|
Right toBeInserted -> do
|
||||||
H.unitEx $ uncurry (insertInto qt) records
|
rows :: [Identity Text] <- H.listEx $ uncurry (insertInto qt) toBeInserted
|
||||||
return $ responseLBS status201 [] ""
|
let inserted :: [Object] = mapMaybe (decode . cs . runIdentity) rows
|
||||||
|
primaryKeys <- primaryKeyColumns qt
|
||||||
|
let responses = flip map inserted $ \obj -> do
|
||||||
|
let primaries =
|
||||||
|
if Prelude.null primaryKeys
|
||||||
|
then obj
|
||||||
|
else M.filterWithKey (const . (`elem` primaryKeys)) obj
|
||||||
|
let params = urlEncodeVars
|
||||||
|
$ map (\t -> (cs $ fst t, cs (paramFilter $ snd t)))
|
||||||
|
$ sortBy (comparing fst) $ M.toList primaries
|
||||||
|
responseLBS status201
|
||||||
|
[ jsonH
|
||||||
|
, (hLocation, "/" <> cs table <> "?" <> cs params)
|
||||||
|
] $ if echoRequested then encode obj else ""
|
||||||
|
return $ multipart responses
|
||||||
|
|
||||||
([table], "PUT") ->
|
([table], "PUT") ->
|
||||||
handleJsonObj reqBody $ \obj -> do
|
handleJsonObj reqBody $ \obj -> do
|
||||||
@@ -227,6 +242,17 @@ handleJsonObj reqBody handler = do
|
|||||||
jErr = encode . object $
|
jErr = encode . object $
|
||||||
[("message", String "Expecting a JSON object")]
|
[("message", String "Expecting a JSON object")]
|
||||||
|
|
||||||
|
multipart :: [Response] -> Response
|
||||||
|
multipart rs =
|
||||||
|
undefined
|
||||||
|
|
||||||
|
where
|
||||||
|
renderHeader :: Header -> BL.ByteString
|
||||||
|
renderHeader (k, v) = k <> ": " <> v
|
||||||
|
|
||||||
|
renderResponse (ResponseBuilder _ headers b) =
|
||||||
|
BL.intercalate "\n" $ map renderHeader headers
|
||||||
|
<> BB.toLazyByteString b
|
||||||
|
|
||||||
data TableOptions = TableOptions {
|
data TableOptions = TableOptions {
|
||||||
tblOptcolumns :: [Column]
|
tblOptcolumns :: [Column]
|
||||||
|
|||||||
Reference in New Issue
Block a user