profun/app/Main.hs

38 lines
1.3 KiB
Haskell
Raw Normal View History

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)
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 ())
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"