diff options
author | Jacques Comeaux <jacquesrcomeaux@gmail.com> | 2022-05-29 16:07:59 -0500 |
---|---|---|
committer | Jacques Comeaux <jacquesrcomeaux@gmail.com> | 2022-05-29 16:07:59 -0500 |
commit | 8a06c083fab9c66e754c7ed5a75dd4d89131c43e (patch) | |
tree | 28098c165bce0d8ebb5dda2d5390e63ddca1ab1e /src/Tomato/Data/Tomato.hs | |
parent | 399ab3ee1653bbc0da69e2cc299aa2a313e765b4 (diff) |
Improve error reporting
Diffstat (limited to 'src/Tomato/Data/Tomato.hs')
-rw-r--r-- | src/Tomato/Data/Tomato.hs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/Tomato/Data/Tomato.hs b/src/Tomato/Data/Tomato.hs new file mode 100644 index 0000000..4b75adf --- /dev/null +++ b/src/Tomato/Data/Tomato.hs @@ -0,0 +1,70 @@ +module Tomato.Data.Tomato + ( Links (..) + , Tomato (..) + , Urls (..) + ) where + +import RIO + +import Data.Aeson ((.:)) +import Network.HTTP.Req (Url, Scheme (Https), useHttpsURI) + +import qualified Data.Aeson as Ae +import qualified Data.Aeson.Types as Ae +import qualified Text.URI as URI + + +data Tomato = Tomato + { id :: !Text + , width :: !Word + , height :: !Word + , color :: !Text + , blur_hash :: !Text + , description :: !Text + , urls :: !Urls + , links :: !Links + } deriving Show + +instance Ae.FromJSON Tomato where + parseJSON = Ae.withObject "Tomato" $ \o -> Tomato + <$> o .: "id" + <*> o .: "width" + <*> o .: "height" + <*> o .: "color" + <*> o .: "blur_hash" + <*> o .: "description" + <*> o .: "urls" + <*> o .: "links" + +data Links = Links + { self :: !(Url Https) + , html :: !(Url Https) + , download :: !(Url Https) + , download_location :: !(Url Https) + } deriving Show + +instance Ae.FromJSON Links where + parseJSON = Ae.withObject "Links" $ \o -> Links + <$> (o .: "self" >>= toUrl) + <*> (o .: "html" >>= toUrl) + <*> (o .: "download" >>= toUrl) + <*> (o .: "download_location" >>= toUrl) + +data Urls = Urls + { raw :: !(Url Https) + , full :: !(Url Https) + , regular :: !(Url Https) + , small :: !(Url Https) + , thumb :: !(Url Https) + } deriving Show + +instance Ae.FromJSON Urls where + parseJSON = Ae.withObject "Urls" $ \o -> Urls + <$> (o .: "raw" >>= toUrl) + <*> (o .: "full" >>= toUrl) + <*> (o .: "regular" >>= toUrl) + <*> (o .: "small" >>= toUrl) + <*> (o .: "thumb" >>= toUrl) + +toUrl :: Text -> Ae.Parser (Url Https) +toUrl t = maybe mzero pure $ fmap fst $ useHttpsURI =<< URI.mkURI t |