on master help sign in
curryer/examples/SimpleKeyValueClient.hs
39 lines 1.2 kB history raw
1{-# LANGUAGE DerivingVia, DeriveGeneric, OverloadedStrings #-}
2import GHC.Generics
3import Network.RPC.Curryer.Client
4import Network.RPC.Curryer.Server (localHostAddr)
5import Options.Generic
6import Codec.Winery
8data SetKey = SetKey String String
9 deriving (Generic, Show)
10 deriving Serialise via WineryVariant SetKey
12data GetKey = GetKey String
13 deriving (Generic, Show)
14 deriving Serialise via WineryVariant GetKey
16data CommandOptions = Get {name :: String}
17 | Set {name :: String, value :: String}
18 deriving (Generic, Show)
20instance ParseRecord CommandOptions
22main :: IO ()
23main = do
24 opts <- getRecord "SimpleKeyValueClient"
25 -- connect to the remote server (in this case on the localhost address)
26 conn <- connect [] localHostAddr 8765
27 case opts of
28 Get k -> do
29 --call the remote function and validate the result
30 eRet <- call conn (GetKey k)
31 case eRet of
32 Left err -> error (show err)
33 Right (Just val) -> putStrLn val
34 Right Nothing -> error "no such key"
35 Set k v -> do
36 eRet <- call conn (SetKey k v)
37 case eRet of
38 Left err -> error (show err)
39 Right () -> pure ()