Implements CSV resnponse for the appropriate accept headers

This commit is contained in:
Diogo Biazus
2015-08-14 11:04:26 -04:00
parent ca40ba1fda
commit 9d5011e864
4 changed files with 40 additions and 6 deletions
+9 -2
View File
@@ -62,11 +62,12 @@ app conf reqBody req =
then return $ responseLBS status416 [] "HTTP Range error"
else do
let qt = qualify table
from = fromMaybe 0 $ rangeOffset <$> range
select = B.Stmt "select " V.empty True <>
parentheticT (
whereT qt qq $ countRows qt
) <> commaq <> (
asJsonWithCount
bodyForAccept accept qt
. limitT range
. orderT (orderParse qq)
. whereT qt qq
@@ -75,7 +76,6 @@ app conf reqBody req =
row <- H.maybeEx select
let (tableTotal, queryTotal, body) =
fromMaybe (0, 0, Just "" :: Maybe Text) row
from = fromMaybe 0 $ rangeOffset <$> range
to = from+queryTotal-1
contentRange = contentRangeH from to tableTotal
status = rangeStatus from to tableTotal
@@ -233,6 +233,7 @@ app conf reqBody req =
range = rangeRequested hdrs
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
lookupHeader = flip lookup hdrs
accept = lookupHeader hAccept
sqlError :: t
sqlError = undefined
@@ -246,6 +247,12 @@ rangeStatus from to total
| (1 + to - from) < total = status206
| otherwise = status200
bodyForAccept :: Maybe BS.ByteString -> QualifiedTable -> StatementT
bodyForAccept accept table =
case accept of
Just "text/csv" -> asCsvWithCount table
_ -> asJsonWithCount -- defaults to JSON
contentRangeH :: Int -> Int -> Int -> Header
contentRangeH from to total =
("Content-Range",