plotserver-api 0.21 → 0.22
raw patch · 2 files changed
+22/−10 lines, 2 filesdep ~base
Dependency ranges changed: base
Files
- Plotserver/Api.hs +20/−8
- plotserver-api.cabal +2/−2
Plotserver/Api.hs view
@@ -15,36 +15,48 @@ plotUrl (PlotConfig {server}) dataset = server ++ "/" ++ dataset plotCat :: PlotConfig -> String -> IO (Either String PlotData)-plotCat config dataset = response2either <$> curlGetResponse_ url_ opts where+plotCat config dataset = parseResponse <$> curlGetResponse_ url_ opts where url_ = actionUrl config dataset "download" opts = defaultOpts config+ -- TODO write real Maybe!+ parseResponse = response2either $ Just . (read :: (String -> PlotData)) plotUpdate :: PlotConfig -> String -> PlotDataRow -> IO (Either String PlotData)-plotUpdate config dataset row = response2either <$> curlGetResponse_ url_ opts where+plotUpdate config dataset row = parseResponse <$> curlGetResponse_ url_ opts where postData = show $ PlotData [row] url_ = actionUrl config dataset "update" opts = defaultOpts config ++ [ CurlPost True, CurlPostFields [postData] ]+ parseResponse = response2either $ matchString2MaybePlotdata "File written." plotDelete :: PlotConfig -> String -> IO (Either String PlotData)-plotDelete config dataset = response2either <$> curlGetResponse_ url_ opts where+plotDelete config dataset = parseResponse <$> curlGetResponse_ url_ opts where url_ = actionUrl config dataset "delete" opts = defaultOpts config+ parseResponse = response2either $ matchString2MaybePlotdata "File deleted." ------ helpers ------ +matchString2MaybePlotdata :: String -> String -> Maybe PlotData+matchString2MaybePlotdata expected real = if real == expected+ then Just $ PlotData []+ else Nothing+ defaultOpts :: PlotConfig -> [CurlOption] defaultOpts PlotConfig {username, password} = [- CurlUserPwd (username ++ ":" ++ password)+ CurlUserPwd (username ++ ":" ++ password),+ CurlFollowLocation True ] actionUrl :: PlotConfig -> String -> String -> String actionUrl config dataset action = plotUrl config dataset ++ "?" ++ action -response2either :: CurlResponse_ [(String, String)] String -> Either String PlotData-response2either response - | respStatus response == 200 = Right $ (read (respBody response) :: PlotData)+response2either :: (String -> Maybe PlotData) -> CurlResponse_ [(String, String)] String -> Either String PlotData+response2either parser response+ | respStatus response == 200 = case parser (respBody response) of+ Just res -> Right res+ Nothing -> Left $ "Parse error: " ++ (respBody response) | otherwise = Left err where- err = "Error: " ++ show (respStatus response)+ err = "Curl error: " ++ show (respStatus response) ++ " " ++ (respStatusLine response)
plotserver-api.cabal view
@@ -1,5 +1,5 @@ Name: plotserver-api-Version: 0.21+Version: 0.22 Synopsis: Plotserver API Description: A lightweight API for Prezi's opensourced Plotserver (https:\/\/github.com\/prezi\/plotserver) License: MIT@@ -15,7 +15,7 @@ Location: https://github.com/dtorok/plotserver-hsapi Library- Build-Depends: base >= 3 && < 5,+ Build-Depends: base >= 4 && < 5, curl, split Exposed-modules: Plotserver.Api, Plotserver.Types