Safe Haskell | None |
---|---|
Language | Haskell2010 |
- type JsonRpcT = ReaderT Session
- runJsonRpcT :: (MonadLoggerIO m, MonadBaseControl IO m) => Ver -> Bool -> Sink Message m () -> Source m (Either Response Message) -> JsonRpcT m a -> m a
- decodeConduit :: MonadLogger m => Ver -> Conduit ByteString m (Either Response Message)
- encodeConduit :: MonadLogger m => Conduit Message m ByteString
- receiveRequest :: MonadLoggerIO m => JsonRpcT m (Maybe Request)
- sendResponse :: MonadLoggerIO m => Response -> JsonRpcT m ()
- sendRequest :: (MonadLoggerIO m, ToJSON q, ToRequest q, FromResponse r) => q -> JsonRpcT m (Maybe (Either ErrorObj r))
- jsonRpcTcpClient :: (MonadLoggerIO m, MonadBaseControl IO m) => Ver -> Bool -> ClientSettings -> JsonRpcT m a -> m a
- jsonRpcTcpServer :: (MonadLoggerIO m, MonadBaseControl IO m) => Ver -> Bool -> ServerSettings -> JsonRpcT m () -> m a
- type SentRequests = HashMap Id (TMVar (Maybe Response))
- data Session = Session {}
- initSession :: Ver -> Bool -> STM Session
- processIncoming :: (Functor m, MonadLoggerIO m) => JsonRpcT m ()
- data Request
- = Request {
- getReqVer :: !Ver
- getReqMethod :: !Method
- getReqParams :: !Value
- getReqId :: !Id
- | Notif {
- getReqVer :: !Ver
- getReqMethod :: !Method
- getReqParams :: !Value
- = Request {
- class FromRequest q where
- parseParams :: Method -> Maybe (Value -> Parser q)
- fromRequest :: FromRequest q => Request -> Either ErrorObj q
- class ToRequest q where
- requestMethod :: q -> Method
- requestIsNotif :: q -> Bool
- buildRequest :: (ToJSON q, ToRequest q) => Ver -> q -> Id -> Request
- data 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 (Maybe Response)
- data ErrorObj
- = ErrorObj {
- getErrMsg :: !String
- getErrCode :: !Int
- getErrData :: !Value
- | ErrorVal {
- getErrData :: !Value
- = ErrorObj {
- fromError :: ErrorObj -> String
- errorParse :: ByteString -> ErrorObj
- errorInvalid :: Value -> ErrorObj
- errorParams :: Value -> ErrorObj
- errorMethod :: Method -> ErrorObj
- errorId :: Id -> ErrorObj
- data Message
- = MsgRequest {
- getMsgRequest :: !Request
- | MsgResponse { }
- = 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
:: (MonadLoggerIO m, MonadBaseControl IO m) | |
=> Ver | JSON-RPC version |
-> Bool | Ignore incoming requests or notifications |
-> Sink Message m () | Sink to send messages |
-> Source m (Either Response Message) | Incoming messages or error responses to be returned to sender |
-> 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 Response Message) Source
encodeConduit :: MonadLogger m => Conduit Message m ByteString Source
Communicate with remote party
receiveRequest :: MonadLoggerIO m => JsonRpcT m (Maybe Request) Source
Receive requests from remote endpoint. Returns Nothing if incoming channel is closed or has never been opened.
sendResponse :: MonadLoggerIO m => Response -> JsonRpcT m () Source
sendRequest :: (MonadLoggerIO m, ToJSON q, ToRequest q, FromResponse r) => q -> JsonRpcT m (Maybe (Either ErrorObj r)) Source
Returns Nothing if did not receive response, could not parse it, or request was a notification. Just Left contains the error object returned by server if any. Just Right means response was received just right.
Transports
Client
:: (MonadLoggerIO m, MonadBaseControl IO m) | |
=> Ver | JSON-RPC version |
-> Bool | Ignore incoming requests or notifications |
-> ClientSettings | Connection settings |
-> JsonRpcT m a | JSON-RPC action |
-> m a | Output of action |
TCP client transport for JSON-RPC.
Server
:: (MonadLoggerIO m, MonadBaseControl IO m) | |
=> Ver | JSON-RPC version |
-> Bool | Ignore incoming requests or notifications |
-> ServerSettings | Connection settings |
-> JsonRpcT m () | Action to perform on connecting client thread |
-> m a |
TCP server transport for JSON-RPC.
Internal data and functions
processIncoming :: (Functor m, MonadLoggerIO m) => JsonRpcT m () Source
Requests
Request | |
| |
Notif | |
|
Parsing
class FromRequest q where Source
fromRequest :: FromRequest q => Request -> Either ErrorObj q Source
Encoding
class ToRequest q where Source
requestMethod :: q -> Method Source
Method associated with request data to build a request object.
requestIsNotif :: q -> Bool Source
Is this request to be sent as a notification (no id, no response)?
Responses
Parsing
class FromResponse r where Source
fromResponse :: FromResponse r => Method -> Response -> Maybe r Source
Encoding
buildResponse :: (Monad m, FromRequest q, ToJSON r) => Respond q m r -> Request -> m (Maybe Response) Source
Errors
ErrorObj | |
| |
ErrorVal | |
|
Error messages
errorParse :: ByteString -> ErrorObj Source
Parse error.
errorInvalid :: Value -> ErrorObj Source
Invalid request.
errorParams :: Value -> ErrorObj Source
Invalid params.
errorMethod :: Method -> ErrorObj Source
Method not found.