From 1f1f0bd8b811740c84215f9ed4fa5ebd8309a990 Mon Sep 17 00:00:00 2001 From: slotThe Date: Thu, 9 Jan 2020 15:54:47 +0100 Subject: Use a single Manager across the whole application --- src/Xmobar/Plugins/Monitors/UVMeter.hs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/Xmobar/Plugins/Monitors/UVMeter.hs') diff --git a/src/Xmobar/Plugins/Monitors/UVMeter.hs b/src/Xmobar/Plugins/Monitors/UVMeter.hs index 079177f..3756856 100644 --- a/src/Xmobar/Plugins/Monitors/UVMeter.hs +++ b/src/Xmobar/Plugins/Monitors/UVMeter.hs @@ -18,14 +18,14 @@ module Xmobar.Plugins.Monitors.UVMeter where import Xmobar.Plugins.Monitors.Common import qualified Control.Exception as CE -import Network.HTTP.Conduit - (parseRequest, newManager, tlsManagerSettings, httpLbs, - responseBody) +import Network.HTTP.Conduit (httpLbs, parseRequest, responseBody) import Data.ByteString.Lazy.Char8 as B import Text.Read (readMaybe) import Text.Parsec import Text.Parsec.String import Control.Monad (void) +import Control.Monad.Reader (asks) +import Data.IORef (readIORef) uvConfig :: IO MConfig @@ -40,16 +40,18 @@ newtype UvInfo = UV { index :: String } uvURL :: String uvURL = "https://uvdata.arpansa.gov.au/xml/uvvalues.xml" -getData :: IO String -getData = - CE.catch (do request <- parseRequest uvURL - manager <- newManager tlsManagerSettings - res <- httpLbs request manager - return $ B.unpack $ responseBody res) - errHandler - where errHandler - :: CE.SomeException -> IO String - errHandler _ = return "" +-- | Get the UV data from the given url. +getData :: Monitor String +getData = do + man <- io =<< readIORef <$> asks manager + io $ CE.catch + (do request <- parseRequest uvURL + res <- httpLbs request man + return $ B.unpack $ responseBody res) + errHandler + where + errHandler :: CE.SomeException -> IO String + errHandler _ = return "" textToXMLDocument :: String -> Either ParseError [XML] textToXMLDocument = parse document "" @@ -73,7 +75,7 @@ getUVRating _ [] = Nothing runUVMeter :: [String] -> Monitor String runUVMeter [] = return "N.A." runUVMeter (s:_) = do - resp <- io getData + resp <- getData case textToXMLDocument resp of Right doc -> formatUVRating (getUVRating s doc) Left _ -> getConfigValue naString -- cgit v1.2.3