{-# LANGUAGE OverloadedStrings #-} module ACMS.API.REST.Collection where #ifndef ghcjs_HOST_OS import Network.HTTP.Simple #else import ACMS.API.REST (setRequestMethod, setRequestBodyLBS, getResponseBody) import Data.ByteString.Char8 qualified as B import Data.Maybe import JavaScript.Web.XMLHttpRequest import Miso.String qualified as J #endif import ACMS.API.REST (APIMonad, fetch, restRequest) import Data.Aeson qualified as A import Data.Function ((&)) import Miso.String (MisoString) import Text.Printf (printf) import Debug.Trace list :: (APIMonad m) => MisoString -> m [A.Object] list c = restRequest (printf "/collection/%s" c) & fetch >>= A.throwDecode read :: (APIMonad m) => MisoString -> MisoString -> m [A.Object] read c i = restRequest (printf "/collection/%s/%s" c i) & fetch >>= A.throwDecode update :: (APIMonad m) => MisoString -> MisoString -> A.Object -> m A.Object update c i o = restRequest (printf "/collection/%s/%s" c i) & setRequestMethod "PUT" & setRequestBodyLBS (A.encode o) & fetch >>= A.throwDecode create :: (APIMonad m) => MisoString -> A.Object -> m A.Object create c o = do restRequest (printf "/collection/%s" c) & setRequestMethod "POST" & setRequestBodyLBS (A.encode o) & fetch >>= A.throwDecode delete :: (APIMonad m) => MisoString -> MisoString -> m [A.Object] delete c i = restRequest (printf "/collection/%s/%s" c i) & setRequestMethod "DELETE" & fetch >>= A.throwDecode schema :: (APIMonad m) => (A.FromJSON a) => MisoString -> m a schema c = restRequest (printf "/collection/%s/schema" c) & fetch >>= A.throwDecode