A peer-to-peer stack for code collaboration
Improve chain API
Thomas Scholtes6e362b58436a
diff --git a/docs/source/guide/Chains.lrad b/docs/source/guide/Chains.lradindex 89c6fba..fc3e25c 100644--- a/docs/source/guide/Chains.lrad+++ b/docs/source/guide/Chains.lrad@@ -1,15 +1,5 @@# Chains-(N.B.: for this section, you should use the `radicle-client` executable rather-than the `radicle` one. The simplest way to run it is to pass `radicle.xyz` as-the `url` argument:--```-radicle-client --config rad/repl.rad --url radicle.xyz-```--In the future `radicle-client` and `radicle` will be folded into one.)-By now you've seen some of `radicle`, but we haven't touched upon what makes itdifferent from other languages.@@ -36,14 +26,14 @@ TODO: more.First we start up a simple server that hosts remote chains on port 8000.```-stack exec -- radicle-server+stack run -- radicle-server```Now we can start a radicle REPL that will be able to communicate withthe remote chain.```-stack exec -- radicle-client --config rad/repl.rad+stack run -- radicle-client rad/repl.rad```We start of by sending an expression to the chain that defines the atom `foo`.@@ -73,7 +63,7 @@ Next we will use `:enter-chain` to evaluate and send expressions to theremote chain.```-(:enter-chain "http://localhost:8023/chains/my-first-chain")+(:enter-chain "http://localhost:8000/chains/my-first-chain")(def bar foo)(def quux 4):send ;; ==> :okdiff --git a/exe/API.hs b/exe/API.hsindex 8dba00e..90da23c 100644--- a/exe/API.hs+++ b/exe/API.hs@@ -7,19 +7,24 @@ import Protoludeimport Radicleimport Servant.API-instance Show a => MimeRender PlainText a where- mimeRender _ x = show x+instance MimeRender PlainText Value where+ mimeRender _ x = LBS.fromStrict $ encodeUtf8 $ renderCompactPretty x-instance Read a => MimeUnrender PlainText a where- mimeUnrender _ x = readEither $ T.unpack $ decodeUtf8 $ LBS.toStrict x+instance MimeUnrender PlainText Value where+ mimeUnrender _ x =+ let src = decodeUtf8 $ LBS.toStrict x+ in first T.unpack $ parse "[request body]" src--type ChainSubmitEndpoint = "submit" :> ReqBody '[PlainText] Value :> Post '[PlainText] ()+-- | Endpoint to submit an expression to a chain. Responds with @:ok@ if the+-- expression has been submitted sucessfully.+type ChainSubmitEndpoint = "submit" :> ReqBody '[PlainText] Value :> Post '[PlainText] ValuechainSubmitEndpoint :: Proxy ChainSubmitEndpointchainSubmitEndpoint = Proxy-type ChainSinceEndpoint = "since" :> Capture "index" Int :> Get '[PlainText] [Value]+-- | Endpoint to obtain all entries in a chain starting from the given+-- index. Responds with a Radicle vector of the expressions.+type ChainSinceEndpoint = "since" :> Capture "index" Int :> Get '[PlainText] ValuechainSinceEndpoint :: Proxy ChainSinceEndpointchainSinceEndpoint = Proxydiff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hsindex f286bcd..47aa7ba 100644--- a/exe/RadicleExe.hs+++ b/exe/RadicleExe.hs@@ -99,7 +99,7 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]case res ofLeft e -> throwErrorHere . OtherError$ "send!: failed:" <> show e- Right () -> pure $ List []+ Right r -> pure r[_, _] -> throwErrorHere $ TypeError "send!: first argument should be a string"xs -> throwErrorHere $ WrongNumberOfArgs "send!" 2 (length xs))@@ -116,7 +116,7 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]liftIO (runClientM' url mgr (since r)) >>= \caseLeft err -> throwErrorHere . OtherError$ "receive!: request failed:" <> show err- Right v' -> pure $ List v'+ Right v' -> pure v'[String _, _] -> throwErrorHere $ TypeError "receive!: expecting number as second arg"[_, _] -> throwErrorHere $ TypeError "receive!: expecting string as first arg"xs -> throwErrorHere $ WrongNumberOfArgs "receive!" 2 (length xs)@@ -124,10 +124,10 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]-- * Client functions-submit :: Value -> ClientM ()+submit :: Value -> ClientM Valuesubmit = client chainSubmitEndpoint-since :: Int -> ClientM [Value]+since :: Int -> ClientM Valuesince = client chainSinceEndpointrunClientM' :: (MonadThrow m, MonadIO m) => Text -> HttpClient.Manager -> ClientM a -> m (Either ServantError a)diff --git a/exe/Server.hs b/exe/Server.hsindex f2977f0..800e271 100644--- a/exe/Server.hs+++ b/exe/Server.hs@@ -38,7 +38,6 @@ import Servant, Get, Handler, JSON- , PlainText, Raw, ServantErr(..), Server@@ -68,7 +67,7 @@ newtype Chains = Chains { getChains :: MVar (Map Text Chain) }type ServerApi= "chains" :> Capture "chain" Text :> (ChainSubmitEndpoint :<|> ChainSinceEndpoint)- :<|> "outputs" :> Capture "chain" Text :> Get '[JSON, PlainText] [Maybe A.Value]+ :<|> "outputs" :> Capture "chain" Text :> Get '[JSON] [Maybe A.Value]:<|> RawserverApi :: Proxy ServerApi@@ -118,17 +117,17 @@ loadState conn = do-- | Submit something to a chain. The value is expected to be a pair, with the-- first item specifying the chain the value is being submitted to, and the-- second the expression being submitted.-submit :: Connection -> Chains -> Text -> Value -> Handler ()+submit :: Connection -> Chains -> Text -> Value -> Handler Valuesubmit conn chains name val = dores <- liftIO $ insertExpr conn chains name valcase res ofLeft err -> throwError $ err400 { errBody = fromStrict $ encodeUtf8 err }- Right _ -> pure ()+ Right _ -> pure $ Keyword $ unsafeToIdent "ok"-- | Get all expressions submitted to a chain since 'index'.-since :: Connection -> Text -> Int -> Handler [Value]-since conn name index = liftIO $ getSinceDB conn name index+since :: Connection -> Text -> Int -> Handler Value+since conn name index = liftIO $ List <$> getSinceDB conn name indexjsonOutputs :: Chains -> Text -> Handler [Maybe A.Value]jsonOutputs st name = do