From 56c2235925a4a5f06bc8a7ec715924eb5f37e9de Mon Sep 17 00:00:00 2001 From: Szymon Szczubkowski Date: Sat, 25 May 2024 21:14:54 +0200 Subject: [PATCH] add ByteString handling --- README.md | 2 ++ app/Main.hs | 12 +++++++----- profun.cabal | 4 +++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 180d26a..72bbb6c 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,5 @@ This project makes use of the following dependencies (specified in `profun.cabal * aeson (JSON parsing) * Chart * Chart-diagrams +* bytestring (for ByteString type) +* utf8-string (for conversion from String to ByteString) diff --git a/app/Main.hs b/app/Main.hs index 40dfa63..809576d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,6 +2,7 @@ import Network.HTTP.Simple import System.Environment (lookupEnv) import Plot +import Data.ByteString.UTF8 (fromString) --to convert acquired API key to ByteString main :: IO () @@ -9,13 +10,13 @@ main = do apiKey <- getWeatherKey let todayRequest = apiRequestBuilder apiKey "today" response <- httpJSON todayRequest :: IO (Response ()) - generatePlot 20 30 25 --example + generatePlot 20 100 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 - +--apiKey is stored in an env variable, not something that should be pushed onto git getWeatherKey :: IO String getWeatherKey = do result <- lookupEnv "WEATHER_API_KEY" @@ -28,10 +29,11 @@ apiRequestBuilder apiKey day = setRequestHost "api.weatherapi.com" $ setRequestPath path $ setRequestMethod "GET" - $ setRequestQueryString [("q", Just "Poznan"), ("key", Just "apiKey")] + $ setRequestQueryString [("q", Just "Poznan"), ("key", Just (fromString apiKey))] $ setRequestPort 443 $ setRequestSecure True $ defaultRequest where path - | day == "today" = "/v1/current.json" - | otherwise = "/d" + | day == "yesterday" = "/v1/" + | day == "tomorrow" = "/v1/" + | otherwise = "/v1/current.json" \ No newline at end of file diff --git a/profun.cabal b/profun.cabal index aff905f..d6f370a 100644 --- a/profun.cabal +++ b/profun.cabal @@ -70,7 +70,9 @@ executable profun http-conduit ^>=2.3.8.3, Chart ^>=1.9.5, Chart-diagrams ^>=1.9.5, - aeson ^>=2.2.2.0 + aeson ^>=2.2.2.0, + bytestring ^>=0.12.1.0, + utf8-string ^>=1.0.2 -- Directories containing source files. hs-source-dirs: app