profun/app/DataTypes.hs

67 lines
1.4 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 Day = Day
{ avgtemp_c :: Float
} deriving (Show, Generic)
data ForecastDay = ForecastDay
{ day :: Day
} deriving (Show, Generic)
data Forecast = Forecast
{ forecastday :: [ForecastDay]
} deriving (Show, Generic)
2024-05-26 16:57:22 +02:00
data WeatherResponse = WeatherResponse
{ location :: Location
, current :: Maybe Current
, forecast :: Maybe Forecast
} deriving (Show, Generic)
2024-05-26 14:41:13 +02:00
instance FromJSON Location
instance ToJSON Location
instance FromJSON Current
instance ToJSON Current
instance FromJSON Day
instance ToJSON Day
instance FromJSON ForecastDay
instance ToJSON ForecastDay
instance FromJSON Forecast
instance ToJSON Forecast
2024-05-26 16:57:22 +02:00
instance FromJSON WeatherResponse
instance ToJSON WeatherResponse
2024-05-26 14:41:13 +02:00
2024-05-26 16:57:22 +02:00
weatherDecode :: BL.ByteString -> IO WeatherResponse
weatherDecode jsonBody =
case decode jsonBody :: Maybe WeatherResponse of
2024-05-26 14:41:13 +02:00
Just decoded -> return decoded
Nothing -> error "Invalid JSON"