2024-05-25 20:21:38 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-} --needed for ByteString arguments
|
2024-05-25 14:32:43 +02:00
|
|
|
import Network.HTTP.Simple
|
2024-05-25 20:21:38 +02:00
|
|
|
import System.Environment (lookupEnv)
|
2024-05-25 20:00:14 +02:00
|
|
|
import Plot
|
|
|
|
|
2024-05-25 14:32:43 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2024-05-25 20:21:38 +02:00
|
|
|
apiKey <- getWeatherKey
|
|
|
|
let todayRequest = apiRequestBuilder apiKey "today"
|
|
|
|
response <- httpJSON todayRequest :: IO (Response ())
|
2024-05-25 20:25:24 +02:00
|
|
|
generatePlot 20 30 25 --example
|
2024-05-25 20:21:38 +02:00
|
|
|
putStrLn $ show todayRequest
|
|
|
|
putStrLn $ show $ getResponseStatusCode response
|
|
|
|
--apiResponse <- httpJSON "http://httpbin.org/get" :: IO (Response ()) -- specifying type as httpJSON return value is ambigious
|
2024-05-25 14:32:43 +02:00
|
|
|
|
2024-05-25 20:21:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
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"
|