diff options
Diffstat (limited to 'backend/lib/ACMS/API')
-rw-r--r-- | backend/lib/ACMS/API/REST.hs | 16 | ||||
-rw-r--r-- | backend/lib/ACMS/API/REST/Collection.hs | 15 |
2 files changed, 19 insertions, 12 deletions
diff --git a/backend/lib/ACMS/API/REST.hs b/backend/lib/ACMS/API/REST.hs index 74d402c..64b47b7 100644 --- a/backend/lib/ACMS/API/REST.hs +++ b/backend/lib/ACMS/API/REST.hs @@ -5,7 +5,6 @@ module ACMS.API.REST where #ifndef ghcjs_HOST_OS import Network.HTTP.Simple #else -import Data.ByteString.Char8 qualified as B import Data.ByteString.Lazy.UTF8 qualified as LB import Data.Maybe import Data.String @@ -14,26 +13,35 @@ 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 Miso (JSM) import Miso.String (MisoString) +restRequest :: String -> Request +restRequest endpoint = + defaultRequest + & setRequestSecure False + & setRequestHost "localhost" + & setRequestPort 8081 + & setRequestPath ("/api/rest" <> B.pack endpoint) + schemaVersion :: (APIMonad m, A.FromJSON a) => m a schemaVersion = - "http://localhost:8081/api/rest/schemaVersion" + restRequest "/schemaVersion" & fetch >>= A.throwDecode listCollections :: (APIMonad m) => m [MisoString] listCollections = - "http://localhost:8081/api/rest/collection" + restRequest "/collection" & fetch >>= A.throwDecode createCollection :: (APIMonad m) => MisoString -> m () createCollection collection = - "http://localhost:8081/api/rest/collections" + restRequest "/collections" & setRequestMethod "POST" & setRequestBodyLBS (A.encode (A.toJSON collection)) & fetch diff --git a/backend/lib/ACMS/API/REST/Collection.hs b/backend/lib/ACMS/API/REST/Collection.hs index e0df21b..9f7cafa 100644 --- a/backend/lib/ACMS/API/REST/Collection.hs +++ b/backend/lib/ACMS/API/REST/Collection.hs @@ -11,28 +11,27 @@ import Data.Maybe import JavaScript.Web.XMLHttpRequest import Miso.String qualified as J #endif -import ACMS.API.REST (APIMonad, fetch) +import ACMS.API.REST (APIMonad, fetch, restRequest) import Data.Aeson qualified as A import Data.Function ((&)) -import Data.String (fromString) import Miso.String (MisoString) import Text.Printf (printf) list :: (APIMonad m) => MisoString -> m [A.Object] list c = - fromString (printf "http://localhost:8081/api/rest/collection/%s" c) + restRequest (printf "/collection/%s" c) & fetch >>= A.throwDecode read :: (APIMonad m) => MisoString -> MisoString -> m [A.Object] read c i = - fromString (printf "http://localhost:8081/api/rest/collection/%s/%s" c i) + restRequest (printf "/collection/%s/%s" c i) & fetch >>= A.throwDecode update :: (APIMonad m) => MisoString -> MisoString -> A.Object -> m () update c i o = - fromString (printf "http://localhost:8081/api/rest/collection/%s/%s" c i) + restRequest (printf "/collection/%s/%s" c i) & setRequestMethod "PUT" & setRequestBodyLBS (A.encode o) & fetch @@ -40,7 +39,7 @@ update c i o = create :: (APIMonad m) => MisoString -> A.Object -> m A.Object create c o = do - fromString (printf "http://localhost:8081/api/rest/collection/%s" c) + restRequest (printf "/collection/%s" c) & setRequestMethod "POST" & setRequestBodyLBS (A.encode o) & fetch @@ -48,13 +47,13 @@ create c o = do delete :: (APIMonad m) => MisoString -> MisoString -> m [A.Object] delete c i = - fromString (printf "http://localhost:8081/api/rest/collection/%s/%s" 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 = - fromString (printf "http://localhost:8081/api/rest/collection/%s/schema" c) + restRequest (printf "/collection/%s/schema" c) & fetch >>= A.throwDecode |