profun/app/DataTypes.hs

82 lines
1.8 KiB
Haskell
Raw Normal View History

2024-05-26 14:41:13 +02:00
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-} --needed for ByteString arguments
module DataTypes where
import Data.Aeson
import GHC.Generics
import qualified Data.ByteString.Lazy.Char8 as BL
data Location = Location
{ name :: String
, region :: String
, country :: String
, lat :: Float
, lon :: Float
, tz_id :: String
, localtime_epoch :: Int
, localtime :: String
} deriving (Show, Generic)
data Current = Current
{ temp_c :: Float
} deriving (Show, Generic)
data TodayResponse = TodayResponse
{ location :: Location
, current :: Current
} deriving (Show, Generic)
data Day = Day
{ avgtemp_c :: Float
} deriving (Show, Generic)
data ForecastDay = ForecastDay
{ day :: Day
} deriving (Show, Generic)
data Forecast = Forecast
{ forecastday :: [ForecastDay]
} deriving (Show, Generic)
-- data YesterdayResponse = YesterdayResponse
-- { location :: Location
-- , forecast :: 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
todayDecode :: BL.ByteString -> IO TodayResponse
todayDecode jsonBody = do
let decoded = decode jsonBody :: Maybe TodayResponse
case decoded 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"