decoding works for each day

This commit is contained in:
K4RP4T 2024-05-26 16:57:22 +02:00
parent 2be73d329d
commit d2fb17df88
2 changed files with 28 additions and 43 deletions

View File

@ -23,12 +23,6 @@ data Current = Current
} deriving (Show, Generic)
data TodayResponse = TodayResponse
{ location :: Location
, current :: Current
} deriving (Show, Generic)
data Day = Day
{ avgtemp_c :: Float
} deriving (Show, Generic)
@ -44,39 +38,29 @@ data Forecast = Forecast
} deriving (Show, Generic)
-- data YesterdayResponse = YesterdayResponse
-- { location :: Location
-- , forecast :: Forecast
-- } deriving (Show, Generic)
data WeatherResponse = WeatherResponse
{ location :: Location
, current :: Maybe Current
, forecast :: Maybe Forecast
} deriving (Show, Generic)
instance FromJSON Location
instance ToJSON Location
instance FromJSON Current
instance ToJSON Current
instance FromJSON TodayResponse
instance ToJSON TodayResponse
instance FromJSON Day
instance ToJSON Day
instance FromJSON ForecastDay
instance ToJSON ForecastDay
instance FromJSON Forecast
instance ToJSON Forecast
-- instance FromJSON YesterdayResponse
-- instance ToJSON YesterdayResponse
instance FromJSON WeatherResponse
instance ToJSON WeatherResponse
todayDecode :: BL.ByteString -> IO TodayResponse
todayDecode jsonBody = do
let decoded = decode jsonBody :: Maybe TodayResponse
case decoded of
weatherDecode :: BL.ByteString -> IO WeatherResponse
weatherDecode jsonBody =
case decode jsonBody :: Maybe WeatherResponse of
Just decoded -> return decoded
Nothing -> error "Invalid JSON"
-- yesterdayDecode :: BL.ByteString -> IO YesterdayResponse
-- yesterdayDecode jsonBody = do
-- let decoded = decode jsonBody :: Maybe YesterdayResponse
-- case decoded of
-- Just decoded -> return decoded
-- Nothing -> error "Invalid JSON"

View File

@ -5,7 +5,7 @@ import Data.ByteString.UTF8 (fromString) --to convert acquired API key to ByteSt
import Data.Time as T
import Plot
import DataTypes
import qualified DataTypes as DT
data WhichDay = Yesterday | Today | Tomorrow
@ -28,29 +28,30 @@ main = do
then error "City not found!"
else do
currentDate <- getCurrentDate
--request for yesterday's weather
let yesterdayDate = formatDate $ addDays (-1) currentDate
let yesterdayRequest = apiRequestBuilder apiKey Yesterday city yesterdayDate
yesterdayResponse <- httpLBS yesterdayRequest
print $ getResponseStatusCode yesterdayResponse
yesterdayDecoded <- DT.weatherDecode $ getResponseBody yesterdayResponse
print yesterdayDecoded
--request for today's weather
let todayDate = formatDate currentDate
let tomorrowDate = formatDate $ addDays 1 currentDate
-- let yesterdayRequest = apiRequestBuilder apiKey Yesterday city yesterdayDate
-- yesterdayResponse <- httpLBS yesterdayRequest
-- print $ getResponseStatusCode yesterdayResponse
-- yesterdayDecoded <- yesterdayDecode $ getResponseBody yesterdayResponse
-- print yesterdayDecoded
let todayRequest = apiRequestBuilder apiKey Today city todayDate
todayResponse <- httpLBS todayRequest
print $ getResponseStatusCode todayResponse
todayDecoded <- todayDecode $ getResponseBody todayResponse
todayDecoded <- DT.weatherDecode $ getResponseBody todayResponse
print todayDecoded
-- let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate
-- tomorrowResponse <- httpLBS tomorrowRequest
-- print $ getResponseStatusCode tomorrowResponse
--apiResponse <- httpJSON "http://httpbin.org/get" :: IO (Response ()) -- specifying type as httpJSON return value is ambigious
--request for tomorrow's weather
let tomorrowDate = formatDate $ addDays 1 currentDate
let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate
tomorrowResponse <- httpLBS tomorrowRequest
print $ getResponseStatusCode tomorrowResponse
tomorrowDecoded <- DT.weatherDecode $ getResponseBody tomorrowResponse
print tomorrowDecoded
--apiKey is stored in an env variable, not something that should be pushed onto git