profun/app/Main.hs

39 lines
1.6 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 21:14:54 +02:00
import Data.ByteString.UTF8 (fromString) --to convert acquired API key to ByteString
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 21:14:54 +02:00
generatePlot 20 100 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
2024-05-25 21:14:54 +02:00
--apiKey is stored in an env variable, not something that should be pushed onto git
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"
2024-05-25 21:14:54 +02:00
$ setRequestQueryString [("q", Just "Poznan"), ("key", Just (fromString apiKey))]
2024-05-25 20:21:38 +02:00
$ setRequestPort 443
$ setRequestSecure True
$ defaultRequest
where path
2024-05-25 21:14:54 +02:00
| day == "yesterday" = "/v1/"
| day == "tomorrow" = "/v1/"
| otherwise = "/v1/current.json"