Skip to content

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.lrad
index 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 it
different 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 with
the 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 the
remote 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 ;; ==> :ok
diff --git a/exe/API.hs b/exe/API.hs
index 8dba00e..90da23c 100644
--- a/exe/API.hs
+++ b/exe/API.hs
@@ -7,19 +7,24 @@ import Protolude
import Radicle
import 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] Value
chainSubmitEndpoint :: Proxy ChainSubmitEndpoint
chainSubmitEndpoint = 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] Value
chainSinceEndpoint :: Proxy ChainSinceEndpoint
chainSinceEndpoint = Proxy
diff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hs
index f286bcd..47aa7ba 100644
--- a/exe/RadicleExe.hs
+++ b/exe/RadicleExe.hs
@@ -99,7 +99,7 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]
case res of
Left 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)) >>= \case
Left 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 Value
submit = client chainSubmitEndpoint
-since :: Int -> ClientM [Value]
+since :: Int -> ClientM Value
since = client chainSinceEndpoint
runClientM' :: (MonadThrow m, MonadIO m) => Text -> HttpClient.Manager -> ClientM a -> m (Either ServantError a)
diff --git a/exe/Server.hs b/exe/Server.hs
index 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]
:<|> Raw
serverApi :: 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 Value
submit conn chains name val = do
res <- liftIO $ insertExpr conn chains name val
case res of
Left 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 index
jsonOutputs :: Chains -> Text -> Handler [Maybe A.Value]
jsonOutputs st name = do