From e66534eefb5979c1ec5e0a28e9c29969ae2c9884 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sun, 13 Oct 2024 11:55:20 +0200 Subject: improve REST API --- backend/lib/ACMS/API/REST.hs | 8 ++------ backend/lib/ACMS/API/REST/Collection.hs | 8 +++----- 2 files changed, 5 insertions(+), 11 deletions(-) (limited to 'backend/lib/ACMS') diff --git a/backend/lib/ACMS/API/REST.hs b/backend/lib/ACMS/API/REST.hs index 64b47b7..ec72db9 100644 --- a/backend/lib/ACMS/API/REST.hs +++ b/backend/lib/ACMS/API/REST.hs @@ -13,19 +13,15 @@ import Miso.String qualified as J #endif import Control.Monad.Catch (MonadThrow) import Data.Aeson qualified as A -import Data.ByteString.Char8 qualified as B import Data.ByteString.Lazy.Char8 qualified as LB import Data.Function ((&)) +import Data.String (IsString (fromString)) import Miso (JSM) import Miso.String (MisoString) restRequest :: String -> Request restRequest endpoint = - defaultRequest - & setRequestSecure False - & setRequestHost "localhost" - & setRequestPort 8081 - & setRequestPath ("/api/rest" <> B.pack endpoint) + fromString ("http://localhost:8081/api/rest/" <> endpoint) schemaVersion :: (APIMonad m, A.FromJSON a) => m a schemaVersion = diff --git a/backend/lib/ACMS/API/REST/Collection.hs b/backend/lib/ACMS/API/REST/Collection.hs index 3ca8ffd..b3faf19 100644 --- a/backend/lib/ACMS/API/REST/Collection.hs +++ b/backend/lib/ACMS/API/REST/Collection.hs @@ -11,12 +11,10 @@ import Data.Maybe import JavaScript.Web.XMLHttpRequest import Miso.String qualified as J #endif -import Collection import ACMS.API.REST (APIMonad, fetch, restRequest) +import Collection import Data.Aeson qualified as A import Data.Function ((&)) -import Debug.Trace -import Miso.String (MisoString) import Text.Printf (printf) list :: (APIMonad m) => Collection -> m [A.Object] @@ -25,7 +23,7 @@ list c = & fetch >>= A.throwDecode -read :: (APIMonad m) => CollectionItem -> m [A.Object] +read :: (APIMonad m) => CollectionItem -> m (Maybe A.Object) read ci = restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName) & fetch @@ -47,7 +45,7 @@ create c o = do & fetch >>= A.throwDecode -delete :: (APIMonad m) => CollectionItem -> m [A.Object] +delete :: (APIMonad m) => CollectionItem -> m A.Object delete ci = restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName) & setRequestMethod "DELETE" -- cgit v1.2.3