weather-api 0.0.1 → 0.1.0
raw patch · 3 files changed
+16/−7 lines, 3 filesdep +utf8-string
Dependencies added: utf8-string
Files
- WeatherApi.hs +6/−1
- WeatherApi/Google.hs +8/−5
- weather-api.cabal +2/−1
WeatherApi.hs view
@@ -30,6 +30,8 @@ module WeatherApi (WeatherApiHandler(..) ,Config(..) ,Weather(..)+ ,ApiError(..)+ ,ApiResponse(..) ,getWeather ,getWeather' ,mkWeatherHandler@@ -43,7 +45,7 @@ , apiPort :: Int , queryFun :: HandleStream String -> String ->- IO (Either String Weather)+ IO (ApiResponse) } data WeatherApiHandler = WeatherApiHandler@@ -56,6 +58,9 @@ , windCondition :: String , condition :: String } deriving (Eq, Show)++data ApiError = NotFoundError String | NetworkError String+type ApiResponse = Either ApiError Weather mkWeatherHandler c@(Config apiHost apiPort queryFun) = WeatherApiHandler { stream = openStream apiHost apiPort
WeatherApi/Google.hs view
@@ -6,6 +6,8 @@ import Network.URI import WeatherApi import Control.Monad (liftM)+import Codec.Binary.UTF8.String+ apiUrl = "http://www.google.com/ig/api?" type Lang = String@@ -15,7 +17,7 @@ initApi :: Lang -> Enc -> Config initApi lang enc = let params = [("hl", lang), ("oe", enc)]- urn c = urlEncodeVars $ params ++ [("weather", c)]+ urn c = urlEncodeVars $ params ++ [("weather", encodeString c)] in Config { apiHost = "www.google.com" , apiPort = 80 , queryFun = makeQueryFun urn@@ -23,14 +25,14 @@ retrieve s urn = case parseURI $ apiUrl ++ urn of- Nothing -> return $ Left "Invalid URL"+ Nothing -> return $ Left $ NetworkError "Invalid URL" Just uri -> get s uri get s uri = do eresp <- sendHTTP s (Request uri GET [] "") case eresp of- Left err -> return $ Left $ show err+ Left err -> return $ Left $ NetworkError $ show err Right res -> return $ Right $ rspBody res atTag tag = deep (isElem >>> hasName tag)@@ -56,13 +58,14 @@ ] -- | This return function witch will actualy retrieve and parse weather from stream+makeQueryFun :: (String -> String) -> (HandleStream String) -> String -> IO ApiResponse makeQueryFun q stream city = do resp <- retrieve stream $ q city case liftM parseXML resp of- Left a -> return $ Left a+ Left a -> return $ Left a Right a -> do r <- runX(a >>> parseWeather) case r of- [] -> return $ Left "can't retrieve weather"+ [] -> return $ Left $ NotFoundError "can't retrieve weather" (x:xs) -> return $ Right x
weather-api.cabal view
@@ -1,5 +1,5 @@ Name: weather-api-Version: 0.0.1+Version: 0.1.0 Synopsis: Weather api implemented in haskell Description: This library implement generic api for retrieving weather@@ -20,6 +20,7 @@ ,network == 2.3.* ,hxt == 9.2.* ,HTTP == 4000.2.*+ ,utf8-string == 0.3.7 source-repository head type: git