module WeatherApi (WeatherApiHandler(..)
,Config(..)
,Weather(..)
,ApiError(..)
,ApiResponse(..)
,getWeather
,getWeather'
,mkWeatherHandler
,isHandlerAlive
,closeHandler) where
import System.IO.Error
import Network.HTTP
import Network.URI
data Config = Config { apiHost :: String
, apiPort :: Int
, queryFun :: HandleStream String ->
String ->
IO (ApiResponse)
}
data WeatherApiHandler = WeatherApiHandler
{ stream :: IO (HandleStream String)
, config :: Config
}
data Weather = Weather { tempF, tempC :: Double
, humidity :: String
, windCondition :: String
, condition :: String
} deriving (Eq, Show)
data ApiError = NotFoundError String
| NetworkError String
| ParseError String
deriving Show
type ApiResponse = Either ApiError Weather
mkWeatherHandler c@(Config apiHost apiPort queryFun) =
WeatherApiHandler { stream = openStream apiHost apiPort
, config = c
}
isHandlerAlive (WeatherApiHandler stream (Config h p _)) =
stream >>= \s -> isTCPConnectedTo s (EndPoint h p)
closeHandler (WeatherApiHandler stream _) = stream >>= closeQuick
getWeather (WeatherApiHandler stream c) city =
stream >>= \s -> queryFun c s city
getWeather' (Config apiHost apiPort queryFun) city = do
catchIOError (openStream apiHost apiPort >>= \s -> queryFun s city)
(\e -> return $ Left $ NetworkError $ show e)