-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Fully-featured JSON-RPC 2.0 library
--   
--   Library compatible with JSON-RPC 2.0 and 1.0
@package json-rpc
@version 1.0.0

module Network.JSONRPC
type JSONRPCT = ReaderT Session

-- | Create JSON-RPC session around conduits from transport layer. When
--   context exits session disappears.
runJSONRPCT :: (MonadLoggerIO m, MonadUnliftIO m) => Ver -> Bool -> ConduitT ByteString Void m () -> ConduitT () ByteString m () -> JSONRPCT m a -> m a

-- | Conduit to decode incoming messages. Left Response indicates a
--   response to send back to sender if parsing JSON fails.
decodeConduit :: MonadLogger m => Ver -> ConduitT ByteString (Either Response Value) m ()
encodeConduit :: (ToJSON j, MonadLogger m) => ConduitT j ByteString m ()

-- | Receive requests from remote endpoint. Returns Nothing if incoming
--   channel is closed or has never been opened. Will reject incoming
--   request if sent in a batch.
receiveRequest :: MonadLoggerIO m => JSONRPCT m (Maybe Request)

-- | Receive batch of requests. Will also accept single requests.
receiveBatchRequest :: MonadLoggerIO m => JSONRPCT m (Maybe BatchRequest)

-- | Send response message. Do not use to respond to a batch of requests.
sendResponse :: MonadLoggerIO m => Response -> JSONRPCT m ()

-- | Send batch of responses. Use to respond to a batch of requests.
sendBatchResponse :: MonadLoggerIO m => BatchResponse -> JSONRPCT m ()

-- | Returns Nothing if did not receive response, could not parse it, or
--   request is a notification. Just Left contains the error object
--   returned by server if any. Just Right means response was received just
--   right.
sendRequest :: (MonadLoggerIO m, ToJSON q, ToRequest q, FromResponse r) => q -> JSONRPCT m (Maybe (Either ErrorObj r))

-- | Send multiple requests in a batch. If only a single request, do not
--   put it in a batch.
sendBatchRequest :: (MonadLoggerIO m, ToJSON q, ToRequest q, FromResponse r) => [q] -> JSONRPCT m [Maybe (Either ErrorObj r)]

-- | TCP client transport for JSON-RPC.
jsonrpcTCPClient :: (MonadLoggerIO m, MonadUnliftIO m) => Ver -> Bool -> ClientSettings -> JSONRPCT m a -> m a

-- | TCP server transport for JSON-RPC.
jsonrpcTCPServer :: (MonadLoggerIO m, MonadUnliftIO m) => Ver -> Bool -> ServerSettings -> JSONRPCT m () -> m a
type SentRequests = HashMap Id (TMVar (Maybe Response))
data Session
Session :: TBMChan (Either Response Value) -> TBMChan Message -> Maybe (TBMChan BatchRequest) -> TVar Id -> TVar SentRequests -> Ver -> TVar Bool -> Session
[inCh] :: Session -> TBMChan (Either Response Value)
[outCh] :: Session -> TBMChan Message
[reqCh] :: Session -> Maybe (TBMChan BatchRequest)
[lastId] :: Session -> TVar Id
[sentReqs] :: Session -> TVar SentRequests
[rpcVer] :: Session -> Ver
[dead] :: Session -> TVar Bool
initSession :: Ver -> Bool -> STM Session

-- | Process incoming messages. Do not use this directly unless you know
--   what you are doing. This is an internal function.
processIncoming :: (Functor m, MonadLoggerIO m) => JSONRPCT m ()

-- | Send any message. Do not use this. Use the other high-level functions
--   instead. Will not track request ids. Incoming responses to requests
--   sent using this method will be ignored.
sendMessage :: MonadLoggerIO m => Message -> JSONRPCT m ()
data Request
Request :: !Ver -> !Method -> !Value -> !Id -> Request
[getReqVer] :: Request -> !Ver
[getReqMethod] :: Request -> !Method
[getReqParams] :: Request -> !Value
[getReqId] :: Request -> !Id
Notif :: !Ver -> !Method -> !Value -> Request
[getReqVer] :: Request -> !Ver
[getReqMethod] :: Request -> !Method
[getReqParams] :: Request -> !Value
data BatchRequest
BatchRequest :: ![Request] -> BatchRequest
[getBatchRequest] :: BatchRequest -> ![Request]
SingleRequest :: !Request -> BatchRequest
[getSingleRequest] :: BatchRequest -> !Request
class FromRequest q

-- | Parser for params Value in JSON-RPC request.
parseParams :: FromRequest q => Method -> Maybe (Value -> Parser q)
fromRequest :: FromRequest q => Request -> Either ErrorObj q
class ToRequest q

-- | Method associated with request data to build a request object.
requestMethod :: ToRequest q => q -> Method

-- | Is this request to be sent as a notification (no id, no response)?
requestIsNotif :: ToRequest q => q -> Bool
buildRequest :: (ToJSON q, ToRequest q) => Ver -> q -> Id -> Request
data Response
Response :: !Ver -> !Value -> !Id -> Response
[getResVer] :: Response -> !Ver
[getResult] :: Response -> !Value
[getResId] :: Response -> !Id
ResponseError :: !Ver -> !ErrorObj -> !Id -> Response
[getResVer] :: Response -> !Ver
[getError] :: Response -> !ErrorObj
[getResId] :: Response -> !Id
OrphanError :: !Ver -> !ErrorObj -> Response
[getResVer] :: Response -> !Ver
[getError] :: Response -> !ErrorObj
data BatchResponse
BatchResponse :: ![Response] -> BatchResponse
[getBatchResponse] :: BatchResponse -> ![Response]
SingleResponse :: !Response -> BatchResponse
[getSingleResponse] :: BatchResponse -> !Response
class FromResponse r

-- | Parser for result Value in JSON-RPC response. Method corresponds to
--   request to which this response answers.
parseResult :: FromResponse r => Method -> Maybe (Value -> Parser r)

-- | Parse a response knowing the method of the corresponding request.
fromResponse :: FromResponse r => Method -> Response -> Maybe r

-- | Type of function to make it easy to create a response from a request.
--   Meant to be used in servers.
type Respond q m r = q -> m (Either ErrorObj r)

-- | Create a response from a request. Use in servers.
buildResponse :: (Monad m, FromRequest q, ToJSON r) => Respond q m r -> Request -> m (Maybe Response)

-- | Error object from JSON-RPC 2.0. ErrorVal for backwards compatibility.
data ErrorObj
ErrorObj :: !String -> !Int -> !Value -> ErrorObj
[getErrMsg] :: ErrorObj -> !String
[getErrCode] :: ErrorObj -> !Int
[getErrData] :: ErrorObj -> !Value
ErrorVal :: !Value -> ErrorObj
[getErrData] :: ErrorObj -> !Value

-- | Get a user-friendly string with the error information.
fromError :: ErrorObj -> String

-- | Parse error.
errorParse :: ByteString -> ErrorObj

-- | Invalid request.
errorInvalid :: Value -> ErrorObj

-- | Invalid params.
errorParams :: Value -> ErrorObj

-- | Method not found.
errorMethod :: Method -> ErrorObj

-- | Id not recognized.
errorId :: Id -> ErrorObj
data Message
MsgRequest :: !Request -> Message
[getMsgRequest] :: Message -> !Request
MsgResponse :: !Response -> Message
[getMsgResponse] :: Message -> !Response
MsgBatch :: ![Message] -> Message
[getBatch] :: Message -> ![Message]
type Method = Text
data Id
IdInt :: !Int -> Id
[getIdInt] :: Id -> !Int
IdTxt :: !Text -> Id
[getIdTxt] :: Id -> !Text

-- | Pretty display a message id. Meant for logs.
fromId :: Id -> String

-- | JSON-RPC version.
data Ver

-- | JSON-RPC 1.0
V1 :: Ver

-- | JSON-RPC 2.0
V2 :: Ver
