Safe Haskell | None |
---|---|
Language | Haskell2010 |
Network.JsonRpc
Contents
- type JsonRpcT = ReaderT Session
- runJsonRpcT :: (MonadLoggerIO m, MonadBaseControl IO m, FromRequest q, ToJSON r) => Ver -> Respond q m r -> Sink Message m () -> Source m (Either Err Message) -> JsonRpcT m a -> m a
- decodeConduit :: MonadLogger m => Ver -> Conduit ByteString m (Either Err Message)
- encodeConduit :: MonadLogger m => Conduit Message m ByteString
- sendRequest :: (MonadLoggerIO m, ToJSON q, ToRequest q, FromResponse r) => q -> JsonRpcT m (Either ErrorObj (Maybe r))
- sendNotif :: (ToJSON no, ToNotif no, MonadLoggerIO m) => no -> JsonRpcT m ()
- receiveNotif :: (MonadLoggerIO m, FromNotif n) => JsonRpcT m (Maybe (Either ErrorObj (Maybe n)))
- dummyRespond :: MonadLoggerIO m => Respond () m ()
- dummySrv :: MonadLoggerIO m => JsonRpcT m ()
- jsonRpcTcpClient :: (MonadLoggerIO m, MonadBaseControl IO m, FromRequest q, ToJSON r) => Ver -> ClientSettings -> Respond q m r -> JsonRpcT m a -> m a
- jsonRpcTcpServer :: (MonadLoggerIO m, MonadBaseControl IO m, FromRequest q, ToJSON r) => Ver -> ServerSettings -> Respond q m r -> JsonRpcT m () -> m a
- data Request = Request {
- getReqVer :: !Ver
- getReqMethod :: !Method
- getReqParams :: !Value
- getReqId :: !Id
- class FromRequest q where
- parseParams :: Method -> Maybe (Value -> Parser q)
- fromRequest :: FromRequest q => Request -> Maybe q
- class ToRequest q where
- requestMethod :: q -> Method
- buildRequest :: (ToJSON q, ToRequest q) => Ver -> q -> Id -> Request
- data Response = Response {}
- class FromResponse r where
- parseResult :: Method -> Maybe (Value -> Parser r)
- fromResponse :: FromResponse r => Method -> Response -> Maybe r
- type Respond q m r = q -> m (Either ErrorObj r)
- buildResponse :: (Monad m, FromRequest q, ToJSON r) => Respond q m r -> Request -> m (Either Err Response)
- data Notif = Notif {
- getNotifVer :: !Ver
- getNotifMethod :: !Method
- getNotifParams :: !Value
- class FromNotif n where
- parseNotif :: Method -> Maybe (Value -> Parser n)
- fromNotif :: FromNotif n => Notif -> Maybe n
- class ToNotif n where
- notifMethod :: n -> Method
- buildNotif :: (ToJSON n, ToNotif n) => Ver -> n -> Notif
- data Err = Err {}
- data ErrorObj
- = ErrorObj {
- getErrMsg :: !String
- getErrCode :: !Int
- getErrData :: !Value
- | ErrorVal {
- getErrData :: !Value
- = ErrorObj {
- fromError :: ErrorObj -> String
- errorParse :: Value -> ErrorObj
- errorInvalid :: Value -> ErrorObj
- errorParams :: Value -> ErrorObj
- errorMethod :: Method -> ErrorObj
- errorId :: Id -> ErrorObj
- data Message
- = MsgRequest {
- getMsgRequest :: !Request
- | MsgResponse { }
- | MsgNotif {
- getMsgNotif :: !Notif
- | MsgError {
- getMsgError :: !Err
- = MsgRequest {
- type Method = Text
- data Id
- fromId :: Id -> String
- data Ver
Introduction
This JSON-RPC library is fully-compatible with JSON-RPC 2.0 and 1.0. It provides an interface that combines a JSON-RPC client and server. It can set and keep track of request ids to parse responses. There is support for sending and receiving notifications. You may use any underlying transport. Basic TCP client and server provided.
A JSON-RPC application using this interface is considered to be peer-to-peer, as it can send and receive all types of JSON-RPC message independent of whether it originated the connection.
Establish JSON-RPC context
Arguments
:: (MonadLoggerIO m, MonadBaseControl IO m, FromRequest q, ToJSON r) | |
=> Ver | JSON-RPC version |
-> Respond q m r | Respond to incoming requests |
-> Sink Message m () | Sink to send messages |
-> Source m (Either Err Message) | Source of incoming messages |
-> JsonRpcT m a | JSON-RPC action |
-> m a | Output of action |
Create JSON-RPC session around conduits from transport layer. When context exits session disappears.
Conduits for encoding/decoding
decodeConduit :: MonadLogger m => Ver -> Conduit ByteString m (Either Err Message) Source
encodeConduit :: MonadLogger m => Conduit Message m ByteString Source
Communicate with remote party
sendRequest :: (MonadLoggerIO m, ToJSON q, ToRequest q, FromResponse r) => q -> JsonRpcT m (Either ErrorObj (Maybe r)) Source
Returns Right Nothing if could not parse response.
sendNotif :: (ToJSON no, ToNotif no, MonadLoggerIO m) => no -> JsonRpcT m () Source
Send notification. Will not block.
receiveNotif :: (MonadLoggerIO m, FromNotif n) => JsonRpcT m (Maybe (Either ErrorObj (Maybe n))) Source
Receive notifications from peer. Will not block. Returns Nothing if incoming channel is closed and empty. Result is Right Nothing if it failed to parse notification.
Dummies
dummyRespond :: MonadLoggerIO m => Respond () m () Source
Respond function for systems that do not reply to requests, as usual in clients.
dummySrv :: MonadLoggerIO m => JsonRpcT m () Source
Dummy action for servers not expecting clients to send notifications, which is true in most cases.
Transports
Client
Arguments
:: (MonadLoggerIO m, MonadBaseControl IO m, FromRequest q, ToJSON r) | |
=> Ver | JSON-RPC version |
-> ClientSettings | Connection settings |
-> Respond q m r | Respond to incoming requests |
-> JsonRpcT m a | JSON-RPC action |
-> m a | Output of action |
TCP client transport for JSON-RPC.
Server
Arguments
:: (MonadLoggerIO m, MonadBaseControl IO m, FromRequest q, ToJSON r) | |
=> Ver | JSON-RPC version |
-> ServerSettings | Connection settings |
-> Respond q m r | Respond to incoming requests |
-> JsonRpcT m () | Action to perform on connecting client thread |
-> m a |
TCP server transport for JSON-RPC.
Requests
Constructors
Request | |
Fields
|
Parsing
class FromRequest q where Source
Methods
parseParams :: Method -> Maybe (Value -> Parser q) Source
Parser for params Value in JSON-RPC request.
Instances
fromRequest :: FromRequest q => Request -> Maybe q Source
Encoding
class ToRequest q where Source
Methods
requestMethod :: q -> Method Source
Method associated with request data to build a request object.
Responses
Parsing
class FromResponse r where Source
Methods
parseResult :: Method -> Maybe (Value -> Parser r) Source
Parser for result Value in JSON-RPC response. Method corresponds to request to which this response answers.
Instances
fromResponse :: FromResponse r => Method -> Response -> Maybe r Source
Encoding
buildResponse :: (Monad m, FromRequest q, ToJSON r) => Respond q m r -> Request -> m (Either Err Response) Source
Notifications
Constructors
Notif | |
Fields
|
Parsing
Encoding
Methods
notifMethod :: n -> Method Source
Errors
Constructors
ErrorObj | |
Fields
| |
ErrorVal | |
Fields
|
Error Messages
errorParse :: Value -> ErrorObj Source
Parse error.
errorInvalid :: Value -> ErrorObj Source
Invalid request.
errorParams :: Value -> ErrorObj Source
Invalid params.
errorMethod :: Method -> ErrorObj Source
Method not found.
Others
Constructors
MsgRequest | |
Fields
| |
MsgResponse | |
Fields | |
MsgNotif | |
Fields
| |
MsgError | |
Fields
|