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) } deriving (Show, Generic)
data TodayResponse = TodayResponse
{ location :: Location
, current :: Current
} deriving (Show, Generic)
data Day = Day data Day = Day
{ avgtemp_c :: Float { avgtemp_c :: Float
} deriving (Show, Generic) } deriving (Show, Generic)
@ -44,39 +38,29 @@ data Forecast = Forecast
} deriving (Show, Generic) } deriving (Show, Generic)
-- data YesterdayResponse = YesterdayResponse data WeatherResponse = WeatherResponse
-- { location :: Location { location :: Location
-- , forecast :: Forecast , current :: Maybe Current
-- } deriving (Show, Generic) , forecast :: Maybe Forecast
} deriving (Show, Generic)
instance FromJSON Location instance FromJSON Location
instance ToJSON Location instance ToJSON Location
instance FromJSON Current instance FromJSON Current
instance ToJSON Current instance ToJSON Current
instance FromJSON TodayResponse
instance ToJSON TodayResponse
instance FromJSON Day instance FromJSON Day
instance ToJSON Day instance ToJSON Day
instance FromJSON ForecastDay instance FromJSON ForecastDay
instance ToJSON ForecastDay instance ToJSON ForecastDay
instance FromJSON Forecast instance FromJSON Forecast
instance ToJSON Forecast instance ToJSON Forecast
-- instance FromJSON YesterdayResponse instance FromJSON WeatherResponse
-- instance ToJSON YesterdayResponse instance ToJSON WeatherResponse
todayDecode :: BL.ByteString -> IO TodayResponse weatherDecode :: BL.ByteString -> IO WeatherResponse
todayDecode jsonBody = do weatherDecode jsonBody =
let decoded = decode jsonBody :: Maybe TodayResponse case decode jsonBody :: Maybe WeatherResponse of
case decoded of
Just decoded -> return decoded Just decoded -> return decoded
Nothing -> error "Invalid JSON" 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 Data.Time as T
import Plot import Plot
import DataTypes import qualified DataTypes as DT
data WhichDay = Yesterday | Today | Tomorrow data WhichDay = Yesterday | Today | Tomorrow
@ -28,29 +28,30 @@ main = do
then error "City not found!" then error "City not found!"
else do else do
currentDate <- getCurrentDate currentDate <- getCurrentDate
--request for yesterday's weather
let yesterdayDate = formatDate $ addDays (-1) currentDate 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 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 let todayRequest = apiRequestBuilder apiKey Today city todayDate
todayResponse <- httpLBS todayRequest todayResponse <- httpLBS todayRequest
print $ getResponseStatusCode todayResponse print $ getResponseStatusCode todayResponse
todayDecoded <- DT.weatherDecode $ getResponseBody todayResponse
todayDecoded <- todayDecode $ getResponseBody todayResponse
print todayDecoded print todayDecoded
-- let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate --request for tomorrow's weather
-- tomorrowResponse <- httpLBS tomorrowRequest let tomorrowDate = formatDate $ addDays 1 currentDate
-- print $ getResponseStatusCode tomorrowResponse let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate
tomorrowResponse <- httpLBS tomorrowRequest
--apiResponse <- httpJSON "http://httpbin.org/get" :: IO (Response ()) -- specifying type as httpJSON return value is ambigious 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 --apiKey is stored in an env variable, not something that should be pushed onto git