From d0e391fb165ab23bb08dc02871747e0131c1e723 Mon Sep 17 00:00:00 2001 From: Dragos Boca Date: Tue, 25 Aug 2015 13:37:23 +0300 Subject: define flag 'with_conduit' --- src/Plugins/Monitors/Weather.hs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'src/Plugins') diff --git a/src/Plugins/Monitors/Weather.hs b/src/Plugins/Monitors/Weather.hs index 5f5fb6f..d458279 100644 --- a/src/Plugins/Monitors/Weather.hs +++ b/src/Plugins/Monitors/Weather.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Plugins.Monitors.Weather @@ -18,12 +19,14 @@ import Plugins.Monitors.Common import qualified Control.Exception as CE +#ifdef HTTP_CONDUIT import Network.HTTP.Conduit import Network.HTTP.Types.Status import Network.HTTP.Types.Method - ---import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy.Char8 as B +#else +import Network.HTTP +#endif import Text.ParserCombinators.Parsec @@ -177,7 +180,8 @@ defUrl = "http://weather.noaa.gov/pub/data/observations/metar/decoded/" stationUrl :: String -> String stationUrl station = defUrl ++ station ++ ".TXT" -getData:: String -> IO String +getData :: String -> IO String +#ifdef HTTP_CONDUIT getData station = CE.catch (do manager <- newManager tlsManagerSettings request <- parseUrl $ stationUrl station @@ -186,6 +190,13 @@ getData station = CE.catch (do ) errHandler where errHandler :: CE.SomeException -> IO String errHandler _ = return "" +#else +getData station = do + let request = getRequest (stationUrl station) + CE.catch (simpleHTTP request >>= getResponseBody) errHandler + where errHandler :: CE.IOException -> IO String + errHandler _ = return "" +#endif formatWeather :: [WeatherInfo] -> Monitor String formatWeather [WI st ss y m d h (WindInfo wc wa wm wk) v sk tC tF dC dF r p] = @@ -201,6 +212,7 @@ runWeather str = formatWeather i weatherReady :: [String] -> Monitor Bool +#ifdef HTTP_CONDUIT weatherReady str = do initRequest <- parseUrl $ stationUrl $ head str let request = initRequest{method = methodHead} @@ -214,3 +226,21 @@ weatherReady str = do | statusIsServerError status = False | statusIsClientError status = False | otherwise = True +#else +weatherReady str = do + let station = head str + request = headRequest (stationUrl station) + io $ CE.catch (simpleHTTP request >>= checkResult) errHandler + where errHandler :: CE.IOException -> IO Bool + errHandler _ = return False + checkResult result = + case result of + Left _ -> return False + Right response -> + case rspCode response of + -- Permission or network errors are failures; anything + -- else is recoverable. + (4, _, _) -> return False + (5, _, _) -> return False + (_, _, _) -> return True +#endif -- cgit v1.2.3