chandlr-server/app/DataClient.hs

54 lines
1.9 KiB
Haskell
Raw Permalink Normal View History

{-# LANGUAGE RecordWildCards #-}
2024-03-23 01:27:12 +00:00
module DataClient
2024-03-23 01:43:24 +00:00
( fetchLatest
, getThread
2024-03-23 01:43:24 +00:00
)
where
import Data.Time.Clock (UTCTime)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LC8
import qualified Data.Text as Text
2024-03-23 01:27:12 +00:00
2024-03-23 01:43:24 +00:00
import Common.Network.CatalogPostType (CatalogPost)
import Common.Network.ClientTypes (Model (..), FetchCatalogArgs (..))
2024-03-23 01:27:12 +00:00
import Common.Network.HttpClient
2024-03-23 01:43:24 +00:00
( post
, get
2024-03-23 01:43:24 +00:00
, HttpError (..)
)
import Data.Aeson (eitherDecode, encode, FromJSON)
import qualified Common.FrontEnd.Action as A
2024-03-23 01:43:24 +00:00
import Common.Server.JSONSettings (JSONSettings)
import Common.Network.SiteType (Site)
2024-03-23 01:27:12 +00:00
2024-03-23 01:43:24 +00:00
fetchLatest :: JSONSettings -> Model -> UTCTime -> IO (Either HttpError [ CatalogPost ])
fetchLatest settings m t = do
2024-03-23 01:27:12 +00:00
post settings "/rpc/fetch_catalog" payload False >>= return . eitherDecodeResponse
where
payload = encode FetchCatalogArgs
{ max_time = t
, max_row_read = fetchCount m
}
2024-03-23 01:43:24 +00:00
getThread :: JSONSettings -> Model -> A.GetThreadArgs -> IO (Either HttpError [ Site ])
getThread settings m A.GetThreadArgs {..} =
get settings path >>= return . eitherDecodeResponse
where
path = "/sites?"
<> "select=*,boards(*,threads(*,posts(*,attachments(*))))"
<> "&name=eq." <> Text.unpack website
<> "&boards.pathpart=eq." <> Text.unpack board_pathpart
<> "&boards.threads.board_thread_id=eq." <> (show board_thread_id)
<> "&boards.threads.posts.order=board_post_id.asc"
2024-03-23 01:43:24 +00:00
eitherDecodeResponse :: (FromJSON a) => Either HttpError LBS.ByteString -> Either HttpError a
eitherDecodeResponse (Left err) = Left err
eitherDecodeResponse (Right bs) =
case eitherDecode bs of
Right val -> Right val
Left err -> Left $ StatusCodeError 500 $ LC8.pack $ "Failed to decode JSON: " ++ err ++ " " ++ (show bs)