add ByteString handling

This commit is contained in:
Szymon Szczubkowski 2024-05-25 21:14:54 +02:00
parent 2e7e1dd9a0
commit 56c2235925
3 changed files with 12 additions and 6 deletions

View File

@ -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)

View File

@ -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"

View File

@ -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