Skip to content

Commit 294e250

Browse files
committed
Split each endpoint into its own type synonym
Change-type: patch Signed-off-by: Giovanni Garufi <giovanni@mlabs.city>
1 parent 1d3126d commit 294e250

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

bot-plutus-interface.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ test-suite bot-plutus-interface-test
186186
, quickcheck-instances
187187
, row-types
188188
, serialise
189-
, servant
190189
, servant-client
191190
, servant-client-core
192191
, stm

src/BotPlutusInterface/Server.hs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22

3-
module BotPlutusInterface.Server (app, initState) where
3+
module BotPlutusInterface.Server (
4+
app,
5+
initState,
6+
WebSocketEndpoint,
7+
ActivateContractEndpoint,
8+
RawTxEndpoint,
9+
) where
410

511
import BotPlutusInterface.Contract (runContract)
612
import BotPlutusInterface.Types (
@@ -61,18 +67,25 @@ initState :: IO AppState
6167
initState = AppState <$> newTVarIO Map.empty
6268

6369
-- | Mock API Schema, stripped endpoints that we don't use in this project
64-
type API a =
65-
("ws" :> WebSocketPending) -- Combined websocket (subscription protocol)
66-
:<|> ( "api"
67-
:> "contract"
68-
:> "activate"
69-
:> ReqBody '[JSON] (ContractActivationArgs a)
70-
:> Post '[JSON] ContractInstanceId -- Start a new instance.
71-
)
72-
:<|> ( "rawTx"
73-
:> Capture "hash" Text
74-
:> Get '[JSON] RawTx
75-
)
70+
type API a = WebSocketEndpoint :<|> ActivateContractEndpoint a :<|> RawTxEndpoint
71+
72+
-- Endpoints are split up so it is easier to test them. In particular servant-client
73+
-- can not generate a client for the WebSocketEndpoint; this allows us to still
74+
-- use servant-client to test the other endpoints
75+
76+
type WebSocketEndpoint = "ws" :> WebSocketPending -- Combined websocket (subscription protocol)
77+
78+
type ActivateContractEndpoint a =
79+
"api"
80+
:> "contract"
81+
:> "activate"
82+
:> ReqBody '[JSON] (ContractActivationArgs a)
83+
:> Post '[JSON] ContractInstanceId -- Start a new instance.
84+
85+
type RawTxEndpoint =
86+
"rawTx"
87+
:> Capture "hash" Text
88+
:> Get '[JSON] RawTx
7689

7790
server :: HasDefinitions t => PABConfig -> AppState -> Server (API t)
7891
server pabConfig state =
@@ -223,9 +236,9 @@ handleContract pabConf state@(AppState st) contract = liftIO $ do
223236
rawTxHandler :: PABConfig -> Text -> Handler RawTx
224237
rawTxHandler config hash = do
225238
-- Check that endpoint is enabled
226-
assert (pcEnableTxEndpoint config)
239+
assert config.pcEnableTxEndpoint
227240
-- Absolute path to pcTxFileDir that is specified in the config
228-
txFolderPath <- liftIO $ makeAbsolute (unpack $ pcTxFileDir config)
241+
txFolderPath <- liftIO $ makeAbsolute (unpack config.pcTxFileDir)
229242

230243
-- Add/Set .raw extension on path
231244
let suppliedPath :: FilePath

src/BotPlutusInterface/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@ data RawTx = RawTx
135135

136136
-- type is a reserved keyword in haskell and can not be used as a field name
137137
-- when converting this to JSON we drop the _ prefix from each field
138-
$(deriveJSON defaultOptions {fieldLabelModifier = drop 1} ''RawTx)
138+
deriveJSON defaultOptions {fieldLabelModifier = drop 1} ''RawTx

test/Spec/BotPlutusInterface/Server.hs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Spec.BotPlutusInterface.Server (tests) where
22

3-
import BotPlutusInterface.Server (app, initState)
3+
import BotPlutusInterface.Server (RawTxEndpoint, app, initState)
44
import BotPlutusInterface.Types (
55
HasDefinitions (..),
66
PABConfig (..),
@@ -17,7 +17,6 @@ import Test.Tasty.HUnit (testCase, (@?=))
1717
import Network.HTTP.Client (defaultManagerSettings, newManager)
1818
import Network.HTTP.Types.Status (status404)
1919
import Network.Wai.Handler.Warp (testWithApplication)
20-
import Servant.API (Capture, Get, JSON, (:>))
2120
import Servant.Client (ClientEnv, ClientError (..), client, mkClientEnv, responseStatusCode, runClientM)
2221
import Servant.Client.Core.BaseUrl (BaseUrl (..), parseBaseUrl)
2322

@@ -75,14 +74,7 @@ rawTxTests =
7574
Left (FailureResponse _ res) <- runRawTxClient txHash
7675
responseStatusCode res @?= status404
7776

78-
-- Ideally we would reuse the API type definition from BotPlutusInterface.Server but servant-client
79-
-- can not generate a client for the websocket endpoint.
80-
txProxy ::
81-
Proxy
82-
( "rawTx"
83-
:> Capture "hash" Text
84-
:> Get '[JSON] RawTx
85-
)
77+
txProxy :: Proxy RawTxEndpoint
8678
txProxy = Proxy
8779

8880
initServerAndClient :: PABConfig -> RawTxTest a -> IO a

0 commit comments

Comments
 (0)