From caf72faccc04e647c27e1b5eef85c515949d8210 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Thu, 20 Feb 2025 12:29:35 +0100 Subject: consolidate `backend, cli, common` -> `acms` --- backend/lib/ACMS/API/Fetch.hs | 63 ----------------------- backend/lib/ACMS/API/Query.hs | 17 ------ backend/lib/ACMS/API/REST.hs | 33 ------------ backend/lib/ACMS/API/REST/Collection.hs | 51 ------------------ backend/lib/ACMS/API/REST/Collection/Paginated.hs | 33 ------------ 5 files changed, 197 deletions(-) delete mode 100644 backend/lib/ACMS/API/Fetch.hs delete mode 100644 backend/lib/ACMS/API/Query.hs delete mode 100644 backend/lib/ACMS/API/REST.hs delete mode 100644 backend/lib/ACMS/API/REST/Collection.hs delete mode 100644 backend/lib/ACMS/API/REST/Collection/Paginated.hs (limited to 'backend/lib/ACMS/API') diff --git a/backend/lib/ACMS/API/Fetch.hs b/backend/lib/ACMS/API/Fetch.hs deleted file mode 100644 index 84330b1..0000000 --- a/backend/lib/ACMS/API/Fetch.hs +++ /dev/null @@ -1,63 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module ACMS.API.Fetch - ( APIMonad(fetch), -#ifndef ghcjs_HOST_OS - Network.HTTP.Simple.Request, - Network.HTTP.Simple.setRequestMethod, - Network.HTTP.Simple.setRequestBodyLBS, -#else - JavaScript.Web.XMLHttpRequest.Request, - setRequestMethod, - setRequestBodyLBS, -#endif - ) -where - -#ifndef ghcjs_HOST_OS -import Network.HTTP.Simple -#else -import Data.ByteString.Lazy.UTF8 qualified as LB -import Data.Maybe -import Data.String -import JavaScript.Web.XMLHttpRequest -import Miso.String qualified as J -import Data.ByteString qualified as B -#endif -import Control.Monad.Catch (MonadThrow) -import Data.ByteString.Lazy.Char8 qualified as LB -import Miso (JSM) - -class (MonadThrow m) => APIMonad m where - fetch :: Request -> m LB.ByteString - -instance APIMonad JSM where - fetch req = LB.fromStrict . getResponseBody <$> httpBS req - -#ifdef ghcjs_HOST_OS -httpBS :: Request -> JSM (Response B.ByteString) -httpBS req = xhrByteString req - -instance IsString Request where - fromString uri = - Request - { reqMethod = GET, - reqURI = J.pack uri, - reqLogin = Nothing, - reqHeaders = [], - reqWithCredentials = False, - reqData = NoData - } - -setRequestMethod :: B.ByteString -> Request -> Request -setRequestMethod "POST" req = req {reqMethod = POST} - -setRequestBodyLBS :: LB.ByteString -> Request -> Request -setRequestBodyLBS body req = req {reqData = StringData (J.pack (LB.toString body))} - -getResponseBody :: Response B.ByteString -> B.ByteString -getResponseBody = fromMaybe "" . contents -#else -instance APIMonad IO where - fetch req = LB.fromStrict . getResponseBody <$> httpBS req -#endif diff --git a/backend/lib/ACMS/API/Query.hs b/backend/lib/ACMS/API/Query.hs deleted file mode 100644 index ab2cabc..0000000 --- a/backend/lib/ACMS/API/Query.hs +++ /dev/null @@ -1,17 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module ACMS.API.Query where - -import ACMS.API.Fetch -import Data.Aeson qualified as A -import Data.ByteString.Lazy.UTF8 qualified as LB -import Data.Function ((&)) -import Data.String (IsString (fromString)) - -query :: (APIMonad m) => String -> m A.Value -query q = - fromString ("http://localhost:8081/api/query") - & setRequestMethod "POST" - & setRequestBodyLBS (LB.fromString q) - & fetch - >>= A.throwDecode diff --git a/backend/lib/ACMS/API/REST.hs b/backend/lib/ACMS/API/REST.hs deleted file mode 100644 index 6cd2982..0000000 --- a/backend/lib/ACMS/API/REST.hs +++ /dev/null @@ -1,33 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module ACMS.API.REST where - -import ACMS.API.Fetch -import Data.Aeson qualified as A -import Data.Function ((&)) -import Data.String (IsString (fromString)) -import Miso.String (MisoString) - -restRequest :: String -> Request -restRequest endpoint = - fromString ("http://localhost:8081/api/rest" <> endpoint) - -schemaVersion :: (APIMonad m, A.FromJSON a) => m a -schemaVersion = - restRequest "/schemaVersion" - & fetch - >>= A.throwDecode - -listCollections :: (APIMonad m) => m [MisoString] -listCollections = - restRequest "/collection" - & fetch - >>= A.throwDecode - -createCollection :: (APIMonad m) => MisoString -> m () -createCollection collection = - restRequest "/collections" - & setRequestMethod "POST" - & setRequestBodyLBS (A.encode (A.toJSON collection)) - & fetch - >>= A.throwDecode diff --git a/backend/lib/ACMS/API/REST/Collection.hs b/backend/lib/ACMS/API/REST/Collection.hs deleted file mode 100644 index 7de1909..0000000 --- a/backend/lib/ACMS/API/REST/Collection.hs +++ /dev/null @@ -1,51 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module ACMS.API.REST.Collection where - -import ACMS.API.Fetch -import ACMS.API.REST (restRequest) -import Collection -import Data.Aeson qualified as A -import Data.Function ((&)) -import Text.Printf (printf) - -list :: (APIMonad m) => Collection -> m [A.Object] -list c = - restRequest (printf "/collection/%s" c.name) - & fetch - >>= A.throwDecode - -read :: (APIMonad m) => CollectionItem -> m (Maybe A.Object) -read ci = - restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName) - & fetch - >>= A.throwDecode - -update :: (APIMonad m) => CollectionItem -> A.Object -> m A.Object -update ci o = - restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName) - & setRequestMethod "PUT" - & setRequestBodyLBS (A.encode o) - & fetch - >>= A.throwDecode - -create :: (APIMonad m) => Collection -> A.Object -> m A.Object -create c o = do - restRequest (printf "/collection/%s" c.name) - & setRequestMethod "POST" - & setRequestBodyLBS (A.encode o) - & fetch - >>= A.throwDecode - -delete :: (APIMonad m) => CollectionItem -> m A.Object -delete ci = - restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName) - & setRequestMethod "DELETE" - & fetch - >>= A.throwDecode - -schema :: (APIMonad m) => (A.FromJSON a) => Collection -> m a -schema c = - restRequest (printf "/collection/%s/schema" c.name) - & fetch - >>= A.throwDecode diff --git a/backend/lib/ACMS/API/REST/Collection/Paginated.hs b/backend/lib/ACMS/API/REST/Collection/Paginated.hs deleted file mode 100644 index 159754a..0000000 --- a/backend/lib/ACMS/API/REST/Collection/Paginated.hs +++ /dev/null @@ -1,33 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE RecordWildCards #-} - -module ACMS.API.REST.Collection.Paginated where - -import ACMS.API.Fetch -import ACMS.API.REST (restRequest) -import Collection -import Data.Aeson qualified as A -import Data.Function ((&)) -import GHC.Generics (Generic) -import Text.Printf (printf) - -data Pagination = Pagination - { limit :: Int, - offset :: Int - } - -data Paginated a = Paginated - { count :: Int, - data_ :: [a] - } - deriving (Eq, Show, Generic) - -instance (A.FromJSON a) => A.FromJSON (Paginated a) - -instance (A.ToJSON a) => A.ToJSON (Paginated a) - -list :: (APIMonad m) => Pagination -> Collection -> m (Paginated A.Object) -list p c = - restRequest (printf "/collection/%s/paginated/%d/%d" c.name p.limit p.offset) - & fetch - >>= A.throwDecode -- cgit v1.2.3