{-# LANGUAGE OverloadedStrings #-} --needed for ByteString arguments import Network.HTTP.Simple import System.Environment (lookupEnv) import Plot main :: IO () main = do apiKey <- getWeatherKey let todayRequest = apiRequestBuilder apiKey "today" response <- httpJSON todayRequest :: IO (Response ()) generatePlot 20 30 25 --example putStrLn $ show todayRequest putStrLn $ show $ getResponseStatusCode response --apiResponse <- httpJSON "http://httpbin.org/get" :: IO (Response ()) -- specifying type as httpJSON return value is ambigious getWeatherKey :: IO String getWeatherKey = do result <- lookupEnv "WEATHER_API_KEY" case result of Just a -> return a Nothing -> error "API key not set in environmental variables!" -- exception thrown (but not handled) per project requirement apiRequestBuilder :: String -> String -> Request apiRequestBuilder apiKey day = setRequestHost "api.weatherapi.com" $ setRequestPath path $ setRequestMethod "GET" $ setRequestQueryString [("q", Just "Poznan"), ("key", Just "apiKey")] $ setRequestPort 443 $ setRequestSecure True $ defaultRequest where path | day == "today" = "/v1/current.json" | otherwise = "/d"