jsonrpc-conduit 0.3.0 → 0.3.2
raw patch · 3 files changed
+89/−19 lines, 3 filesdep ~aesondep ~bytestringdep ~conduitPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: aeson, bytestring, conduit, conduit-extra, hspec, transformers
API changes (from Hackage documentation)
- Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.Class.FromJSON (Data.Conduit.JsonRpc.Internal.Types.Request Data.Aeson.Types.Internal.Value)
- Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.Class.FromJSON a => Data.Aeson.Types.Class.FromJSON (Data.Conduit.JsonRpc.Internal.Types.Response a)
- Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.Class.ToJSON (Data.Conduit.JsonRpc.Internal.Types.Response Data.Aeson.Types.Internal.Value)
- Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.Class.ToJSON a => Data.Aeson.Types.Class.ToJSON (Data.Conduit.JsonRpc.Internal.Types.Request a)
- Data.Conduit.JsonRpc.Methods: Method :: (i -> m (Either MethodError o)) -> Method m
+ Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.FromJSON.FromJSON (Data.Conduit.JsonRpc.Internal.Types.Request Data.Aeson.Types.Internal.Value)
+ Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Data.Conduit.JsonRpc.Internal.Types.Response a)
+ Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.ToJSON.ToJSON (Data.Conduit.JsonRpc.Internal.Types.Response Data.Aeson.Types.Internal.Value)
+ Data.Conduit.JsonRpc.Internal.Types: instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Data.Conduit.JsonRpc.Internal.Types.Request a)
+ Data.Conduit.JsonRpc.Methods: [Method] :: forall i m o. (FromJSON i, ToJSON o) => (i -> m (Either MethodError o)) -> Method m
- Data.Conduit.JsonRpc.Server: serve :: (Applicative m, Monad m) => Methods m -> Conduit ByteString m ByteString
+ Data.Conduit.JsonRpc.Server: serve :: (Applicative m, Monad m) => Methods m -> ConduitT ByteString ByteString m ()
Files
- jsonrpc-conduit.cabal +18/−14
- src/Data/Conduit/JsonRpc/Server.hs +6/−5
- test/Data/Conduit/JsonRpc/ServerSpec.hs +65/−0
jsonrpc-conduit.cabal view
@@ -1,5 +1,5 @@ name: jsonrpc-conduit-version: 0.3.0+version: 0.3.2 synopsis: JSON-RPC 2.0 server over a Conduit. description: @jsonrpc-conduit@ implements the basic building block of a JSON-RPC 2.0 server.@@ -10,8 +10,7 @@ . The JSON-RPC conduit is generic with respect to the channel used to exchange data with the client. It can use a network connection or, for example,- the standard input / ouput of a process. The latter is demonstrated by the- @jsonrpc-conduit-demo@ executable, which can be compiled using the @demo@ flag.+ the standard input / ouput of a process. license: GPL-3 license-file: LICENSE@@ -19,39 +18,44 @@ maintainer: gbrsales@gmail.com category: Conduit build-type: Simple-cabal-version: >=1.8+cabal-version: 2.0+tested-with: GHC >= 8.6.5 source-repository head type: git location: https://github.com/gbrsales/jsonrpc-conduit.git library+ default-language: Haskell2010 hs-source-dirs: src exposed-modules: Data.Conduit.JsonRpc.Internal.Types, Data.Conduit.JsonRpc.Methods, Data.Conduit.JsonRpc.Server build-depends: base >=4 && <5,- aeson >=0.7 && <0.11,+ aeson >=0.7 && <1.6, attoparsec >=0.11 && <0.14,- bytestring >=0.10 && <0.11,- conduit >=1.1 && <1.3,- conduit-extra >=1.1 && <1.3,+ bytestring >=0.10 && <0.12,+ conduit >=1.2.8 && <1.4,+ conduit-extra >=1.1 && <1.4, mtl >=2.1 && <2.3, text >=1.1 && <1.3,- transformers >=0.3 && <0.5,+ transformers >=0.3 && <0.6, unordered-containers ==0.2.* ghc-options: -Wall -fno-warn-name-shadowing test-suite spec type: exitcode-stdio-1.0+ default-language: Haskell2010 hs-source-dirs: test main-is: Spec.hs+ other-modules: Data.Conduit.JsonRpc.ServerSpec build-depends: base >=4 && <5,- aeson >=0.7 && <0.11,- bytestring >=0.10 && <0.11,- conduit >=1.1 && <1.3,- conduit-extra >=1.1 && <1.3,- hspec >=2.1 && <2.3,+ aeson >=0.7 && <1.6,+ bytestring >=0.10 && <0.12,+ conduit >=1.2.8 && <1.4,+ conduit-extra >=1.1 && <1.4,+ hspec >=2.1 && <2.8, jsonrpc-conduit, text >=1.1 && <1.3+ build-tool-depends: hspec-discover:hspec-discover ghc-options: -Wall -fno-warn-name-shadowing
src/Data/Conduit/JsonRpc/Server.hs view
@@ -1,6 +1,6 @@ -- | -- Module : Data.Conduit.JsonRpc.Server--- Copyright : (c) 2012-2013,2015 Gabriele Sales <gbrsales@gmail.com>+-- Copyright : (c) 2012-2021 Gabriele Sales <gbrsales@gmail.com> -- -- JSON-RPC 2.0 server 'Conduit'. @@ -43,9 +43,10 @@ * it is not possible to set the @data@ attribute of error objects -}-serve :: (Applicative m, Monad m) => Methods m -> Conduit ByteString m ByteString-serve methods = parseRequests =$=- C.concatMapM (\r -> encodeResponse <$> handleRequest methods r)+serve :: (Applicative m, Monad m)+ => Methods m -> ConduitT ByteString ByteString m ()+serve methods = parseRequests+ .| C.concatMapM (fmap encodeResponse . handleRequest methods) parseRequests :: (Monad m)@@ -82,7 +83,7 @@ Nothing -> InvalidRequest Just r' -> Correct r' -handleRequest :: (Applicative m, Monad m)+handleRequest :: Monad m => Methods m -> Processed (Request Value) -> m (Response Value)
+ test/Data/Conduit/JsonRpc/ServerSpec.hs view
@@ -0,0 +1,65 @@+{- |+Module : Data.Conduit.JsonRpc.ServerSpec+Description : Tests for the server.+Copyright : (c) 2015-2021 Gabriele Sales+-}++{-# LANGUAGE NamedFieldPuns #-}+{-# LANGUAGE OverloadedStrings #-}++module Data.Conduit.JsonRpc.ServerSpec+ ( spec )+where++import Data.Aeson hiding (Error)+import Data.ByteString (ByteString)+import Data.Conduit+import qualified Data.Conduit.Binary as B+import Data.Conduit.JsonRpc.Internal.Types+import Data.Conduit.JsonRpc.Methods+import Data.Conduit.JsonRpc.Server+import Data.Text (Text)+import Test.Hspec+++spec :: Spec+spec = do+ describe "method sum" $ do+ it "computes the sum" $ do+ let nums = [1,2,3] :: [Int]+ oneshot "sum" nums `shouldReturn` Right (6::Int)+ it "rejects strings" $ do+ let strings = ["1","2","3"] :: [Text]+ oneshot "sum" strings `shouldReturn`+ (Left "Invalid params" :: Either Text Int)++ describe "method cat" $+ it "concatenates strings" $ do+ let strings = ["a","b","c"] :: [Text]+ oneshot "cat" strings `shouldReturn` Right ("abc"::Text)++ describe "unknown method" $+ it "is rejected" $+ oneshot "unknown" () `shouldReturn`+ (Left "Method not found" :: Either Text ())+++oneshot :: (ToJSON a, FromJSON b) => Text -> a -> IO (Either Text b)+oneshot method params = do+ let reqId = toJSON (1::Int)+ req = encode (Request method params reqId)+ bs <- runConduit (B.sourceLbs req .| server .| B.sinkLbs)+ return $ case decode bs of+ Nothing -> Left "invalid response"+ Just Error{errMsg} -> Left errMsg+ Just Result{result} -> Right result++server :: ConduitT ByteString ByteString IO ()+server = serve (fromList [ method "sum" sum'+ , method "cat" cat ])++sum' :: [Int] -> IO (Either MethodError Int)+sum' = return . Right . sum++cat :: [String] -> IO (Either MethodError String)+cat = return . Right . concat