Use accept header version to set schema

This commit is contained in:
Joe Nelson
2014-07-18 00:22:39 -07:00
parent c4550f97b0
commit dd1547adea
4 changed files with 23 additions and 11 deletions
+17 -6
View File
@@ -22,10 +22,10 @@ import qualified Data.ByteString.Char8 as BS
import PgStructure (printTables, printColumns) import PgStructure (printTables, printColumns)
import PgQuery (selectWhere) import PgQuery (selectWhere)
import Debug.Trace import Data.Maybe (fromMaybe)
import qualified Data.Text as T
traceThis :: (Show a) => a -> a import Text.Regex.Posix ((=~))
traceThis x = trace (show x) x import Text.Read (readMaybe)
data AppConfig = AppConfig { data AppConfig = AppConfig {
configDbUri :: String configDbUri :: String
@@ -52,11 +52,11 @@ app :: AppConfig -> Application
app config req respond = do app config req respond = do
r <- try $ r <- try $
case path of case path of
[] -> responseLBS status200 [json] <$> (printTables =<< conn) [] -> responseLBS status200 [json] <$> (printTables ver =<< conn)
[table] -> responseLBS status200 [json] <$> [table] -> responseLBS status200 [json] <$>
( if verb == methodOptions ( if verb == methodOptions
then printColumns table =<< conn then printColumns table =<< conn
else selectWhere table qq =<< conn ) else selectWhere (T.pack $ show ver) table qq =<< conn )
_ -> return $ responseLBS status404 [] "" _ -> return $ responseLBS status404 [] ""
respond $ either sqlErrorHandler id r respond $ either sqlErrorHandler id r
@@ -67,6 +67,17 @@ app config req respond = do
json = (hContentType, "application/json") json = (hContentType, "application/json")
conn = connectPostgreSQL $ configDbUri config conn = connectPostgreSQL $ configDbUri config
qq = queryString req qq = queryString req
ver = fromMaybe 1 $ requestedVersion (requestHeaders req)
requestedVersion :: RequestHeaders -> Maybe Int
requestedVersion hdrs =
case verStr of
Just [[_, ver]] -> readMaybe ver
_ -> Nothing
where verRegex = "version[ ]*=[ ]*([0-9]+)" :: String
accept = BS.unpack <$> lookup hAccept hdrs :: Maybe String
verStr = (=~ verRegex) <$> accept :: Maybe [[String]]
sqlErrorHandler :: SqlError -> Response sqlErrorHandler :: SqlError -> Response
sqlErrorHandler e = sqlErrorHandler e =
+3 -3
View File
@@ -16,8 +16,8 @@ import Database.HDBC.PostgreSQL
import Network.HTTP.Types.URI import Network.HTTP.Types.URI
selectWhere :: T.Text -> Query -> Connection -> IO BL.ByteString selectWhere :: T.Text -> T.Text -> Query -> Connection -> IO BL.ByteString
selectWhere table qq conn = do selectWhere ver table qq conn = do
s <- selectSql s <- selectSql
w <- whereClause conn qq w <- whereClause conn qq
r <- quickQuery conn (BS.unpack $ s <> w) [] r <- quickQuery conn (BS.unpack $ s <> w) []
@@ -29,7 +29,7 @@ selectWhere table qq conn = do
selectSql = pgFormat conn selectSql = pgFormat conn
"select array_to_json(array_agg(row_to_json(t)))\ "select array_to_json(array_agg(row_to_json(t)))\
\ from (select * from %I.%I) t" \ from (select * from %I.%I) t"
[toSql (T.pack "base"), toSql table] [toSql ver, toSql table]
whereClause :: Connection -> Query -> IO BS.ByteString whereClause :: Connection -> Query -> IO BS.ByteString
+2 -2
View File
@@ -97,8 +97,8 @@ columns t conn = do
namedColumnHash :: [Column] -> HashMap String Column namedColumnHash :: [Column] -> HashMap String Column
namedColumnHash = fromList . (Prelude.zip =<< Prelude.map colName) namedColumnHash = fromList . (Prelude.zip =<< Prelude.map colName)
printTables :: Connection -> IO BL.ByteString printTables :: Int -> Connection -> IO BL.ByteString
printTables conn = JSON.encode <$> tables "base" conn printTables schema conn = JSON.encode <$> tables (show schema) conn
printColumns :: T.Text -> Connection -> IO BL.ByteString printColumns :: T.Text -> Connection -> IO BL.ByteString
printColumns table conn = JSON.encode . namedColumnHash <$> columns table conn printColumns table conn = JSON.encode . namedColumnHash <$> columns table conn
+1
View File
@@ -22,5 +22,6 @@ executable dbapi
, bytestring, aeson, network , bytestring, aeson, network
, text, optparse-applicative , text, optparse-applicative
, unordered-containers , unordered-containers
, http-media, regex-posix
-- hs-source-dirs: -- hs-source-dirs:
default-language: Haskell2010 default-language: Haskell2010